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 这两个地方都不显示出来
{
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 这两个地方都不显示出来
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog( ButtonTest .this, "xyz!", "Inane warning", JOptionPane.ERROR_MESSAGE);
}}
构造窗体时加一句:
this.addWindowListener(this);