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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:12:49+00:00 2026-06-10T01:12:49+00:00

I am making a simple game using a JFrame . I have made a

  • 0

I am making a simple game using a JFrame. I have made a simple “Start” screen which basically consists of a String and a JButton. I am picking up the button click with the actionPerformed(ActionEvent e) method. I don’t know how to change the cards using a button click. This may seem like a simple problem to solve, but the twist comes with this: My main JFrame, my StartScreen and my JPanel where the game takes place are all in separate files. My main file, Virus.java, is where I create the JFrame. My file VirusGamePanel.java is where the game takes place. My file StartScreen.java is the screen with the button. I want to change ‘cards’ to the game screen when the player clicks the button. How can I do this?
My StartScreen.java file:

package virus;

import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.CardLayout;




public class StartScreen extends JPanel implements ActionListener{
    private static final long serialVersionUID = 1L;
    JButton start = new JButton("Start");
    public StartScreen(){
        start.addActionListener(this);
        start.setBounds(new Rectangle(400,300,100,30));
        this.add(start);
    }
    public void paint(Graphics g){
        super.paint(g);
        g.setFont(new Font("Impact",Font.BOLD,72));
        g.setColor(Color.MAGENTA);
        g.drawString("Virus",275,300);
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==start)
        {
            //what to do here?
        }
    }
}

My Virus.java file:

package virus;

import javax.swing.*;
import java.awt.CardLayout;
import virus.StartScreen;

public class Virus extends JFrame{
    private static final long serialVersionUID =1L;
    JFrame jf = new JFrame("Virus");
    static JPanel thegame = new JPanel(new CardLayout());
    JPanel game = new VirusGamePanel();
    JPanel start = new StartScreen();

    public Virus(){
        jf.setResizable(false);
        jf.setSize(600,600);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jf.setVisible(true);
        jf.add(thegame);
        thegame.add(start);
        thegame.add(game);

    }

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

    }

}
  • 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-10T01:12:51+00:00Added an answer on June 10, 2026 at 1:12 am

    You simply have to right this in your actionPerformed(...) method :

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource()==start)
        {
            //what to do here?
            CardLayout cardLayout = (CardLayout) Virus.thegame.getLayout();
            cardLayout.next(Virus.thegame);
        }
    }
    

    As very much pointed out by @kleopatra (THE EMPRESS) herself, don’t override paint() instead do your painting stuff inside paintComponent(Graphics g) method of any JPanel/JComponent. Moreover, first add the components to your JFrame, once it’s size is realized, then only set it to Visible, not before that. Instead of setting sizes for the JFrame simply override the JPanel‘s method getPreferredSize(), make it return some valid Dimension Object.

    Do watch this sequence, as you write your code the next time :

    public Virus(){
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setResizable(false);                               
        thegame.add(start);
        thegame.add(game);
        jf.add(thegame);        
        jf.pack();
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
    }
    

    Here is your full code :

    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.event.ActionListener;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Font;
    import java.awt.Color;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    import java.awt.Rectangle;
    import java.awt.event.ActionEvent;
    import java.awt.CardLayout;
    
    public class Virus extends JFrame{
        private static final long serialVersionUID =1L;
        JFrame jf = new JFrame("Virus");
        static JPanel thegame = new JPanel(new CardLayout());
        JPanel game = new VirusGamePanel();
        JPanel start = new StartScreen();
    
        public Virus(){
            jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            jf.setResizable(false);                               
            thegame.add(start);
            thegame.add(game);
            jf.add(thegame);        
            jf.pack();
            jf.setLocationRelativeTo(null);
            jf.setVisible(true);
        }
    
        public static void main(String[] args) {
            new Virus();
    
        }
    
    }
    
    class StartScreen extends JPanel implements ActionListener{
        private static final long serialVersionUID = 1L;
        JButton start = new JButton("Start");
        public StartScreen(){
            start.addActionListener(this);
            start.setBounds(new Rectangle(400,300,100,30));
            this.add(start);
        }
    
        @Override
        protected void paintComponent(Graphics g){
            super.paintComponent(g);
            g.setFont(new Font("Impact",Font.BOLD,72));
            g.setColor(Color.MAGENTA);
            g.drawString("Virus",275,300);
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(600, 600));
        }
    
        public void actionPerformed(ActionEvent e)
        {
            if(e.getSource()==start)
            {
                //what to do here?
                CardLayout cardLayout = (CardLayout) Virus.thegame.getLayout();
                cardLayout.next(Virus.thegame);
            }
        }
    }
    
    class VirusGamePanel extends JPanel
    {
        public VirusGamePanel()
        {
            JLabel label = new JLabel("I am ON", JLabel.CENTER);
            add(label);
        }
    
        @Override
        public Dimension getPreferredSize()
        {
            return (new Dimension(600, 600));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple game using the Windows Phone Silverlight and XNA Application type
I'm making a simple game in Javascript, in which when an object collides with
I am making a simple multiplayer economic game in pygame. It consists of turns
I have a somewhat simple game that I'm making and now that I'm testing
I'm making a simple 2d game for the android platform, which works perfectly from
I am making a simple 2D game in java... I am using java's Graphics2D
I'm making a Flash game which will be up on Facebook, I'm using Python/Django
So, I'm making a simple game using DirectX 9 and C++. I looked at
I'm making a simple game in Java using swing and am having problems with
Ok so I am experimenting with making a simple game. I have a struct

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.