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.
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 dogame.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.
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
protectedallows for a subclass of Main.java to still have access to those fields.