【发布时间】:2016-04-19 14:16:55
【问题描述】:
点击时,如何只关闭一个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 */
}
}
【问题讨论】:
-
请您具体说明您的原因,因为我想念您的问题
-
顺便说一句 - 本来想问这个,但忘记了。这个应用程序的确切原因是什么。在这个千年中使用 AWT 组件?您将在 Swing 上获得更好的帮助——如果只是因为开发人员可能在过去十年中使用过它并能记住它。 ;)
-
仅供参考,这适用于媒体播放器,在 Window2 上播放全高清电影,而 Window1 是按钮控制器。
-
“仅供参考,这是用于媒体播放器的..” 这应该是我问题的答案吗?如果您使用的库不支持 Swing,请获得更好的库。哎呀,即使是 JMF 也内置了 Swing 支持。无论是那个还是目标 1.7+,混合 Swing 和 AWT 再次成为“很酷的事情”。
-
谢谢大师 Andrew Thompson,它有效。我不必使用 AWT 我很困惑,因为有时在 Swing 中当我播放每秒 60 帧的电影时,我看到一些灰色背景出现,这就是我使用 AWT 的原因,但我不必切换到 Swing。
标签: java user-interface awt