我有几个课程,但我只发布了我的主课程。大部分代码都来自我教授的示例程序。我的问题是我要在窗口按钮关闭时运行检查。 'check'将查看另一个类中“已保存”的布尔变量是True还是False,并相应地执行操作。然后,关上窗户。到目前为止,我有这个,但我从JFrameL得到一个错误。如何使用其他类的WindowClosing方法?我看到我不应该自己打电话了吗?
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Main {
public static JFrameL frame;
public static void main(String[] args) {
// Create a window.
frame = new JFrameL("Checking Account Actions");
// Set the size of the window.
frame.setSize(450, 350);
frame.setAlwaysOnTop(true);
// Specify what happens when the close button is clicked.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
CheckingAccountActions panel = new CheckingAccountActions();
frame.getContentPane().add(panel);
frame.pack();
// Display the window
frame.setVisible(true);
}
public class JFrameL extends JFrame {
public JFrameL(String title) {
super(title);
FrameListener listener = new FrameListener();
addWindowListener(listener);
}
private class FrameListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("WindowListener method called: windowClosed.");
if(!CheckingAccountActions.saved) {
String message = "The data in the application is not saved.\n"
+ "Would you like to save it before exiting the application?";
int confirm = JOptionPane.showConfirmDialog (null, message);
if (confirm == JOptionPane.YES_OPTION)
CheckingAccountActions.chooseFile(2);
}
Main.frame.setVisible(false);
System.exit(0);
}
}
}
}
我在第2行从main()收到的错误消息:
“不能访问Main类型的封闭实例。必须使用Main类型的封闭实例限定分配(例如x.new A(),其中x是Main的实例)。”
答案 0 :(得分:3)
如,
class MyWindowAdapter extends WindowAdapter {
private CheckingAccountActions checkActions;
public MyWindowAdapter(CheckingAccountActions checkActions) {
this.checkActions = checkActions;
}
// in your window closing method
// check the state of checkActions first before doing anything
public void windowClosing(WindowEvent e) {
// note -- don't check for saved in a static way
// use a method on the instance.
if(!checkActions.getSaved()) {
// etc...
}
JFrame frame = (JFrame)e.getSource();
frame.setVisible(false);
// Main.frame.setVisible(false);
System.exit(0);
}
}
然后在主要:
public static void main(String[] args) {
frame = new JFrame("Checking Account Actions");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
CheckingAccountActions panel = new CheckingAccountActions();
MyWindowAdapter winAdapter = new MyWindowAdapter(panel);
frame.addWindowListener(winAdapter);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
修改强>
这种静态过度使用的气味:CheckingAccountActions.saved
。
答案 1 :(得分:0)
我怀疑你需要做的是移动panel
以便以后可以访问它。
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Main {
public static JFrameL frame;
public static CheckingAccountActions panel;
public static void main(String[] args) {
// Create a window.
frame = new JFrameL("Checking Account Actions");
// Set the size of the window.
frame.setSize(450, 350);
frame.setAlwaysOnTop(true);
// Specify what happens when the close button is clicked.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
panel = new CheckingAccountActions();
frame.getContentPane().add(panel);
frame.pack();
// Display the window
frame.setVisible(true);
}
public class JFrameL extends JFrame {
public JFrameL(String title) {
super(title);
FrameListener listener = new FrameListener();
addWindowListener(listener);
}
private class FrameListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.out.println("WindowListener method called: windowClosed.");
if(!panel.saved) {
String message = "The data in the application is not saved.\n"
+ "Would you like to save it before exiting the application?";
int confirm = JOptionPane.showConfirmDialog (null, message);
if (confirm == JOptionPane.YES_OPTION)
panel.chooseFile(2);
}
Main.frame.setVisible(false);
System.exit(0);
}
}
}