古詩詞大全網 - 成語經典 - Java編程。比如在壹個面板上有兩個按鈕,怎麽實現分別點擊這兩個按鈕然後彈出不同的窗口?我的代碼不

Java編程。比如在壹個面板上有兩個按鈕,怎麽實現分別點擊這兩個按鈕然後彈出不同的窗口?我的代碼不

import?java.awt.Color;

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);

}

}