import?java.awt.event.ActionEvent;
import?java.awt.event.ActionListener;
import?javax.swing.JButton;
import?javax.swing.JFrame;
import?javax.swing.JPanel;
public?class?Test?extends?JFrame{
public?Test(){
this.setSize(400,300);
this.setLocationRelativeTo(null);//設置居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel?jp?=new?JPanel();
final?JButton?jb1?=?new?JButton("彈出藍色");
JButton?jb2?=?new?JButton("彈出青色");
jp.add(jb1);
jp.add(jb2);
//給第壹個按鈕添加監聽
jb1.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
JFrame?jf=new?JFrame();
JPanel?jp=new?JPanel();
jf.setSize(200,?200);
jf.add(jp);
jp.setBackground(Color.blue);
jf.setLocationRelativeTo(jb1);
jf.setVisible(true);
}
});
//給第二個按鈕添加監聽
jb2.addActionListener(new?ActionListener()?{
public?void?actionPerformed(ActionEvent?e)?{
JFrame?jf=new?JFrame();
JPanel?jp=new?JPanel();
jf.setSize(200,?200);
jf.add(jp);
jp.setBackground(Color.cyan);
jf.setLocationRelativeTo(jb1);
jf.setVisible(true);
}
});
this.add(jp);
}
public?static?void?main(String?arg[]){
new?Test().setVisible(true);
}
}