Skip to main content

Multiple Menu Program

1 reply [Last post]
pisaia
Offline
Joined: 2010-04-03

Hi,

I want to create a program in Java, and I am using JFrames for my GUI. What the program does is that it has some buttons on a JFrame and each button does an event. One of this buttons has to lead in a new menu.
I have decided that the best way to do this is to create two JFrames, set the one to visible (i.e. setVisible(true)) and the other one invisible (i.e setVisible(false))
When I press on the button I want the first frame to become invisible and the second one visible so I will be able to view the new menu.

Is there any way to do this? If you want my code so far I can post it here, just let me know guys.

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
RovaGeekox
Offline
Joined: 2014-03-13

if i were you i'll use two jpanels, add it two to one JFrame that have a CardLayout, and switch the jpanels along the application

public class Main extends JFrame {

public Main() {

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel jpanel1 = new JPanel();
final JPanel jpanel2 = new JPanel();
setLayout(new CardLayout());
jpanel1.setBackground(Color.BLACK);
final JButton button = new JButton("swap");
final JButton button2 = new JButton("over");
button.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("swap")) {
jpanel1.setVisible(false);
jpanel2.setVisible(true);
}
}
});
button2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
if(arg0.getActionCommand().equals("over")) {

jpanel1.setVisible(true);
jpanel2.setVisible(false);
}
}
});
add(jpanel1);
add(jpanel2);
jpanel1.add(button);
jpanel2.add(button2);
setSize(500, 500);
setVisible(true);
}

public static void main(String[] args) {

new Main();
}
}

let's try to test this