[英]Hiding JFrame with anonymous ActionListener in a JButton
I have a welcome (or menu) window (JFrame) with some buttons (JButton) for each possible action. 我有一个欢迎(或菜单)窗口(JFrame),其中包含一些按钮(JButton),用于每个可能的操作。 Each of these should launch a new window and hide the welcome window.
其中每个都应该启动一个新窗口并隐藏欢迎窗口。 I know I can do it with
setVisible(false);
我知道我可以用
setVisible(false);
做到这一点setVisible(false);
. 。 But I can't make it work yet.
但我还不能让它发挥作用。
This is one example of code I have: 这是我的代码的一个例子:
_startBtn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
System.out.println("_startBtn pressed");
// Code to hide this JFrame and initialize another
}
My question is, how can I do it using a anonymous class like this one? 我的问题是,我怎么能使用像这样的匿名类呢?
Thanks in advance! 提前致谢!
I am posting an example for you i hope it will help you. 我正在为你发贴一个例子,我希望它会对你有所帮助。
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class windows_test {
JFrame login = null;
JFrame inner_frame = null;
public windows_test() {
login = new JFrame();
login.setBounds(10, 10, 300, 300);
login.setLayout(new BorderLayout());
JButton button = new JButton("Login");
login.add(button, BorderLayout.CENTER);
login.setVisible(true);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (inner_frame == null) {
inner_frame = new JFrame();
}
inner_frame.setLayout(new FlowLayout(FlowLayout.CENTER));
inner_frame.add(new JButton("inner frame"));
inner_frame.setVisible(true);
login.setVisible(false);
inner_frame.setBounds(10, 10, 300, 300);
}
});
}
}
I will recommend you to use jpanel instead of jframes but you have asked for frames so i have created it with them. 我建议你使用jpanel而不是jframes,但你已经要求框架,所以我用它们创建了它。 Hope it will help you ask if i am wrong somewhere or you are not able to understand.
希望它会帮助你问我某处是否错,或者你无法理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.