Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8733965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:45:06+00:00 2026-06-13T09:45:06+00:00

I am writing a tetris game. When the application starts Jlabel with button Play

  • 0

I am writing a tetris game. When the application starts Jlabel with button “Play” opens. How do I switch to a different label (Board) within the existing Jframe?

Like this it opens the game directly.. But first I would want to use the ButtonPage class to show some welcome screen with a button instead and then call the game.

    public class Tetris extends JFrame {

    public Tetris(){

        // JFrame Properties
        setSize(198, 409);  
        setResizable(false);
        setTitle("Tetris");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

//        ButtonPage buttons = new ButtonPage();
//        add(buttons);
//        buttons.setOpaque(true);

        Board board = new Board(this);
        add(board);
        board.start();

    } // end of constructor 
    public static void main(String[] args){

        Tetris game = new Tetris();
        game.setLocationRelativeTo(null);        
        game.setVisible(true);        
        game.setLayout(null);
    } // end of main

} // end of class

Here is the ButtonPage class.

public class ButtonPage extends JPanel implements ActionListener{

    JButton buttonPLAY = new JButton();
    JLabel backgroundImage = new JLabel();

    public ButtonPage(){  

        setLayout(null);

        ImageIcon buttonIcon = new ImageIcon(getClass().getResource("PlayButton.png"));
        ImageIcon buttonIconHover = new ImageIcon(getClass().getResource("PlayButtonHover.png"));
        ImageIcon buttonIconClicked = new ImageIcon(getClass().getResource("PlayButtonClicked.png"));
        int buttonHeight = buttonIcon.getIconHeight();
        int buttonWidth = buttonIcon.getIconWidth();


        buttonPLAY.addActionListener(this);
        buttonPLAY.setActionCommand("Play"); 
        buttonPLAY.setIcon(buttonIcon);
        buttonPLAY.setRolloverIcon(buttonIconHover);
        buttonPLAY.setPressedIcon(buttonIconClicked);
        buttonPLAY.setBorderPainted(false);        

        add(buttonPLAY);


        Dimension size2 = getSize();         
        Dimension size = buttonPLAY.getPreferredSize();
        buttonPLAY.setBounds((192 - buttonWidth)/2, 100 ,buttonWidth, buttonHeight);


    }// end of constructor

    @Override
    public void actionPerformed(ActionEvent e) {
        if ("Play".equals(e.getActionCommand())) {

        Tetris game = new Tetris();        
        // opens the window in the middle of the screen
        game.setLocationRelativeTo(null);
        // set the tetris window visible, unless its true - its invisible DUH!
        game.setVisible(true);        
        game.setLayout(null);

        }
    } // end of actionPerformed

}// end of class

Using the actionPerformed method I can open the game in a new Frame, but I have no idea how to switch the Panels.

Thanks in advance for any tips!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:45:08+00:00Added an answer on June 13, 2026 at 9:45 am

    Tetris is intanciated from main, the following line from actionPerformed():

    Tetris game = new Tetris();
    

    instanciates a second Tetris, it’s really what’s you want?

    To add several panels to a frame, only one visible at a time, use CardLayout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Tetris-based game, and I am using a GridLayout to display
I'm writing a tetris game using C++ and MFC. I have a timer and
I'm writing tetris right now, I have a method that drops figures one by
I am writing a Tetris clone in WPF. If I hold down the right
Writing a .NET DLL how do I find Application.ProductName ? EDIT: Obviously, importing Windows.Forms
Writing my first C# application...never touched the language before and not much of a
Writing a small command line tool, it would be nice to output in different
I am writing Tetris in JavaScript as practice, and i am somewhat confused by
You are writing a Tetris program in Java. How would you set up your
Writing Junit Tests for my spring application. Because I am new at this I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.