【发布时间】: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;
}
}
【问题讨论】: