Java 我的新游戏JButton不工作了?
嗨,我目前正在做一项作业,我已经完成了98%。我制作了一个gui程序,其中jtextfield设置为0,但当您玩游戏时,它会改变 现在我创建了一个名为“新游戏”和“退出”的j按钮。单击“退出”按钮时,我的退出按钮工作正常。但我的新游戏按钮是我的问题 我想要这样,当我点击按钮时,它会将分数设置为0Java 我的新游戏JButton不工作了?,java,swing,Java,Swing,嗨,我目前正在做一项作业,我已经完成了98%。我制作了一个gui程序,其中jtextfield设置为0,但当您玩游戏时,它会改变 现在我创建了一个名为“新游戏”和“退出”的j按钮。单击“退出”按钮时,我的退出按钮工作正常。但我的新游戏按钮是我的问题 我想要这样,当我点击按钮时,它会将分数设置为0 public ShinyButtonsGUIProgramToShare(String tit, int x, int y) { //The button GUI Sh
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);
}
}
你有一个背景问题
jtf
的实例不能在NewGame
的ActionListener
的上下文中引用
您需要创建jtf
和实例变量,以便可以从ShinyButtonsGUIProgramToShare
对象实例中的任何位置访问它
看看这个代码,
JTextField jtf.setText(“0”)代码>,甚至编译?如果它没有编译,那么你还没有完成98%。在过去的两天里,我们看到了很多这些闪亮的按钮应用类型的问题。这是怎么回事?我看到setSize
和setLocation
,我的一小部分就死了…@MadProgrammer:每当一个程序员写setLayout(null)
,一只小狗就会死在某个地方。@Solace我会更担心这会创建一个递归循环并导致程序崩溃,出现堆栈溢出错误,但我知道什么:我没有正确使用动作列表器。我注意到那是我的错误,谢谢。