×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Learning SWING,write a simple code to test event mode,why can not exit from system menu,please help!,button is working

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class EventDemo1 extends JFrame {
JButton jbtnExit = new JButton("Exit");
EventDemo1()
{
setTitle("Event Demo 1");

addWindowListener(new WindowAdapter(){
public void windowClosed(WindowEvent e)
{
System.exit(0);
}
});
jbtnExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});

getContentPane().add(jbtnExit,BorderLayout.SOUTH);
setSize(300,300);
show();
}
JButton jbtnButton = new JButton("Exit");
public static void main(String[] args) {
new EventDemo1();
}
}
Report

Replies, comments and Discussions:

  • 工作学习 / 专业技术讨论 / Learning SWING,write a simple code to test event mode,why can not exit from system menu,please help!,button is working
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class EventDemo1 extends JFrame {
    JButton jbtnExit = new JButton("Exit");
    EventDemo1()
    {
    setTitle("Event Demo 1");

    addWindowListener(new WindowAdapter(){
    public void windowClosed(WindowEvent e)
    {
    System.exit(0);
    }
    });
    jbtnExit.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    });

    getContentPane().add(jbtnExit,BorderLayout.SOUTH);
    setSize(300,300);
    show();
    }
    JButton jbtnButton = new JButton("Exit");
    public static void main(String[] args) {
    new EventDemo1();
    }
    }
    • Swing中的event listener-WindowAdapter 中的windowClosed(WindowEvent) 和windowClosing(WindowEvent)有什么区别?高手指点一下,多谢了!!
    • you may have already had the answer, add this method inside your addWindowListener: public void windowClosing(WindowEvent e){ dispose(); }
      Frame inherits the dispose method from its Window superclass. When called, dispose destroys all native resources (including memory) associated with a component object, and, a call to dispose results in a call to a registered WindowListener's windowClosed method.