I have a JFrame and two JPanels. First of them has "go on" button and the second one has "go back" button. When the program starts, the first panel is added to the frame. When I press "go on" button, I want to replace panels - the same with "go back".
public class Main extends JFrame {
private final int width = 320;
private final int height = 640;
private JPanel firstPanel;
private JPanel secondPanel;
private JButton goOn;
private JButton goBack;
public Main() {
firstPanel = new JPanel();
firstPanel.setBackground(Color.yellow);
firstPanel.setOpaque(true);
firstPanel.setPreferredSize(new Dimension(width, height));
goOn = new JButton("Go on");
goOn.setPreferredSize(new Dimension(width/2, height/2));
firstPanel.add(goOn);
secondPanel = new JPanel();
secondPanel.setBackground(Color.green);
secondPanel.setOpaque(true);
secondPanel.setPreferredSize(new Dimension(width, height));
goBack = new JButton("Go back");
goBack.setPreferredSize(new Dimension(width/2, height/2));
secondPanel.add(goBack);
showFirstPanel();
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
addButtonsListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Go on"))
showSecondPanel();
else
showFirstPanel();
}
});
}
public void showFirstPanel() {
remove(secondPanel);
add(firstPanel);
}
public void showSecondPanel() {
remove(firstPanel);
add(secondPanel);
}
public void addButtonsListener(ActionListener listener) {
goOn.addActionListener(listener);
goBack.addActionListener(listener);
}
public static void main (String[] args) {
new Main();
}
}
The problem is when I press the first button, the program hangs. Sometimes it works after 10-20 seconds. The same situation happens with "go back" button.