I've recently been developing software applications which have mostly been very basic, and I have reached a problem.
The application I am developing now has many different menus and screens, which I would like the JFrame to alternate between displaying upon clicks of a button.
I can see surprisingly little information on this given that this is a feature that most applications seem to implement, which makes me wonder if my approach is completely off, however is some example code illustrating this approach.
My question therefore is, a) what is the best way to go about achieving this, and b) what is wrong with my current code? The former question is the most important however.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Demo {
JFrame frame;
JButton nextButton = new JButton ("Next Screen");
public void setup() {
frame = new JFrame();
frame.setVisible(true);
frame.add(new PanelOne());
frame.pack();
}
public class PanelOne extends JPanel { {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JLabel("Label One"));
this.add(new JLabel("Label Two"));
this.add(new JLabel("Label Three"));
this.add(new JLabel("Label Four"));
this.add(new JLabel("Label Five"));
JButton button = new JButton("Next Screen");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
swapPanel();
}
});
this.add(button);
} }
public class PanelTwo extends JPanel {{
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.add(new JButton("Button One"));
this.add(new JButton("Button Two"));
this.add(new JButton("Button Three"));
this.add(new JButton("Button Four"));
this.add(new JButton("Button Five"));
}}
protected void swapPanel() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
frame.removeAll();
frame.add(new PanelTwo());
frame.invalidate();
frame.revalidate();
}
});
}
public static void main (String[] args) {
Demo demo = new Demo();
demo.setup();
}
}