这个程序怎么改改
package gui;import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Smain extends JFrame implements ActionListener {
private JLabel Top;
private JButton find;
private JButton add;
private JButton modify;
private JButton delete;
private JPanel tp;
private JPanel panel1;
private JPanel panel2;
public Smain(){
super("主界面");
Container container=getContentPane();
container.setLayout(new BorderLayout(8,8));
this.setBackground(Color.RED);
this.setBounds(400,200,500,500); //大小与位置;
Top = new JLabel("学生信息管理系统",SwingConstants.CENTER);
tp= new JPanel();
tp.setBackground(Color.yellow);
tp.add(Top);
find= new JButton("find");
find.addActionListener(this);//find 按钮进行事件监听;
add= new JButton("add");
add.addActionListener(this);//add 按钮进行事件监听;
modify= new JButton("modify");
modify.addActionListener(this);//modify 按钮进行事件监听;
delete= new JButton("delte");
delete.addActionListener(this);//delete 按钮进行事件监听;
panel1=new JPanel();
panel1.setBackground(Color.yellow);
panel1.add(find);
panel1.add(add);
panel1.add(modify);
panel1.add(delete); //添加按钮到panel1面板;
panel2= new JPanel();
panel2.setBackground(Color.white);
container.add(BorderLayout.NORTH,tp);
container.add(panel2,BorderLayout.CENTER);
container.add(panel1,BorderLayout.SOUTH);
pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent event){
JLabel jt= new JLabel("hello",SwingConstants.CENTER);
if (event.getSource()==find){
panel2.add(jt);
//container.add(panel2,BorderLayout.CENTER);
//JOptionPane.showMessageDialog(null,"hello" );
}
else if(event.getSource()==modify){}
else if(event.getSource()==add ){}
else if(event.getSource()==delete){}
}
public static void main(String[] args) {
Smain smain= new Smain();
smain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
为什么执行后点击 add按钮后,窗体不会立即显示出 hello 标签 ,怎么改才能立即显示出来呢~大虾指教~~~
----------------解决方案--------------------------------------------------------
container.add(BorderLayout.NORTH,tp);
里面的参数顺序对吗 ??
----------------解决方案--------------------------------------------------------
这个简单 我以前也遇到过 很郁闷
if (event.getSource()==find){
panel2.add(jt);
validate(); }
----------------解决方案--------------------------------------------------------
谢了,搞定
----------------解决方案--------------------------------------------------------