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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:53:49+00:00 2026-05-28T03:53:49+00:00

So I am pretty new to java and programming (4 months) and I am

  • 0

So I am pretty new to java and programming (4 months) and I am trying to make a basic snake game with a menu. I am switching between each different component by adding and removing them on the frame. Here is the code:

public class screen extends Frame {
private static final long serialVersionUID = 1L;
private static screen f;
private static menu ex;
private static loadsettings settings;
public screen(String frameLabel, int width, int height) {
    setSize(width, height);
    setLocation(50,50);
    setResizable(true);
    setUndecorated(true);
    setVisible(true);  
}

public static void main(String[] args){
    settings = new loadsettings();      
    f = new screen("Game", settings.getWindowWidth(), settings.getWindowHeight());
    ex = new menu(f,settings); 
    f.add(ex);
    ex.createBufferStrategy(2);
    ex.requestFocus();
}
public static void new1Player(){
    f.setSize(400,400);
    f.removeAll();
    player1 ex = new player1(f,settings);
    f.add(ex);
    ex.createBufferStrategy(2);
    ex.requestFocus();
}
public static void new2Player(){
    f.setSize(600,600);
    f.removeAll();
    player2 ex = new player2(f,settings);
    f.add(ex);
    ex.createBufferStrategy(2);
    ex.requestFocus();
}
public static void newMenu(){
    f.setSize(400,400);
    f.removeAll();
    menu ex = new menu(f,settings);
    f.add(ex);
    ex.createBufferStrategy(2);
    ex.requestFocus();
}
}

The thing is, each component is a Canvas, so they are constantly being added and removed. I just want to know, is this bad coding and is there a better way to do this?
Thanks.

  • 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-28T03:53:50+00:00Added an answer on May 28, 2026 at 3:53 am

    Read about using the CardLayout.

    A CardLayout object is a layout manager for a container. It treats
    each component in the container as a card. Only one card is visible at
    a time, and the container acts as a stack of cards. The first
    component added to a CardLayout object is the visible component when
    the container is first displayed.

    You can use a Canvas if you like, but theres no reason you couldn’t just use Panels.

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

Sidebar

Related Questions

I'm pretty new to Perl but have been programming in java for several months
I'm pretty new to java programming and am looking to do basic data mappings.
I'm pretty new to Java ME and i'm trying to use Microlog to handle
I'm pretty new to Java, and I need to build up programming environments for
I am a pretty decent programmer in Java, however I am new to programming
I'm pretty new to java (and programming in general), and am having a tough
Pretty new to Java programming, but I'm having trouble getting this to run. I
I am pretty new to django but experienced in Python and java web programming
Im pretty new to Java Web Services, but I cant find a good explanation
I am pretty new to the whole Java and OSGi world and I have

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.