㈠ java编程。比如在一个面板上有两个按钮,怎么实现分别点击这两个按钮然后弹出不同的窗口我的代码不
importjava.awt.Color;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JPanel;
publicclassTestextendsJFrame{
publicTest(){
this.setSize(400,300);
this.setLocationRelativeTo(null);//设置居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPaneljp=newJPanel();
finalJButtonjb1=newJButton("弹出蓝色");
JButtonjb2=newJButton("弹出青色");
jp.add(jb1);
jp.add(jb2);
//给第一个按钮添加监听
jb1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
JFramejf=newJFrame();
JPaneljp=newJPanel();
jf.setSize(200,200);
jf.add(jp);
jp.setBackground(Color.blue);
jf.setLocationRelativeTo(jb1);
jf.setVisible(true);
}
});
//给第二个按钮添加监听
jb2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
JFramejf=newJFrame();
JPaneljp=newJPanel();
jf.setSize(200,200);
jf.add(jp);
jp.setBackground(Color.cyan);
jf.setLocationRelativeTo(jb1);
jf.setVisible(true);
}
});
this.add(jp);
}
publicstaticvoidmain(Stringarg[]){
newTest().setVisible(true);
}
}