【问题标题】:Close one Frame (not the whole application) using AWT使用 AWT 关闭一帧(不是整个应用程序)
【发布时间】: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


【解决方案1】:
import java.awt.*;
import java.awt.event.*;

public class Test11 extends Frame {

    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");
                (new Test11("Window 1")).setVisible(true);
                System.out.println("Run: Window 2");
                (new Test11("Window 2")).setVisible(true);
            }
        });
    }
}

典型输出

Run: Window 1
Run: Window 2
Window 1 says Bye-Bye!  Mon Nov 14 10:20:25 EST 2011
Window 2 says Bye-Bye!  Mon Nov 14 10:20:35 EST 2011
Press any key to continue . . .

更新 1

此代码以编程方式关闭“窗口 2”。您的版本的问题是“时间”,由调用 later 引起的(您认为这意味着什么?)。它可以通过两种相对简单的方法之一进行修复。

  1. 杂物。添加一个 Swing Timer/ActionListener 设置为在主运行后 2 秒关闭。对于该路线,请取出 main 中所有已注释代码行的“注释部分”。
  2. 更好的解决方案。去掉对EventQueue.invokeLater()的调用,这与AWT组件无关。

这是修改后的代码:

import java.awt.*;
import java.awt.event.*;
// since the OP has not taken the time to explain 'why AWT',
// I choose to make life easy by using a Swing class.
import javax.swing.Timer;

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);
        //    }
        //});

        //ActionListener closeWindow = new ActionListener(){
        //  public void actionPerformed(ActionEvent ae) {
                System.out.println(
                    window2.getTitle() +
                    " says Bye-Bye!  " +
                    new java.util.Date());
                /* failed then try */
                window2.dispose();
        //  }
        //};
        //Timer timer = new Timer(2000,closeWindow);
        //timer.setRepeats(false);
        //timer.start();
    }
}

【讨论】:

  • @mKorbel(笑)我从 6->7(紧张的习惯)更新了 your 链接,并检查了链接是否有效,但没有意识到它指向了特定于 Swing 的方法。只有当我使用源代码时,为了展示使用setDefaultCloseOperation(int) 是多么容易,我的编译器 有用地警告我WTE“制作方法 - 停止!”。 ;) JavaDocs 很棒,但编译器更好。 :)
  • 1) 在 AWT 时代,我用沙子建造了塔 2) 我不相信 Java7 目前的形式,新的 o.w.n.e.r == 一切 d.i.e.d,只有 7 个
  • Re 2) 哦,别误会我的意思!我不会在这台机器上安装 J7。它对我的 DukeBox 根本不起作用,所以我恢复到 6,直到出现更稳定的 JRE。 JavaDocs OTOH 是另一回事。只是想看看 1.4.2 从 Google 链接中删除,并且获得大量指向 7 个文档的链接可能(?)产生这种结果。
  • 也许我错了,但你没有任何机会,如果我没记错的话,这个引擎有字母顺序,然后是 1.4.2、1.5,有时是 6(-:在 5mio 上,758T , Google 上的 856 位置 :-) 我从来没有见过像 Java7 之类的东西,Java7 是什么,没有 Google,抱歉没办法,--> 请不要停止思考 Java7,也许一天一次,但不是明天:- ) 笑话>
  • @Andrew Thompson:请查看我的编辑(跟进部分)。我现在还不能以这种方式关闭 Window2(在程序上不是,但是可以用鼠标单击它关闭)。
【解决方案2】:

查看setDefaultCloseOperation()HIDE_ON_CLOSE 是该行为的正确属性,这些方法对 Swing 有效,

编辑:

如果你想重复使用这个/这些容器,那么

this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent we) {
        this.setVisible(false);
    }
});

【讨论】:

  • 我打开了两个框架/窗口 (AWT)。一旦我想关闭一个,我只想关闭那个。但不是像 System.exit(0) 这样的整个应用程序现在都在做。
  • @Google,如果您想了解这些变化,请查看 download.oracle.com/javase/tutorial/uiswing/events/…download.oracle.com/javase/tutorial/uiswing/events/…,两者都适用于 AWT 和 Swing Container
  • 我在download.oracle.com/javase/6/docs/api/java/awt/Frame.html 中都看到了,然后在默认情况下看到了 JFrame,或者我在你的评论中错过了另一个东西 :-)
  • OP使用的是纯AWT,所以没有setDefaultCloseOperation(int)可用。
  • this.setVisible(false); 的问题在于它没有为该帧处理线程。全部关闭后,JRE 仍将运行。如果它们被 丢弃, 就像在我的示例中一样,JRE 将在最后一帧消失后退出。
【解决方案3】:

最简单的方法是使用:

dispose();

如果放置在 onClickListener 中,这只会关闭与文件关联的窗口。这不会终止整个程序。

【讨论】:

    【解决方案4】:

    在设计模式下,您必须使用正确的 clic 转到 jframe 的属性并选择: DISPOSE 在默认关闭操作行(通常是第一行)。

    所有这些操作在设计上都比较容易。 我说的是netbeans,但我希望它在另一个ide中是相似的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 2011-09-19
      • 2023-04-10
      • 2012-11-20
      • 2017-04-28
      相关资源
      最近更新 更多