java swing 组件不显示的问题 swingjava 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该是窗口初始化后,没有调用刷新方法,试一下JFrame的repaint() public MainLayout(String session) { this.session=session; init(); setLayout(null); add(sess); add(check); add(inform); add(p); add(url); add(keyword); add(view);add(save); add(enterButton); add(delete); add(imgPanel); //设置imgPanel的透明度 imgPanel.setAlpha(.19f); setSize(660,750); this.setLocationRelativeTo(null); setVisible(true); setTitle("欢迎使用站内搜索系统v2.0"); setResizable(false); this.validate(); this.repaint(); } /*****初始化*************/ @SuppressWarnings({ "rawtypes", "unchecked" }) private void init() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); sess=new JLabel("欢迎,【"+session+"】"); sess.setBounds(480, 30, 200, 25); p.setBounds(26, 240, 602, 470); p.setLayout(new GridLayout(1,1)); p.add(scroller); scroller.setBackground(Color.green); p.setBackground(Color.cyan); jtextarea.setVisible(true); jtextarea.setEditable(false); jtextarea.setLineWrap(true); jtextarea.setText("搜索结果:"+"\n"); jtextarea.setBorder(new LineBorder(new Color(127,127,127), 1, true)); jtextarea.setOpaque(false); scroller.setOpaque(false); scroller.getViewport().setOpaque(false); scroller.getVerticalScrollBar().setOpaque(false); p.setOpaque(false); Font font=new Font("微软雅黑",Font.BOLD,15); jtextarea.setForeground(Color.WHITE); /*******关键组件****/ url= new JComboBox(); url.addItem("http://fanwen.chazidian.com/"); url.addItem("http://fanwen.chazidian.com/gongzuozongjiefanwen/"); url.addItem("http://fanwen.chazidian.com/gongzuojihua/"); url.addItem("http://fanwen.chazidian.com/fanwen312518/"); url.setBounds(150,70,400,25); url.setOpaque(false); url.setForeground(Color.RED); keyword=new JTextField(); keyword.setOpaque(false); keyword.setBounds(150,124,400,25); keyword.setForeground(Color.WHITE); enterButton=new JButton("搜 索"); enterButton.setBounds(152,177,80,25); enterButton.setFont(font); enterButton.setForeground(Color.BLUE); view=new JButton("查 看"); view.setBounds(254,177,80,25); view.setFont(font); view.setForeground(Color.BLUE); save=new JButton("保 存"); save.setBounds(354,177,80,25); save.setFont(font); save.setForeground(Color.BLUE); delete=new JButton("清 除"); delete.setBounds(450,177,80,25); delete.setFont(font); delete.setForeground(Color.BLUE); check=new JLabel(); check.setText("网络正常"); check.setBounds(47,177,100,25); check.setForeground(Color.GREEN); check.setFont(font); inform=new JLabel(); inform.setBounds(540,177,100,25); inform.setFont(font); inform.setForeground(Color.GREEN); /***************************************/ enterButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { inform.setText("执行中..."); if(keyword.getText().equals("")||url.getSelectedItem().toString().equals("")){ check.setText("参数为空!"); inform.setText(""); return; }else{ inform.setText("执行中..."); check.setText("网络正常"); addBtnActionListener(); inform.setText("执行完毕"); } } }); delete.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub addDeleteActionListener(); } }); save.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { saveBtnActionListener(); } }); view.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { viewData(); } }); } /***************为按钮添加清除事件**********************/ private void addDeleteActionListener(){ jtextarea.setText("搜索结果:"+"\n"); keyword.setText(""); url.setSelectedIndex(0); inform.setText(""); check.setText("网络正常"); } /**************为按钮添加搜索侦听事件*********/ private void addBtnActionListener() { inform.setText("执行中..."); //获取URL String Url = url.getSelectedItem().toString(); //声明编辑文本的变量和声明String的变量一样会报错 //获取keyword String keyWord = keyword.getText(); //System.out.println(keyWord); //得到http返回的字节流contentSource String contentSource=null; contentSource=s.getPage(new String(url.getSelectedItem().toString()),s.code); System.out.println(contentSource); //获得纯文本 String dealContent=exp.excuteTrimAndRemoveHtml(contentSource); System.out.println("---str----"); //temp是所有空格换成一个空格 String temp=dealContent.replaceAll("\\s{1,}", " "); System.out.println(temp); System.out.println("-------"); //获得含关键字的List<String>集合 String ss=exp.getStringByKeyWords(keyWord, temp).toString(); List<String> list=exp.getStringByKeyWords(keyWord, dealContent); int i=list.size(); System.out.println(i); System.out.print(ss); //分割字符串数组 if(i==1){ jtextarea.append("【1】"+ss.substring(1, ss.length()-1).toString()); }else if(i>1){ String[] strs=ss.split(","); int n=1; for(int m=0;m<strs.length;m++){ System.out.print("【"+n+"】"+strs[m]); jtextarea.append("【"+n+"】"+strs[m]+"\n"); n++; } }else{ jtextarea.append("没有匹配到结果!"); } } /**********保存搜索的数据**************/ private void saveBtnActionListener(){ if(jtextarea.getText().length()<10||keyword.getText().equals("")){ inform.setText("参数错误"); return; }else{ Dao d=new Dao(); d.validateInsert(d.getID(session), jtextarea.getText().toString()); System.out.println("insert success!!!!"); inform.setText("保存成功"); } } /**********查看数据*********/ private void viewData(){ getContentPane().setLayout(null); { JTable userTable; Dao view = new Dao(); Object[][] rowData = view.queryAllData(session); String clos[] = new String[]{ "记录编号", "拥有者ID","内容" }; TableModel DataTableModel = new DefaultTableModel(rowData,clos); userTable = new JTable(); JTableHeader head = userTable.getTableHeader(); //head.columnAdded(e) getContentPane().add(userTable); userTable.setModel(DataTableModel); userTable.setBounds(136, 102, 525, 234); } } this.repaint(),删除你试试。。这个方法能将GUI图像重画 你添加了事件响应后,当对添加事件响应区域的SWT控件进行操作,系统才又会自动调用repaint()方法,不知道楼主理解了么?希望有帮助 Thread t=new Test(){ int w=200,h=140; public void run(){ int i=0; while(i<10){ isdel.setSize(w,h); if(i%2==0){ w++; h++; }else{ w--; h--; } i++; } } }; t.start(); JFrame.setVisible(true);class Test extends Thread{}这样能让组件显示出来。不过我这也是万不得已的通过线程来改变窗口大小显示 JFrame 初始化放在最后 就可以了 如:package cn.book.frame;import java.awt.Font;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JPasswordField;import javax.swing.JTextField;import cn.book.gui.ColorLabel;public class LoginFrame extends JFrame{ private static final long serialVersionUID = 1L; private JFrame frame = null; private JButton jbok; private JPanel p = null; private JLabel jbName = null; private JLabel jbPassword = null; private JTextField jfName = null; private JPasswordField jpfPassword = null; private ColorLabel clmade = null; private ColorLabel clfindps = null; public LoginFrame(){ initColor(); initLabel(); initButton(); initField(); init(); } public void init(){ frame = new JFrame(); frame.setTitle("登录"); frame.setSize(350, 250); frame.setLocation(400, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(p); frame.setVisible(true); } public void initButton(){ jbok = new JButton("登录"); jbok.setBounds(100, 170, 150, 35); p.add(jbok); } public void initLabel(){ jbName = new JLabel("姓名"); jbName.setFont(new Font("楷体",Font.PLAIN,18)); jbName.setBounds(20, 30, 60, 35); p.add(jbName); jbPassword = new JLabel("密码"); jbPassword.setFont(new Font("楷体",Font.PLAIN,18)); jbPassword.setBounds(20, 70, 60, 35); p.add(jbPassword); } public void initField(){ jfName = new JTextField(""); jfName.setBounds(85, 30, 180, 35); jfName.setFont(new Font("楷体",Font.PLAIN,18)); p.add(jfName); jpfPassword = new JPasswordField(""); jpfPassword.setBounds(85, 70, 180, 35); jpfPassword.setEchoChar('*'); jpfPassword.setFont(new Font("楷体",Font.PLAIN,18)); p.add(jpfPassword); } public void initColor(){ p = new JPanel(null); clmade = new ColorLabel("注册用户"); clmade.setBounds(265, 35, 70, 30); p.add(clmade); } public static void main(String[] args) { new LoginFrame(); }} 有可能是,你在面板(A)上添加面板(B),面板(B)上添加了组件,但面板A 没有设置布局为NULL,并且面板(B)添加完组件后,没有刷新面板(A),即repaint,最小化后,就有可能面板(B)上的内容消失。 将setVisible(true); 放在构造函数的最后即可 这个程序不用内部类怎么实现? 求教:请问如何在Java程序里执行windows系统下的dos命令 java从键盘输入的问题 java兴趣小组,有兴趣的来 急! 排列的问题 一个关于Arrays中的sort()方法的问题? JAVA通过SOCKET接收二进制数据的困惑(用了三种方法都有不同问题),请教各位大虾 求救 java.io.* 有一个关于gui的问题 简单的问题!! 大量数据的排序问题,新手请教 下面代码怎么写成伪代码啊
this.session=session;
init();
setLayout(null);
add(sess);
add(check);
add(inform);
add(p);
add(url);
add(keyword);
add(view);add(save);
add(enterButton);
add(delete);
add(imgPanel);
//设置imgPanel的透明度
imgPanel.setAlpha(.19f);
setSize(660,750);
this.setLocationRelativeTo(null);
setVisible(true);
setTitle("欢迎使用站内搜索系统v2.0");
setResizable(false);
this.validate();
this.repaint();
}
/*****初始化*************/
@SuppressWarnings({ "rawtypes", "unchecked" })
private void init() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sess=new JLabel("欢迎,【"+session+"】");
sess.setBounds(480, 30, 200, 25);
p.setBounds(26, 240, 602, 470);
p.setLayout(new GridLayout(1,1));
p.add(scroller);
scroller.setBackground(Color.green);
p.setBackground(Color.cyan);
jtextarea.setVisible(true);
jtextarea.setEditable(false);
jtextarea.setLineWrap(true);
jtextarea.setText("搜索结果:"+"\n");
jtextarea.setBorder(new LineBorder(new Color(127,127,127), 1, true));
jtextarea.setOpaque(false);
scroller.setOpaque(false);
scroller.getViewport().setOpaque(false);
scroller.getVerticalScrollBar().setOpaque(false);
p.setOpaque(false);
Font font=new Font("微软雅黑",Font.BOLD,15);
jtextarea.setForeground(Color.WHITE);
/*******关键组件****/
url= new JComboBox();
url.addItem("http://fanwen.chazidian.com/");
url.addItem("http://fanwen.chazidian.com/gongzuozongjiefanwen/");
url.addItem("http://fanwen.chazidian.com/gongzuojihua/");
url.addItem("http://fanwen.chazidian.com/fanwen312518/");
url.setBounds(150,70,400,25);
url.setOpaque(false);
url.setForeground(Color.RED);
keyword=new JTextField();
keyword.setOpaque(false);
keyword.setBounds(150,124,400,25);
keyword.setForeground(Color.WHITE);
enterButton=new JButton("搜 索");
enterButton.setBounds(152,177,80,25);
enterButton.setFont(font);
enterButton.setForeground(Color.BLUE);
view=new JButton("查 看");
view.setBounds(254,177,80,25);
view.setFont(font);
view.setForeground(Color.BLUE);
save=new JButton("保 存");
save.setBounds(354,177,80,25);
save.setFont(font);
save.setForeground(Color.BLUE);
delete=new JButton("清 除");
delete.setBounds(450,177,80,25);
delete.setFont(font);
delete.setForeground(Color.BLUE);
check=new JLabel();
check.setText("网络正常");
check.setBounds(47,177,100,25);
check.setForeground(Color.GREEN);
check.setFont(font);
inform=new JLabel();
inform.setBounds(540,177,100,25);
inform.setFont(font);
inform.setForeground(Color.GREEN);
/***************************************/
enterButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inform.setText("执行中...");
if(keyword.getText().equals("")||url.getSelectedItem().toString().equals("")){
check.setText("参数为空!");
inform.setText("");
return;
}else{
inform.setText("执行中...");
check.setText("网络正常");
addBtnActionListener();
inform.setText("执行完毕");
}
}
});
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
addDeleteActionListener();
}
});
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveBtnActionListener();
}
});
view.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
viewData();
}
});
}
/***************为按钮添加清除事件**********************/
private void addDeleteActionListener(){
jtextarea.setText("搜索结果:"+"\n");
keyword.setText("");
url.setSelectedIndex(0);
inform.setText("");
check.setText("网络正常");
}
/**************为按钮添加搜索侦听事件*********/
private void addBtnActionListener() {
inform.setText("执行中...");
//获取URL
String Url = url.getSelectedItem().toString(); //声明编辑文本的变量和声明String的变量一样会报错
//获取keyword
String keyWord = keyword.getText();
//System.out.println(keyWord);
//得到http返回的字节流contentSource
String contentSource=null;
contentSource=s.getPage(new String(url.getSelectedItem().toString()),s.code);
System.out.println(contentSource);
//获得纯文本
String dealContent=exp.excuteTrimAndRemoveHtml(contentSource);
System.out.println("---str----");
//temp是所有空格换成一个空格
String temp=dealContent.replaceAll("\\s{1,}", " ");
System.out.println(temp);
System.out.println("-------");
//获得含关键字的List<String>集合
String ss=exp.getStringByKeyWords(keyWord, temp).toString();
List<String> list=exp.getStringByKeyWords(keyWord, dealContent);
int i=list.size();
System.out.println(i);
System.out.print(ss);
//分割字符串数组
if(i==1){
jtextarea.append("【1】"+ss.substring(1, ss.length()-1).toString());
}else if(i>1){
String[] strs=ss.split(",");
int n=1;
for(int m=0;m<strs.length;m++){
System.out.print("【"+n+"】"+strs[m]);
jtextarea.append("【"+n+"】"+strs[m]+"\n");
n++;
}
}else{
jtextarea.append("没有匹配到结果!");
}
}
/**********保存搜索的数据**************/
private void saveBtnActionListener(){
if(jtextarea.getText().length()<10||keyword.getText().equals("")){
inform.setText("参数错误");
return;
}else{
Dao d=new Dao();
d.validateInsert(d.getID(session), jtextarea.getText().toString());
System.out.println("insert success!!!!");
inform.setText("保存成功");
}
}
/**********查看数据*********/
private void viewData(){
getContentPane().setLayout(null);
{ JTable userTable;
Dao view = new Dao();
Object[][] rowData = view.queryAllData(session);
String clos[] = new String[]{ "记录编号", "拥有者ID","内容" };
TableModel DataTableModel = new DefaultTableModel(rowData,clos);
userTable = new JTable();
JTableHeader head = userTable.getTableHeader();
//head.columnAdded(e)
getContentPane().add(userTable);
userTable.setModel(DataTableModel);
userTable.setBounds(136, 102, 525, 234);
}
}
this.repaint(),删除你试试。。这个方法能将GUI图像重画 你添加了事件响应后,当对添加事件响应区域的SWT控件进行操作,系统才又会自动调用repaint()方法,不知道楼主理解了么?希望有帮助
int w=200,h=140;
public void run(){
int i=0;
while(i<10){
isdel.setSize(w,h);
if(i%2==0){
w++;
h++;
}else{
w--;
h--;
}
i++;
}
}
};
t.start();
JFrame.setVisible(true);class Test extends Thread{}
这样能让组件显示出来。不过我这也是万不得已的通过线程来改变窗口大小显示
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;import cn.book.gui.ColorLabel;public class LoginFrame extends JFrame{ private static final long serialVersionUID = 1L;
private JFrame frame = null;
private JButton jbok;
private JPanel p = null;
private JLabel jbName = null;
private JLabel jbPassword = null;
private JTextField jfName = null;
private JPasswordField jpfPassword = null;
private ColorLabel clmade = null;
private ColorLabel clfindps = null;
public LoginFrame(){
initColor();
initLabel();
initButton();
initField();
init();
}
public void init(){
frame = new JFrame();
frame.setTitle("登录");
frame.setSize(350, 250);
frame.setLocation(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(p);
frame.setVisible(true);
}
public void initButton(){
jbok = new JButton("登录");
jbok.setBounds(100, 170, 150, 35);
p.add(jbok);
}
public void initLabel(){
jbName = new JLabel("姓名");
jbName.setFont(new Font("楷体",Font.PLAIN,18));
jbName.setBounds(20, 30, 60, 35);
p.add(jbName); jbPassword = new JLabel("密码");
jbPassword.setFont(new Font("楷体",Font.PLAIN,18));
jbPassword.setBounds(20, 70, 60, 35);
p.add(jbPassword);
}
public void initField(){
jfName = new JTextField("");
jfName.setBounds(85, 30, 180, 35);
jfName.setFont(new Font("楷体",Font.PLAIN,18));
p.add(jfName);
jpfPassword = new JPasswordField("");
jpfPassword.setBounds(85, 70, 180, 35);
jpfPassword.setEchoChar('*');
jpfPassword.setFont(new Font("楷体",Font.PLAIN,18));
p.add(jpfPassword);
}
public void initColor(){
p = new JPanel(null);
clmade = new ColorLabel("注册用户");
clmade.setBounds(265, 35, 70, 30);
p.add(clmade);
}
public static void main(String[] args) {
new LoginFrame();
}
}