Reputation: 75
I am trying to have one class open by default, and then when I click a Swing button I would like to have the other form run. I used to use Visual basic and it was so simple:
Form2.Show()
and
Form2.Hide()
But in Java, I can't find anything like that. Is there any way to do this easily?
Thanks!
Upvotes: 1
Views: 684
Reputation: 7716
Study the following to see how pressing a button causes a new frame to display.
public static void main(String args[]) throws Exception {
new JFrame(){{
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300,300);
setLocation(100,100);
setVisible(true);
setLayout(new BorderLayout());
add(new JButton("PressToOpenNewFrame"){{
addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
new JFrame(){{
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300,300);
setLocation(200,200);
setVisible(true);
setLayout(new BorderLayout());
add(new JButton("PressMeToBeep"){{
addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Toolkit.getDefaultToolkit().beep();
}});
}}, BorderLayout.SOUTH);
}};
}});
}}, BorderLayout.SOUTH);
}};
}
Upvotes: 2