基本的な Java Swing ウィンドウのコードを書きましたが、別のウィンドウでゲームを実行したいと考えています。ウィンドウは基本的にユーザーがボタンを押すまでの一時的なものです。これは最初のウィンドウのコードです:
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) {
}
}
}
基本的に、Main.java と Game.java の 2 つの Java ファイルがあります。
Main.java には上記のコードがあり、完全に実行され、Game.Java には Jlabels と JFrame がありますが、デフォルトでは非表示になっています。Main.java が Game.java からのすべての宣言を認識し、ボタンをクリックしたときに表示されるようにするにはどうすればよいですか?
私はEclipseを使用してWindows XPを使用しています。