我试图让一旦按下播放按钮,GameUI 类中的startingGame 方法将被调用,该方法将删除实际框架内的所有内容,将其完全留空,以便我可以继续构建一个新框架。 我的目标是创建一个 connect 4 游戏,因此在类 UI 下创建的第一个框架将是起始页面,而一旦按下播放按钮,就会在同一窗口下出现一个包含游戏的新框架。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class UI {
JFrame frame = new JFrame();
JButton play = new JButton("Play");
JLabel inputUsername = new JLabel("Username", JLabel.RIGHT);
JTextField username = new JTextField(13);
private String Player1;
public UI() {
//Load the background Image.
ImageIcon imgBackground = new ImageIcon(this.getClass().getResource("/img/connect4_4.png"));
//Create the frame.
JFrame frame = new JFrame("Connect 4");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Setting a JLabel which contains an image to the content pane of the frame,
//So that the image is displayed as a background.
frame.setContentPane(new JLabel((imgBackground)));
//Any components added to the frame will be arranged according to GridBadLayout manager.
frame.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
//sets padding botton to 20 within the elements of the grid.
gbc.insets = new Insets(0, 0, 20, 0);
//make the play Button bigger.
play.setPreferredSize(new Dimension(200, 75));
//Add it to the frame.
play.setFont(new Font("Britannic Bold", Font.PLAIN, 30));
Color myColor = Color.decode("#CDCDCD");
play.setBackground(myColor);
play.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Retrieve the text from the JTextField and save it to the variable
Player1 = username.getText();
System.out.println("Username entered: " + Player1);
GameUI startGame = new GameUI();
startGame.startingGame();
}
});
frame.add(play, gbc);
//readjusts the padding of the elemts inside the grid.
gbc.insets = new Insets(0, 0, 2, 0);
inputUsername.setFont(new Font("Britannic Bold", Font.PLAIN, 20));
frame.add(username, gbc);
username.setFont(new Font("Corbel", Font.PLAIN, 15));
username.setHorizontalAlignment(JTextField.CENTER);
frame.add(inputUsername, gbc);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class GameUI extends UI {
public void startingGame() {
frame.getContentPane().removeAll();
frame.setVisible(true);
}
}
在您的代码中,
startingGame
类中的 GameUI
方法尝试从 UI 类中清除框架。但是,JFrame frame
存在范围问题,导致其无法按预期运行。
在
UI
类的构造函数中,您声明了一个局部变量 JFrame frame
,它隐藏了类成员 JFrame frame
。
这是导致问题的行:
JFrame frame = new JFrame("Connect 4");
更改为:
frame = new JFrame("Connect 4");
这样,您就可以初始化类成员
frame
,而不是创建局部变量,并且 GameUI
类应该能够访问和操作相同的 frame
。
这是您的
UI
课程的更正版本:
public class UI {
JFrame frame = new JFrame();
JButton play = new JButton("Play");
JLabel inputUsername = new JLabel("Username", JLabel.RIGHT);
JTextField username = new JTextField(13);
private String Player1;
public UI() {
// ... (no change here)
frame = new JFrame("Connect 4"); // Notice the change here
// ... (rest of the code remains same)
}
// ...
}
还有你的
GameUI
类,看起来不错:
class GameUI extends UI {
public void startingGame() {
frame.getContentPane().removeAll();
frame.setVisible(true);
}
}
现在,当您调用
startingGame()
时,它应该按预期工作,从 frame
中删除所有组件并使其再次可见。然后,您可以继续为 Connect 4 游戏添加新组件。