public class ButtonTest extends JFrame implements WindowListener
{
ButtonTest (){
init_Frame();

//create Button
JButton yelloBut = new JButton("Yellow");
JButton blueBut = new JButton("Blue");

buttonPanel = new JPanel();
buttonPanel.add(yelloBut);
buttonPanel.add(blueBut);
add(buttonPanel);

yelloBut.addActionListener(new buttonAction(Color.YELLOW));
blueBut.addActionListener(new buttonAction(Color.BLUE));

setVisible(true);
}

protected void init_Frame(){
Toolkit tk;

tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
width = screenSize.width / 2;
height = screenSize.height / 2;
this.setSize(width, height);
this.setLocationByPlatform(true);
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Test event");
}

public void windowClosing(WindowEvent e){
JOptionPane.showMessageDialog(null, "xyz", "dddd", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
public void windowOpened(WindowEvent e) {
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "xyz", "dddd", JOptionPane.ERROR_MESSAGE);
}windowClosing 和 windowOpened 这两个地方都不显示出来

解决方案 »

  1.   

    JOptionPane.showMessageDialog( ButtonTest .this, "xyz!", "Inane warning", JOptionPane.ERROR_MESSAGE);
      

  2.   

    用这个ButtonTest.this  也没显示
      

  3.   

    刚学swing写着来练,不要介意
      

  4.   

    yelloBut.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog( ButtonTest .this, "xyz!", "Inane warning", JOptionPane.ERROR_MESSAGE);
    }}
      

  5.   

    兄弟,我是要在 windowOpened中显示,不是button
      

  6.   

    监听器都没有注册上去,当然没有用
    构造窗体时加一句:
    this.addWindowListener(this);