使用AWT关闭一个Frame(不是整个应用程序)

时间:2011-11-13 22:45:24

标签: java user-interface 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 */
    }
}

4 个答案:

答案 0 :(得分:5)

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

此代码关闭" Window 2"编程。你的版本的问题是“时间安排”,这是由于稍后调用的呼叫造成的(您认为这意味着什么?)。它可以用两种相对简单的方法中的一种来修复。

  1. kludge。添加Swing Timer / ActionListener设置为在主要运行后2秒后关闭。对于该路线,请取出评论部分'主要的所有注释代码行。
  2. 更好的解决方案。移除对EventQueue.invokeLater()的调用,这与AWT组件无关。
  3. 以下是更改后的代码:

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

答案 1 :(得分:3)

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

编辑:

如果你想重用这个/这些容器,那么<​​/ p>

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

答案 2 :(得分:2)

最简单的方法是使用:

dispose();

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

答案 3 :(得分:0)

在设计模式中,你必须使用正确的clic来选择jframe的特性并选择: 在defaultcloseoperation行中显示(通常是第一行)。

所有这些操作在设计上都比较容易。 我正在谈论netbeans,但我希望它与另一个ide相似。