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 6873583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:05:13+00:00 2026-05-27T04:05:13+00:00

I am creating a Tetris clone as a personal project. In order to switch

  • 0

I am creating a Tetris clone as a personal project. In order to switch between the menus, I am using a CardLayout, but I have come across a problem. When I switch from one menu to the other, the focus does not transfer to the other JPanel. How can I fix this?

Here is my JFrame:

package com.cgp.tetris;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class TetrisFrame extends JFrame {
    private static final long serialVersionUID = 1L;
    TetrisMenu tm = new TetrisMenu();
    static SinglePlayerMenu spm = new SinglePlayerMenu();
    public static CardLayout cl = new CardLayout();
    static JPanel parentComponent = new JPanel(cl);

    public static void main(String[] args) {
        new TetrisFrame();
    }

    public TetrisFrame() {
        setTitle("Tetris");
        setSize(new Dimension(646, 604));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((d.width / 2) - 323, (d.height / 2) - 302);
        parentComponent.add(tm, "tm");
        parentComponent.add(spm, "spm");
        cl.show(parentComponent, "tm");
        add(parentComponent);
    }

    public static void replace() {
        cl.show(parentComponent, "spm");
        spm.startClip();
    }
}

Here is my first JPanel/menu:

package com.cgp.tetris;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class TetrisMenu extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private BufferedImage titletop, titlebottom, selector;
    private boolean left = true, right = false;
    private AudioInputStream themestream;
    private Clip clip;

    public TetrisMenu() {
        super();
    }

    public void run() {
        requestFocus(true);
        loadImages();
        sound();
        bind();
        while (true) {
            repaint();
        }
    }

    private void sound() {
        File theme = new File("res/theme.wav");
        try {
            themestream = AudioSystem.getAudioInputStream(theme);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        DataLine.Info info = new DataLine.Info(Clip.class, themestream.getFormat());
        clip = null;
        try {
            clip = (Clip) AudioSystem.getLine(info);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        try {
            clip.open(themestream);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        clip.start();
    }

    private void bind() {
        InputMap im = getInputMap();
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke("LEFT"), "left");
        am.put("left", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                left = true;
                right = false;
            }
        });

        im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
        am.put("right", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                System.out.println("2");
                right = true;
                left = false;
            }
        });

        im.put(KeyStroke.getKeyStroke("SPACE"), "space");
        am.put("space", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (left) {
                    TetrisFrame.replace();
                    clip.stop();
                    thread.stop();a
                }
            }
        });
    }

    private void loadImages() {
        try {
            titletop = ImageIO.read(new File("res/tetrispic.png"));
            titlebottom = ImageIO.read(new File("res/titlebottom.png"));
            selector = ImageIO.read(new File("res/selector.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(titletop, 0, 0, 640, 440, null);
        g.drawImage(titlebottom, 0, 440, 640, 136, null);
        if (left) {
            g.drawImage(selector, 36, 452, 16, 24, null);
        } else if (right) {
            g.drawImage(selector, 356, 452, 16, 24, null);
        }


    }
}

Here is my second JPanel/menu:

package com.cgp.tetris;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class SinglePlayerMenu extends JPanel implements Runnable {
    private static final long serialVersionUID = 1L;
    private Thread thread;
    private AudioInputStream themestream;
    private Clip clip;
    private BufferedImage base, bi1, bi2, bi3, bi4, bi1s, bi2s, bi3s, bi4s;
    private boolean typeselected = false, b1 = true, b2 = false, b3 = true, b4 = false, b5 = false, b6 = false;

    public SinglePlayerMenu() {
        super();
    }

    public void paint(Graphics g) {
        super.paint(g);

        g.drawImage(base, 0, 0, 640, 576, null);

        if (b1) {
            g.drawImage(bi1s, 80, 156, 240, 40, null);
        } else if (!b1) {
            g.drawImage(bi1, 80, 156, 240, 40, null);
        }
        if (b2) {
            g.drawImage(bi2s, 324, 156, 236, 40, null);
        } else if (!b2) {
            g.drawImage(bi2, 324, 156, 236, 40, null);
        }
    }

    public void run() {
        sound();
        loadImages();
        bind();
        while (true) {
            repaint();
        }
    }

    private void bind() {
        InputMap im = getInputMap();
        ActionMap am = getActionMap();

        im.put(KeyStroke.getKeyStroke("LEFT"), "left");
        am.put("left", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    b1 = true;
                    b2 = false;
                }
            }
        });

        im.put(KeyStroke.getKeyStroke("RIGHT"), "right");
        am.put("right", new AbstractAction() {
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                if (!typeselected) {
                    b2 = true;
                    b1 = false;
                }
            }
        });
    }

    private void loadImages() {
        try {
            base = ImageIO.read(new File("res/base.png"));
            bi1 = ImageIO.read(new File("res/1.png"));
            bi2 = ImageIO.read(new File("res/2.png"));
            bi3 = ImageIO.read(new File("res/3.png"));
            bi4 = ImageIO.read(new File("res/4.png"));
            bi1s = ImageIO.read(new File("res/1s.png"));
            bi2s = ImageIO.read(new File("res/2s.png"));
            bi3s = ImageIO.read(new File("res/3s.png"));
            bi4s = ImageIO.read(new File("res/4s.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void sound() {
        File theme = new File("res/playtheme.wav");
        try {
            themestream = AudioSystem.getAudioInputStream(theme);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        DataLine.Info info = new DataLine.Info(Clip.class, themestream.getFormat());
        clip = null;
        try {
            clip = (Clip) AudioSystem.getLine(info);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
        try {
            clip.open(themestream);
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addNotify() {
        super.addNotify();
        thread = new Thread(this);
        thread.start();
    }

    public void startClip() {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
}

Thanks in advance!

  • 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-05-27T04:05:14+00:00Added an answer on May 27, 2026 at 4:05 am

    Add a ComponentListener so you can request the window to put it in focus. Replace your existing constructor with this:

    public SinglePlayerMenu() {
        super();
        this.addComponentListener( new ComponentAdapter() {
            @Override
            public void componentShown( ComponentEvent e ) {
                SinglePlayerMenu.this.requestFocusInWindow();
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a Tetris clone in C++, and I have an enum GameProperty ,
Creating the closure is easy but using it is confusing for me. Here is
Creating an ATL project in MSVC seems to create not one but two projects;
Creating a website using simple html(not html5) and css but got stock in mouseover
Creating a JApplet I have 2 Text Fields, a button and a Text Area.
When creating a web application, and lets say you have a User object denoting
Creating an installer for possible remote systems so that if they do not have
Creating a table without tbody using javascript createElement/appendChild will not add tbody in Firebug
Creating sprite sheets aka texture atlases rather than using many hundres of individual images
Creating extensions got much easier with Vs2010, but this seems not to be the

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.