显示在 java 中使用按钮之前必须等待多少秒的计时器
timer that shows how many seconds you have to wait before using a button in java
我正在尝试创建一个类似于《英雄联盟》的游戏来练习,我想让它在您单击一个按钮时按钮禁用 5 秒,同时按钮文本变为一个计时器,说明如何在再次使用该按钮之前,您必须等待很多秒。我成功地使按钮禁用了 5 秒,但现在我卡在了计时器部分。
我尝试创建一个变量,然后将其除以 1000(我必须将其除以 1000,因为我还将此变量用于禁用按钮的其他计时器)但我收到一条错误消息:
The method setText(String) in the type AbstractButton is not applicable for the arguments (int)
这是我上面所说的代码:
ImageIcon qAbilityIcon = new ImageIcon( getClass().getResource("images/Jaws_of_the_Beast.png"));
JButton qAbilityBtn = new JButton("Q");
// creates a timer that disables the button for 5 seconds
int qCDTime =(int)5000;
Timer qCooldown = new Timer(qCDTime, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
qAbilityBtn.setEnabled(true);
qAbilityBtn.setText("Q");
}
});
qCooldown.setRepeats(false);
JLabel qAbility = new JLabel();
qAbility.setIcon(qAbilityIcon);
qAbility.setBounds(90, 350, 64, 64);
qAbilityBtn.setBounds(90, 315, 64, 35);
qAbilityBtn.setOpaque(false);
qAbilityBtn.setContentAreaFilled(false);
qAbilityBtn.setBorderPainted(false);
qAbilityBtn.setFocusable(false);
qAbilityBtn.addActionListener(e -> {
int minQDmg = 25;
int maxQDmg = 75;
int qDmg = (int)(Math.random() * (maxQDmg - minQDmg + 1) + minQDmg);
abilityLabel.setText("You dealt " + qDmg + " damage!");
abilityLabel.setBounds(170, 10, 200, 50);
qAbilityBtn.setEnabled(false);
// here im getting the error
qAbilityBtn.setText(qCDTime / 1000);
qCooldown.start();
});
我知道这可能行不通,但有人可以告诉我为什么会出现该错误以及如何修复它。
此外,如果这不起作用(这很可能不起作用),我该如何制作计时器来显示您必须等待的秒数。
我也 google 做了这个,但我没有找到我需要的东西,或者至少我不知道如何在我的代码中实现它。
This 是我的主文件。
This 是我的游戏文件
P.S:我为我糟糕的英语和糟糕的解释能力道歉!!!
尝试了解错误告诉您的内容。您将 int
类型的参数提供给需要 String
类型参数的函数。为了解决错误,您可以替换此行:
qAbilityBtn.setText(qCDTime / 1000);
有了这个:
qAbilityBtn.setText(String.valueOf(qCDTime / 1000));
这将解决错误,但我很确定代码不会执行您希望它执行的操作,因为按钮上只会显示 qCDTime / 1000
的当前值,它不会已更新。
可能有很多方法可以做到这一点,但这是我会做的。下面的大部分内容是关于如何布局 GUI 的典型样板。
- 使用 Swing 定时器来控制事件。
- 按钮和计时器共享侦听器并使用
actionCommand
确定哪个组件触发了事件。
- 默认倒计时时间为 5 秒。定时器每秒发出一个事件。
- 按钮显示剩余秒数。
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ButtonTimer extends JPanel {
JFrame f = new JFrame("Button Timer Demo");
JButton b = new JButton("Click Me!");
MyActionListener mal = new MyActionListener();
Timer t = new Timer(0, mal);
public ButtonTimer() {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
add(b);
b.addActionListener(mal);
b.setPreferredSize(new Dimension(100, 40));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
t.setDelay(1000);
t.setActionCommand("timer");
}
public Dimension getPreferredSize() {
return new Dimension(200, 50);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ButtonTimer());
}
public class MyActionListener implements ActionListener {
public int seconds = 0;
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("timer")) {
if (seconds == 0) {
t.stop();
b.setText("Click Me!");
} else {
b.setText(seconds + "");
seconds--;
}
} else {
// must be button
if (seconds == 0) {
// only do button action if seconds == 0
seconds = 5;
// start timer
t.start();
}
}
}
}
}
我正在尝试创建一个类似于《英雄联盟》的游戏来练习,我想让它在您单击一个按钮时按钮禁用 5 秒,同时按钮文本变为一个计时器,说明如何在再次使用该按钮之前,您必须等待很多秒。我成功地使按钮禁用了 5 秒,但现在我卡在了计时器部分。
我尝试创建一个变量,然后将其除以 1000(我必须将其除以 1000,因为我还将此变量用于禁用按钮的其他计时器)但我收到一条错误消息:
The method setText(String) in the type AbstractButton is not applicable for the arguments (int)
这是我上面所说的代码:
ImageIcon qAbilityIcon = new ImageIcon( getClass().getResource("images/Jaws_of_the_Beast.png"));
JButton qAbilityBtn = new JButton("Q");
// creates a timer that disables the button for 5 seconds
int qCDTime =(int)5000;
Timer qCooldown = new Timer(qCDTime, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
qAbilityBtn.setEnabled(true);
qAbilityBtn.setText("Q");
}
});
qCooldown.setRepeats(false);
JLabel qAbility = new JLabel();
qAbility.setIcon(qAbilityIcon);
qAbility.setBounds(90, 350, 64, 64);
qAbilityBtn.setBounds(90, 315, 64, 35);
qAbilityBtn.setOpaque(false);
qAbilityBtn.setContentAreaFilled(false);
qAbilityBtn.setBorderPainted(false);
qAbilityBtn.setFocusable(false);
qAbilityBtn.addActionListener(e -> {
int minQDmg = 25;
int maxQDmg = 75;
int qDmg = (int)(Math.random() * (maxQDmg - minQDmg + 1) + minQDmg);
abilityLabel.setText("You dealt " + qDmg + " damage!");
abilityLabel.setBounds(170, 10, 200, 50);
qAbilityBtn.setEnabled(false);
// here im getting the error
qAbilityBtn.setText(qCDTime / 1000);
qCooldown.start();
});
我知道这可能行不通,但有人可以告诉我为什么会出现该错误以及如何修复它。
此外,如果这不起作用(这很可能不起作用),我该如何制作计时器来显示您必须等待的秒数。
我也 google 做了这个,但我没有找到我需要的东西,或者至少我不知道如何在我的代码中实现它。
This 是我的主文件。
This 是我的游戏文件
P.S:我为我糟糕的英语和糟糕的解释能力道歉!!!
尝试了解错误告诉您的内容。您将 int
类型的参数提供给需要 String
类型参数的函数。为了解决错误,您可以替换此行:
qAbilityBtn.setText(qCDTime / 1000);
有了这个:
qAbilityBtn.setText(String.valueOf(qCDTime / 1000));
这将解决错误,但我很确定代码不会执行您希望它执行的操作,因为按钮上只会显示 qCDTime / 1000
的当前值,它不会已更新。
可能有很多方法可以做到这一点,但这是我会做的。下面的大部分内容是关于如何布局 GUI 的典型样板。
- 使用 Swing 定时器来控制事件。
- 按钮和计时器共享侦听器并使用
actionCommand
确定哪个组件触发了事件。 - 默认倒计时时间为 5 秒。定时器每秒发出一个事件。
- 按钮显示剩余秒数。
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class ButtonTimer extends JPanel {
JFrame f = new JFrame("Button Timer Demo");
JButton b = new JButton("Click Me!");
MyActionListener mal = new MyActionListener();
Timer t = new Timer(0, mal);
public ButtonTimer() {
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
add(b);
b.addActionListener(mal);
b.setPreferredSize(new Dimension(100, 40));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
t.setDelay(1000);
t.setActionCommand("timer");
}
public Dimension getPreferredSize() {
return new Dimension(200, 50);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ButtonTimer());
}
public class MyActionListener implements ActionListener {
public int seconds = 0;
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("timer")) {
if (seconds == 0) {
t.stop();
b.setText("Click Me!");
} else {
b.setText(seconds + "");
seconds--;
}
} else {
// must be button
if (seconds == 0) {
// only do button action if seconds == 0
seconds = 5;
// start timer
t.start();
}
}
}
}
}