为什么这不能正常工作?我似乎找不到问题
它的假设是每次单击按钮时,文本显示都会增加一
public class ClickerGame extends javax.swing.JFrame {
public ClickerGame() {
initComponents();
}
//declare
int clicks;
String clicksout = "" + clicks;
//Swing GUI netbeans code is here, removed because it is irrelevant
//click increments number by 1
private void clickActionPerformed(java.awt.event.ActionEvent evt) {
clicks++;
clickercounter.setText(clicksout);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new ClickerGame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton click;
private javax.swing.JTextField clickercounter;
// End of variables declaration
}
最佳答案
您只需设置一次clicksout
:
String clicksout = "" + clicks;
如果
clicks
更改,则该值不会动态重置。如果您从不更改它,那么在这里总会得到相同的结果:clickercounter.setText(clicksout);
尝试以下方法:
clickercounter.setText("" + clicks);
您不需要
clicksout
作为单独的变量。关于java - 无法制作基本的Java程序来计算您的点击次数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20023893/