-1

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();

    }
}
x3non
  • 3
  • 4
  • 1
    `remove(board);` ... did you try adding it again? – MadProgrammer Jun 02 '17 at 06:46
  • `add(board);` - tried in ActionListener but still nothing happens – x3non Jun 02 '17 at 06:50
  • 1
    1) `setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) .. – Andrew Thompson Jun 02 '17 at 06:50
  • 1
    .. 3) Use a [`CardLayout`](http://download.oracle.com/javase/8/docs/api/java/awt/CardLayout.html) as shown in [this answer](http://stackoverflow.com/a/5786005/418556). But in this case, it would be a better design to reset the game model, and update the components to reflect the model. – Andrew Thompson Jun 02 '17 at 06:51
  • 1
    `invalidate` and `revalidate` won't have any effect because 1) the `board` is not on the screen and 2) you're using `null` layouts. Have a look at the code you used in `Menu`, basically this is the same thing you need to to do in your `ActionListener` (for the `board`) and repainting the parent container probably won't hurt – MadProgrammer Jun 02 '17 at 06:59
  • 2
    The button know has focus, welcome to the wonderful world of "why we don't recommend `KeyListener`" - See [How to use key bindings](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for a solution – MadProgrammer Jun 02 '17 at 07:14

1 Answers1

0

@MadProgrammer thanks for help.this solve the problem:

private class Restart implements ActionListener
{
    public void actionPerformed(ActionEvent e)
    {
        Board.timer.stop();
        Board.inGame = true;
        remove(board);
        board = new Board();
        board.setBounds(10, 87, 1600, 800);
        board.setFocusable(true);
        add(board);
        repaint();
        Board.timer.restart();
    }
}

but probably i'm loosing focus on board JFrame cause after restart i can't steer by keyboard, any help?

x3non
  • 3
  • 4
  • 1
    In its present form, this is not an answer; it appears to be a request for additional help. Please click the [edit](https://stackoverflow.com/posts/44323060/edit) button to include this [comment](https://stackoverflow.com/questions/44322456/button-wont-restart-jframe#comment75651866_44323060). – trashgod Jun 02 '17 at 08:05