我为基本的 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) {
}
}
}
基本上,我有 2 个 java 文件:Main.java 和 Game.java
Main.java 有上面的代码,可以完美执行,Game.Java 有 Jlabels 和 JFrame,但默认情况下它是不可见的。如何让 Main.java 识别来自 Game.java 的所有声明并在单击按钮时使其可见?
我在 Windows XP 上,使用 Eclipse。