Java 使用AWT关闭一个帧(而不是整个应用程序)

Java 使用AWT关闭一个帧(而不是整个应用程序),java,user-interface,awt,Java,User Interface,Awt,单击后,如何仅关闭一个框架而不是同时关闭两个或整个应用程序? (我也尝试过AWT事件调度,EDT) 跟进: 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

单击后,如何仅关闭一个
框架
而不是同时关闭两个或整个应用程序? (我也尝试过AWT事件调度,EDT)

跟进:

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 */
    }
}
查看:
HIDE\u ON\u CLOSE
是该行为的适当属性,这些方法对Swing有效

编辑:

如果要重用此/这些容器,则

典型输出 更新1 此代码以编程方式关闭“窗口2”。您的版本出现了“计时”问题,这是由稍后调用引起的(您认为这是什么意思?)。它可以用两种相对简单的方法之一进行修复

  • 乱七八糟的。添加一个Swing
    计时器
    /
    ActionListener
    设置为在主运行后2秒关闭。对于该路由,去掉main中所有注释代码行的“注释部分”
  • 更好的解决方案。删除对
    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();
        }
    }
    

    最简单的方法是使用:

    dispose();
    

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

    在设计模式下,您必须使用右clic转到jframe的属性并选择: 在defaultcloseoperation行中处理(通常是第一行)

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

    请你把你的理由说得具体一点,因为我在你的问题中错过了这一点。顺便说一句,我本来想问这个的,但是忘了。这个应用程序的确切原因是什么。在这个千年中使用AWT组件?您将在Swing上获得更好的帮助——如果只是因为开发人员可能在过去十年中使用过Swing并且能够记住它的话仅供参考,这是一个媒体播放器,在Window2上播放全高清电影,Window1是按钮控制器。“仅供参考,这是一个媒体播放器…”这应该是我问题的答案吗?如果您正在使用的库不支持Swing,请使用更好的库。见鬼,就连JMF都内置了摇摆支架。或者是目标1.7+,在那里混合Swing和AWT再次成为“酷的东西”。谢谢大师安德鲁·汤普森,它很管用。我不必使用AWT,我很困惑,因为有时在Swing中,当我播放每秒60帧的电影时,我会看到一些灰色背景出现,这就是为什么我使用AWT,但它不是必须的,我将切换到Swing。我打开了两个帧/窗口(AWT)。一旦我想关闭一个,我只想关闭那个。但不是像System.exit(0)这样的整个应用程序正在运行。@Google,如果您想了解有关这些更改的一些信息,请检查和,AWT和Swing容器都可以访问,我在中看到了,然后默认情况下是JFrame,或者我在您的注释中遗漏了其他内容:-)OP使用的是纯AWT,因此没有可用的
    setDefaultCloseOperation(int)
    是指它不会处理该帧的线程。在它们全部关闭后,JRE仍将运行。如果像我的例子中那样,它们被处理掉,JRE将在最后一帧消失后退出。@mKorbel(咯咯笑)我从6->7(神经习惯)更新了你的链接,并检查了链接是否有效,但没有意识到它指向了特定于Swing的方法。只有在我使用源代码的时候,为了展示使用
    setDefaultCloseOperation(int)
    是多么容易,我的编译器才有用地警告我“编写方法-停止它!”)Javadoc很棒,但编译器更好。:)1) 在AWT时代,我用沙子建造了塔楼2)我不相信现在形式的Java7,新的o.w.n.e.r==一切都是d.I.e.d,只有7个是真的2)哦,别误会!我还没有在这台机器上安装J7。它对我的DukeBox根本不起作用,所以我恢复到6,直到一个更稳定的JRE出现。JavaDocs OTOH是另一回事。只想看到1.4.2从Google链接中退出,获得大量指向7个文档的链接可能会(?)产生这样的结果。也许我错了,但你没有机会,如果我没记错的话,这个引擎有字母顺序,然后是1.4.2,1.5,有时是6(:在5mio上,758T,856在Google上:-),我从来没有见过像Java7这样的东西,什么是Java7,没有Google,对不起没有办法,-->请不要停止用Java7思考,也许有一天,但明天不行:-)@安德鲁·汤普森:请看我的编辑(后续部分)。我现在还不能用那种方式关闭Window2(从语法上讲不是,但用鼠标点击它关闭是的)。
    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 . . .
    
    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();
        }
    }
    
    dispose();