簡體   English   中英

如何配置jframe

[英]How to dispose jframe

我在一個類中有一個jframe對象,我希望能夠從我的jpanel類(顯然我將其附加到該框架)上關閉該框架。 無論如何,我嘗試使用jframe對象在jpanel中創建一個實例字段,然后使用我制作的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;
   }

  }

您的代碼和問題很難遵循,因為您有一個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命名標准,因為類名都應以大寫字母開頭。 請Google對此進行研究,因為如果您的代碼符合標准,那么其他人,包括我們和您的未來自我,將更好地理解您的代碼。
  • 您幾乎不需要從JFrame進行擴展,因為您幾乎不需要更改它的固有行為。 通常,您將在需要的時間和地點創建並使用JFrame或JDialog。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM