×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

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.