Reputation: 235
I have a welcome (or menu) window (JFrame) with some buttons (JButton) for each possible action. Each of these should launch a new window and hide the welcome window. I know I can do it with 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!
Upvotes: 3
Views: 1399
Reputation: 3966
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. Hope it will help you ask if i am wrong somewhere or you are not able to understand.
Upvotes: 2