
发送
JDialog一个
WindowEvent.WINDOW_CLOSING使用事件
dispatchEvent(),
附录:
System.exit()应谨慎使用,在下面的示例中,第二个非守护线程正常完成,即使对话框在退出循环之前已关闭。有关详细信息,请参见JLS§12.8程序退出
。
import java.awt.EventQueue;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.AbstractAction;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;public class DialogEventTest extends JDialog { public DialogEventTest() { this.setLayout(new GridLayout(0, 1)); this.add(new JLabel("Dialog event test.", JLabel.CENTER)); this.add(new JButton(new AbstractAction("Close") { @Override public void actionPerformed(ActionEvent e) { DialogEventTest.this.setVisible(false); DialogEventTest.this.dispatchEvent(new WindowEvent( DialogEventTest.this, WindowEvent.WINDOW_CLOSING)); } })); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.out.println(e.paramString()); } }); } private void display() { this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new DialogEventTest().display(); } }); new Thread(new Runnable() { @Override public void run() { System.out.println("Starting…"); for (int i = 0; i < 5; i++) { try { Thread.sleep(1000); System.out.println((i + 1) + "s. elapsed."); } catch (InterruptedException e) { e.printStackTrace(System.err); } } System.out.println("Finished."); } }).start(); }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)