使用按钮关闭 JFrame
Closing JFrame with Button
我目前正在使用 Eclipse 的拖放功能,我有一个应用程序 window,它默认带有 JFrame,并且能够 setVisible(false);
但我创建的另一个 frames/panel/window使用 JPanel 和扩展 JFrame。
因为extend
我无法setVisible(false or true);
它对window完全没有影响它仍然是正确的。
我的代码:
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LibrarianMenu frame = new LibrarianMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LibrarianMenu() {
setTitle("Librarian");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 385, 230);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
.
.
. so on
我在这里尝试执行我的按钮:
btnLogout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
LibrarianMenu frame = new LibrarianMenu();
frame.setVisible(false);
}
});
有什么解决办法吗?
因为您是在 Runnable 内部创建框架,所以它的范围仅限于 runnable。尝试在 runnable 之外声明变量,然后在 runnable 内初始化它,如下所示:
private JPanel contentPane;
private LibrarianMenu frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new LibrarianMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
然后在不声明 LibrarianMenu 的新实例的情况下将可见设置为 false:
btnLogout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
发生这种情况是因为每次按下按钮都会创建该框架的一个新实例。这是您更新的代码:
static LibrarianMenu frame ;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new LibrarianMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
注销按钮事件应该是这样的:
btnLogout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
我目前正在使用 Eclipse 的拖放功能,我有一个应用程序 window,它默认带有 JFrame,并且能够 setVisible(false);
但我创建的另一个 frames/panel/window使用 JPanel 和扩展 JFrame。
因为extend
我无法setVisible(false or true);
它对window完全没有影响它仍然是正确的。
我的代码:
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LibrarianMenu frame = new LibrarianMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LibrarianMenu() {
setTitle("Librarian");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 385, 230);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
.
.
. so on
我在这里尝试执行我的按钮:
btnLogout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
LibrarianMenu frame = new LibrarianMenu();
frame.setVisible(false);
}
});
有什么解决办法吗?
因为您是在 Runnable 内部创建框架,所以它的范围仅限于 runnable。尝试在 runnable 之外声明变量,然后在 runnable 内初始化它,如下所示:
private JPanel contentPane;
private LibrarianMenu frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new LibrarianMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
然后在不声明 LibrarianMenu 的新实例的情况下将可见设置为 false:
btnLogout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
发生这种情况是因为每次按下按钮都会创建该框架的一个新实例。这是您更新的代码:
static LibrarianMenu frame ;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new LibrarianMenu();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
注销按钮事件应该是这样的:
btnLogout.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});