【问题标题】:How to dispose jframe如何处理jframe
【发布时间】:2016-09-25 01:11:56
【问题描述】:

我在一个类中有一个 jframe 对象,我希望能够从我的 jpanel 类中关闭框架(显然我附加到框架上)。无论如何,我尝试在我的 jpanel 中使用 jframe 对象创建一个实例字段,然后使用我创建的 jframe 对象的参数在 jframe 类中调用一个方法,这样我就可以使 jpanel 实例字段与 jframe 对象相同的对象。然后我调用了实例 field.dispose();希望它会关闭框架。任何想法将不胜感激!

如果难以理解,这里举个例子:

public class example extends jFrame
{
public static void main(String[]args)
{
    examplePanel ep = new examplePanel();
    example e = new example(ep);

}
/**
 * Constructor for objects of class example
 */
 public example(examplePanel ep)
 {
    //code that handles my frame settings
 }
 }

  public class examplePanel extends jPanel implements ActionListener
  {
   private example e;
   private boolean checkWin;
   public void actionPerformed(ActionEvent e)
   {
    if(this.checkWin())
   {
       setVisible(false);
       e.dispose();
       //^this line of code is supposed to dispose of the frame but it does  not
   }  
   }

   public void getExample(example e)
   {
    this.e = e;
   }

  }

【问题讨论】:

    标签: java swing jframe jpanel


    【解决方案1】:

    您的代码和问题很难理解,因为您有一个 ActionListener,您将其添加到没有 JButton 或 JMenuItem,您创建了一个 JFrame 对象和一个 JPanel,但从未观察到将面板添加到框架中。你给你的 JPanel 一个“示例”变量,但从不给它分配一个对可视化 JFrame 的引用,你似乎没有设置过 JFrame 的默认关闭操作,所以你上面写的 JFrame 应该是不可关闭的。从您的代码看来,JPanel 中的 examplePanel 的 e 变量实际上应该为 null,因此在其上调用任何方法都应该抛出 NullPointerException,除非您为其分配了正确的 JFrame 对象引用,但不是向我们展示。

    我自己,我会在需要时从 Swing 本身获取顶级窗口,例如:

            @Override
            public void actionPerformed(ActionEvent e) {
                // get the top-level window that is displaying this JPanel
                Window win = SwingUtilities.getWindowAncestor(this);
                if (win != null) {
                    win.dispose();  // dispose of it
                }
            }
    

    例如:

    import java.awt.Dimension;
    import java.awt.Window;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CloseFromJPanel extends JPanel implements ActionListener {
        private static final int PREF_W = 400;
        private static final int PREF_H = 300;
    
        public CloseFromJPanel() {
            JButton closeButton = new JButton("Close Me");
            closeButton.addActionListener(this);
    
            add(closeButton);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // get the top-level window that is displaying this JPanel
            Window win = SwingUtilities.getWindowAncestor(this);
            if (win != null) {
                win.dispose();  // dispose of it
            }
        }
    
        @Override
        public Dimension getPreferredSize() {
            if (isPreferredSizeSet()) {
                return super.getPreferredSize();
            }
            return new Dimension(PREF_W, PREF_H);
        }
    
        private static void createAndShowGui() {
            JFrame frame = new JFrame("Close From JPanel");
    
            // GUI will exit when the JFrame is closed
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().add(new CloseFromJPanel());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(() -> createAndShowGui());
        }
    
    }
    

    此代码适用于 JFrames 和 JDialogs 中的 JButton,但不适用于 JMenuItems 或 JApplets(我不认为)。或者,如果您只想结束应用程序,那么您可以简单地从 actionPerformed 方法中调用 System.exit(0)。如果您绝对想使用 JFrame 的字段来执行此操作,那么您需要将对 JFrame 的引用传递到 JPanel,可能使用构造函数参数,并可能传递 this

    如果这没有帮助,请创建并发布真正的代码,而不是某种代码,我们可以编译、运行和实际测试的代码,MCVE(请查看链接)。

    其他问题:

    • 您的代码不符合 Java 命名标准,因为类名都应以大写字母开头。请谷歌并研究它,因为如果您的代码遵循标准,其他人,包括我们和您未来的自己,将能够更好地理解您的代码。
    • 您很少需要从 JFrame 扩展,因为您很少需要更改它的固有行为。通常,您会在需要的时间和地点创建和使用 JFrame 或 JDialog。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多