试图为我的Window实现close方法尽管java给了我一个
“未定义构造函数WindowEvent(WelcomeWindow,int)”:错误
给出错误的行“ WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);”
你能帮我一下吗
public class WelcomeWindow {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WelcomeWindow window = new WelcomeWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public WelcomeWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 248, 357);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{86, 0, 79, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
JButton btnNewButton = new JButton("Shop");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton.gridx = 1;
gbc_btnNewButton.gridy = 2;
frame.getContentPane().add(btnNewButton, gbc_btnNewButton);
JButton btnNewButton_1 = new JButton("Manage Store");
GridBagConstraints gbc_btnNewButton_1 = new GridBagConstraints();
gbc_btnNewButton_1.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton_1.gridx = 1;
gbc_btnNewButton_1.gridy = 3;
frame.getContentPane().add(btnNewButton_1, gbc_btnNewButton_1);
JButton btnManageUsers = new JButton("Manage Users");
GridBagConstraints gbc_btnManageUsers = new GridBagConstraints();
gbc_btnManageUsers.insets = new Insets(0, 0, 0, 5);
gbc_btnManageUsers.gridx = 1;
gbc_btnManageUsers.gridy = 5;
frame.getContentPane().add(btnManageUsers, gbc_btnManageUsers);
}
public void close(){
WindowEvent winClosingEvent = new WindowEvent(this,WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(winClosingEvent);
}
}
最佳答案
您尝试用于“ WindowEvent”的构造函数如下:
WindowEvent(Window source, int id)
因此,为了使用您,“ WelcomeWindow”类应继承自
java.awt.Window
而且由于您没有覆盖Window构造函数,所以我猜您没有这样做。
此处进一步参考:Java7 Docs
关于java - 构造函数WindowEvent(WelcomeWindow,int)是未定义的,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37402293/