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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:02:31+00:00 2026-06-08T04:02:31+00:00

I wrote the code for a basic Java Swing window and now I want

  • 0

I wrote the code for a basic Java Swing window and now I want to run a game in another window. The window is basically temporarily until the user hits the button. This is the code for the first window:

    import javax.swing.*;

public class Main extends Game {
    public static int height = 300;
    public static int width = 200;
    public String x = "X", y = "Y", player1, player2;
    public String[] grid;

    public static void main(String args[]) {
/*--------------------------- DECLARATIONS ----------------------------*/
        JFrame sudwin = new JFrame("Tic tac toe");
        JLabel label1 = new JLabel("<html><b>Welcome to Tic Tac Toe!</b></html>");  
        JLabel label2 = new JLabel("Player 1:");
        JLabel label3 = new JLabel("Player 2:");
        JLabel label4 = new JLabel("<html>Enter your names in the boxes, then </br>" + "click the start button to begin!</html>");
        JLabel label5 = new JLabel("Version 0.1");
        JTextField np1 = new JTextField();
        JTextField np2 = new JTextField();
        JButton btstart = new JButton("Start");

        sudwin.getContentPane().add(label1);
        sudwin.getContentPane().add(label2);
        sudwin.getContentPane().add(label3);
        sudwin.getContentPane().add(label4);
        sudwin.getContentPane().add(np1);
        sudwin.getContentPane().add(np2);
        sudwin.getContentPane().add(btstart);
        sudwin.getContentPane().add(label5);
/*--------------------------- METHODS ---------------------------------*/
        sudwin.setSize(width, height);
        sudwin.setVisible(true);
        sudwin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        sudwin.setLocationRelativeTo(null);
        sudwin.getContentPane().setLayout(null);

        label1.setBounds(26, 11, 156, 14);
        label2.setBounds(10, 102, 100, 10);
        label3.setBounds(10, 144, 100, 10);
        label4.setBounds(10, 39, 172, 52);
        label5.setBounds(10, 241, 100, 14);
        np1.setBounds(10, 113, 111, 20);
        np2.setBounds(10, 156, 111, 20);
        btstart.setBounds(50, 202, 100, 28);

        btstart.addActionListener(new act1());
    }
/*--------------------------- EVENT HANDLERS ----------------------------*/
    static class act1 implements ActionListener {
        public void actionPerformed(ActionEvent e) {

        }
    }
}

Basically, I have 2 java files: Main.java and Game.java

Main.java has the code above, which executes perfectly and Game.Java has the Jlabels and the JFrame but it is, by default, invisible. How can I make Main.java recognise all declarations from Game.java and make it visible when clicking the button?

I’m on Windows XP, using Eclipse.

  • 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-08T04:02:32+00:00Added an answer on June 8, 2026 at 4:02 am

    You don’t want to have all of the fields of Game.java available to Main.java, but what you do want is a method that you can call, something like public void setGameVisible(). Then in your Main.java you will have an instance of Game.java, Game game = new Game() and then you can do game.setGameVisible() when the button is clicked. In this method you would have all of your logic for making the Game.java components visible.

    In general you do not want to make fields public. In the general idea of encapsulation you want to make fields from one class available to other classes through methods. E.g.

    public class Main
    {
      protected String gameName = "Super Awesome Game";
    
      public String getName()
      {
        return gameName;
      }
    }
    

    This way someone else is not able to change the String gameName, which they would be able to do if it were public. Keeping fields protected allows for a subclass of Main.java to still have access to those fields.

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

Sidebar

Related Questions

In basic Java, if I write code to solve a common problem and want
I wrote the basic code below and saved to a file called pdf.java. package
I'm developing a small game in Java and I have run into a problem
Wrote up a basic file handler for a Java Homework assignment, and when I
I'm currently writing a very basic Java game based on the idea of Theme
Ok, I'm trying to code a basic program that could help user to find
I have created a web system using Java Servlets and now want to make
I'm trying to use basic Java code in Scala to read from a file
I've started learning basic Java and wanted to rewrite a Game Map Generator that
Basic doubt...If QTP generates vbscript code as we record actions, can't we directly write

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.