java关闭当前framework_java – 使用AWT关闭一个Frame(不是整个应用程序)

点击,如何只关闭一个框架而不是两个或整个应用程序?

(我也尝试过AWT事件发送,EDT)

package test;

import java.awt.*;

import java.awt.AWTEvent;

import java.awt.EventQueue;

import java.awt.Toolkit;

import java.awt.event.*;

import java.lang.reflect.InvocationTargetException;

public class Test11 extends Frame implements MouseListener

{

public static Frame gp;

public Test11()

{

try {

this.setLayout (new BorderLayout ());

Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

this.setBounds(screen.width-400,33,400, 400);

this.setBackground(Color.red);

this.addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent we)

{

System.exit(0);

}

});

this.addMouseListener(this);

this.setVisible(true);

} finally {

}

}

/* How do i do AWT Event Dispatch (EDT): to cloase AWT window? */

public static void main(String[] args) throws InterruptedException, InvocationTargetException

{

/* EDT: AWT Event Dispatch

EventQueue eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();

eventQueue.push(new MyEventQueue()); */

/* Simple close */

EventQueue.invokeAndWait(new Runnable()

{

public void run()

{

System.out.println("Run: Window 1");

gp = new Test11();

gp.setVisible(true);

//gp.setVisible(false);

}

});

/* Simple close */

EventQueue.invokeAndWait(new Runnable()

{

public void run()

{

System.out.println("Run: Window 2");

new Test11().setVisible(true);

}

});

}

@Override

public void mouseClicked(MouseEvent me)

{

System.out.println("Clicked: out of Window1 or Window2, close only any one not whole application");

System.exit(0);

}

@Override

public void mousePressed(MouseEvent me) {

throw new UnsupportedOperationException("Not supported yet.");

}

@Override

public void mouseReleased(MouseEvent me) {

throw new UnsupportedOperationException("Not supported yet.");

}

@Override

public void mouseEntered(MouseEvent me) {

throw new UnsupportedOperationException("Not supported yet.");

}

@Override

public void mouseExited(MouseEvent me) {

throw new UnsupportedOperationException("Not supported yet.");

}

/* EDT: Extended class

private static class MyEventQueue extends EventQueue

{

public void postEvent(AWTEvent theEvent)

{

System.out.println("Event Posted");

super.postEvent(theEvent);

}

}

*/

}

跟进:

import java.awt.*;

import java.awt.event.*;

public class Test11 extends Frame

{

public static Frame window1;

public static Frame window2;

public Test11(String title) {

super(title);

setSize(400, 400);

setBackground(Color.red);

addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {

System.out.println(

getTitle() +

" says Bye-Bye! " +

new java.util.Date());

dispose();

}

});

setLocationByPlatform(true);

}

public static void main(String[] args) {

/* AFAIU starting the GUI on the EDT only applies to Swing.*/

EventQueue.invokeLater(new Runnable() {

public void run() {

System.out.println("Run: Window 1");

window1 = new Test11("Window 1");

window1.setVisible(true);

System.out.println("Run: Window 2");

window2 = new Test11("Window 2");

window2.setVisible(true);

}

});

/* Remotely: i need to close thi window, not manually */

window2.setVisible(false);

/* failed then try */

window2.dispose();

/* Now: I should have only Window1, but that i am not able to make yet */

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值