说明:嗨,我正在创建我的答题器游戏,但是我找不到为什么我的button1.setEnabled(false);。代码不会链接到我的JButton button1 = new JButton(“ Hacker Level:”); 。我正在使用两个按钮。第一个按钮启动计时器并计算点击次数/空格键的按下次数。一旦10秒钟用完,该按钮将自行禁用。第二个按钮重置点击次数并启用第一个按钮。
问题:因为我使用了两个不同的按钮,所以我必须使用JButton,它不会使其链接到button1.setEnabled(false);。码。如何链接?
当计时器用尽时,将button1设置为禁用屏幕截图。button1屏幕截图这是问题所在的代码部分:
public TestPane() {
new GridLayout(0, 1); // 0 rows, 1 column
timer = new Timer(10, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (startTime < 0) {
startTime = System.currentTimeMillis();
}
long now = System.currentTimeMillis();
long clockTime = now - startTime;
if (clockTime >= duration) {
clockTime = duration;
timer.stop();
**button1.setEnabled(false);**<<<<<<<<<<<<<<<<<<<<
}
SimpleDateFormat df = new SimpleDateFormat("mm:ss.SSS");
label.setText(df.format(duration - clockTime));
}
});
timer.setInitialDelay(0);
// \/ label wrapper \/ TITLE
label = new JLabel("Hacker GUI v1", JLabel.CENTER); // label text
label.setFont(new Font("Roboto", Font.PLAIN, 20)); // font size
add(label); // add the label
// /\ label wrapper /\
// \/ label wrapper \/ DESCRIPTION
label = new JLabel("To hack, click on the button or press space while on the window.", JLabel.CENTER); // text label
label.setFont(new Font("Roboto", Font.PLAIN, 15)); // font size
label.setHorizontalTextPosition(JLabel.CENTER);
add(label); // add the label
// /\ label wrapper /\
// \/ button wrapper \/ BUTTON PLAY
Dimension d = new Dimension(215, 30); // first number is length (left to right), second number is heigh (top to bottom).
**JButton button1 = new JButton("Hacker Level: "); // button text**<<<<<<<<<<<<
button1.setPreferredSize(d); // size of button relitive to dimension d
button1.setFont(new Font("Roboto", Font.PLAIN, 15)); //font size of button
button1.addActionListener( new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
count++; //count in positive integers
button1.setText("Hacker Level: " + count); //text change when action preformed
if (!timer.isRunning()) {
startTime = -1;
timer.start();
}
}
});
add(button); //add the button
// /\ button wrapper /\
// \/ button wrapper \/ BUTTON RESET
Dimension a = new Dimension(215, 30); // first number is length (left to right), second number is heigh (top to bottom).
JButton button2 = new JButton("Reset Score"); // button text
button2.setPreferredSize(a); // size of button relitive to dimension d
button2.setFont(new Font("Roboto", Font.PLAIN, 15)); //font size of button
button2.addActionListener( new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
count++; //count in positive integers
button1.setText("Hacker Level: " + count); //text change when action preformed
button1.setEnabled(true);
}
});
add(button); //add the button
// /\ button wrapper /\
JButton button1 = new JButton("Hacker Level: ");
您正在定义一个局部变量。局部变量不能在其他方法中使用。
您需要将“ button1”定义为实例变量(定义标签的方式相同):
button1 = new JButton("Hacker Level: ");
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句