2016-05-26 30 views
0

我对java编程有点新,对我来说这么光荣。无论如何,我在一个类中有一个jframe对象,我希望能够从我的jpanel类(显然我附加到框架)关闭框架。无论如何,我尝试在jpanel中创建一个实例字段,使jframe对象具有一个实例字段,然后创建一个方法,我将使用jframe对象的参数调用jframe类,以便使jpanel实例字段成为与jframe对象相同的对象。然后我调用实例field.dispose();希望能够结束这个框架。任何想法将不胜感激!我的jframe不会配置

在,这是很难理解这里的情况是一个例子:

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

    } 

回答

4

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

我自己,在需要的时候我会得到从摆动本身顶层窗口,是这样的:

 @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

如果这没有帮助,请创建并发布实际代码,而不是实物代码,我们可以编译,运行并实际测试的代码,请查看链接。

其他问题:

  • 您的代码不符合Java的命名标准类名称都应该开始用大写字母。请仔细阅读并研究它,因为如果您的代码遵循标准,其他人(包括我们和您的未来自己)都会更好地了解您的代码。
  • 你很少想从JFrame延伸出来,因为你很少需要改变它的先天行为。通常您会在需要的时间和地点创建并使用JFrame或JDialog。