您好,我目前正在做一项作业,已完成 98%。我制作了 gui 程序,其中 jtextfield 设置为 0,但当你玩游戏时它会发生变化。
现在我创建了一个名为“NEW GAME”和“QUIT”的 j 按钮。单击“我的退出”按钮后效果很好。但我的新游戏按钮是我的问题。
我想要这样,当我单击按钮时,它会将分数设置为 0
public ShinyButtonsGUIProgramToShare(String tit, int x, int y) {
//The button GUI
ShinyButtonsGUIToShare sbg = new ShinyButtonsGUIToShare("NYI", 552, 552, new ShinyButtons());
sbg.setLocation(10, 10);
getContentPane().add(sbg);
//The score text and text box
JLabel jlb = new JLabel("Score: ");
jlb.setLocation(12, (y - 75));
jlb.setSize(45, 40);
getContentPane().add(jlb);
JTextField jtf = new JTextField("0");
jtf.setLocation(60, (y - 70));
jtf.setSize(150, 30);
jtf.setHorizontalAlignment(JTextField.RIGHT);
getContentPane().add(jtf);
JButton NewGame, Quit;
NewGame = new JButton("New Game");
Quit = new JButton("Quit");
Quit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.exit(0);
}});
NewGame.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JTextField jtf.setText("0");
}});
NewGame.setLocation((x - 220), (y - 70));
NewGame.setSize(100, 30);
getContentPane().add(NewGame);
Quit.setLocation((x - 110), (y - 70));
Quit.setSize(100, 30);
getContentPane().add(Quit);
setDefaultCloseOperation(EXIT_ON_CLOSE); // allow window to close
setSize(x, y);
setLayout(null);
setResizable(false);
}
public static void main(String[] args) {
ShinyButtonsGUIProgramToShare sbgp = new ShinyButtonsGUIProgramToShare("Shiny Buttons", 578, 634);
sbgp.setVisible(true);
}
}