我在Panel1中放置了4个按钮,每个按钮都对应一个面板,
当点击regester时,我希望RegesterPanel这个面板出现在BorderLayout.center的位置(其他的按钮对应放置)
可实际的程序点击出现异常,哪位高手能帮忙改一下actionPerformed  e.getSource()==regester{}
这段代码,或者用cardLayout(),将RegesterPanel出现在中央,感激不尽!
对您的关注深表感谢
public class Account extends JApplet{
Panel1 p1;
public void init(){
setSize(400,200);
p1=new Panel1();
setLayout(new BorderLayout());
add(p1,BorderLayout.NORTH);
}
}
class Panel1 extends JPanel implements ActionListener{
JButton regester,save,withdraw,show;

public Panel1(){
setLayout(new FlowLayout());
setBackground(Color.pink);
regester=new JButton("Regester");
regester.addActionListener(this);
save=new JButton("SaveMoney");
save.addActionListener(this);
withdraw=new JButton("Withdraw");
withdraw.addActionListener(this);
show=new JButton("Check");
show.addActionListener(this);
add(regester); add(save); 
add(withdraw); add(show);

}
public void actionPerformed(ActionEvent e) {
Account a=null;
if(e.getSource()==regester){
RegesterPanel regesters=new RegesterPanel();
a.add(regesters,BorderLayout.CENTER);
}
else if(e.getSource()==save){

}
else if(e.getSource()==withdraw){

}
else if(e.getSource()==show){

}
}
}
class RegesterPanel extends JPanel implements ActionListener{
JLabel label1,label2,label3,label4;
JTextField text1,text2,text3;
Box baseBox,boxV1,boxV2;
JButton button;
public RegesterPanel(){
label1=new JLabel("请输入您的姓名");
label2=new JLabel("请输入您的地址");
label3=new JLabel("请输入开户金额");
label4=new JLabel("点击确认");
text1=new JTextField(15);
text2=new JTextField(15);
text3=new JTextField(15);
button=new JButton("确认");
boxV1=Box.createVerticalBox();
boxV1.add(label1);
boxV1.add(Box.createVerticalStrut(14));
boxV1.add(label2);
boxV1.add(Box.createVerticalStrut(12));
boxV1.add(label3);
boxV1.add(Box.createVerticalStrut(12));
boxV1.add(label4);
boxV2=Box.createVerticalBox();
boxV2.add(Box.createVerticalStrut(10));
boxV2.add(text1);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(text2);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(text3);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(button);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(15));
baseBox.add(boxV2);
add(baseBox);
}
public void actionPerformed(ActionEvent e) {

}
}
class SavePanel extends JPanel{
}
class WithdrawPanel extends JPanel{
}
class ShowPanel extends JPanel{
}

解决方案 »

  1.   


                   public void actionPerformed(ActionEvent e) {
    //Account a=null;
    if(e.getSource()==regester){
    RegesterPanel regesters=new RegesterPanel();
    //a.add(regesters,BorderLayout.CENTER);
    this.add(regesters,BorderLayout.SOUTH);
    //regesters.setVisible(true);
    }
    else if(e.getSource()==save){

    }
    else if(e.getSource()==withdraw){

    }
    else if(e.getSource()==show){

    }
    }