0

I've been working on a version of a Tetris game and I tried to make a start page. I figured working with 2 panels will do fine but once I press the button the game doesn't run. Here is a piece of the code, where I implemented the panels. Any ideas what I did wrong and how I should have done it?

public class Tetris extends JFrame 
    {

        JLabel statusbar;
        private JPanel panel1=new JPanel();
        private JPanel panel2=new JPanel();
        public Tetris() 
        {

            setResizable(false);
            setSize(200, 400);
            setTitle("Tetris");
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            //setLayout(new FlowLayout());

            statusbar = new JLabel(" 0");
            JButton startButton = new JButton("START");

            panel1.add(startButton);

            panel2.add(statusbar, BorderLayout.SOUTH);
            Board board = new Board(this);
            board.addKeyListener(new TAdapter(board));
            panel2.add(board);

            startButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent arg0) {

                    add(panel2);
                    panel1.setVisible(false);

                }
            });

            add(panel1);
            //board.start();

       }
Lorena Sfăt
  • 215
  • 2
  • 7
  • 18

1 Answers1

0

Try to add 'revalidate' to your event handler. It should help, I guess.

startButton.addActionListener(new ActionListener()
{
    public void actionPerformed(ActionEvent arg0) {

        add(panel2);
        panel1.setVisible(false);
        revalidate();

 }

});

By the way, if you are not forced to use Swing, I would advice you to use a modern JavaFX. Swing is not being developed by Oracle anymore.

Michal
  • 550
  • 7
  • 14