I am creating a simple game in java.swing and I encountered a problem. Everything works exactly as I want except one button. I would like the "Restart" button to remove the jFrame board content and reload it, but I can not handle this problem. When I press the button nothing happens. Please help me.
PS. I know the code is amateurish but I'm just learning.
board declaration in Menu Class:
public static JPanel board = new Board();
Here's my Menu Constructor:
public Menu()
{
super("Tron");
setSize(1625, 925);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setResizable(false);
setLocation(new Point(10, 10));
Insets insets = new Insets(1, 1, 1, 1);
start = new JButton("Start");
start.setMargin(insets);
start.setFocusable(false);
start.setBounds(50, 50, 80, 30);
start.addActionListener(new Start());
difficulty = new JComboBox<String>(diffLevel);
difficulty.setFocusable(false);
difficulty.setBounds(200, 50, 80, 30);
//difficulty.addActionListener(new Difficulty());
restart = new JButton("Restart");
restart.setMargin(insets);
restart.setFocusable(false);
restart.setBounds(500, 50, 80, 30);
restart.addActionListener(new Restart());
exit = new JButton("Zakończ");
exit.setMargin(insets);
exit.setFocusable(false);
exit.setBounds(350, 50, 80, 30);
exit.addActionListener(new Exit());
board.setBounds(10, 87, 1600, 800);
add(start);
add(difficulty);
add(exit);
add(restart);
add(board);
// add(obraz);
setVisible(true);
}
and Restart ActionListener:
private class Restart implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
remove(board);
board = new Board();
board.setFocusable(true);
board.invalidate();
board.revalidate();
board.setVisible(true);
Board.timer.start();
}
}