doClick() 和 Simon:所有按钮将同时取消按下,而不是单独按下
doClick(), and Simon: All buttons will unpress at the same time instead of individually
嗨,我是 Whosebug 的新手,如果我犯了错误,请多多包涵。
我正在为一个 class 项目制作这个 Java 西蒙说游戏。它由每个序列号的随机数生成器工作。我通过 doClick() 显示序列,但事先删除 actionlisteners 并在之后添加它。
问题是在按下所有其他按钮之前,按钮不会解除按下或解除武装。我已经尝试使用 thread.sleep 在每个 if...else 语句之间放置一个延迟,但它只会保持更长的时间。我已经尝试通过 thread.sleep 的 try...catch 中的 repaint()、revalidate()、updateUI() 更新 gui,但这也不起作用。
我意识到这个问题主要是表面问题,因为当我尝试实施 setPressed 或 setArmed 时,它说它没有被按下,但它看起来是被按下的。
这是最简单形式的代码片段,没有 thread.sleep 或我之前的评论尝试。
public void sequence2() //This is where the issue happens. The buttons won't unpress until every button has been pressed.
{
level.setText(" Level 2"); //Level indicator
Green.removeActionListener(Listener);
Red.removeActionListener(Listener);
Yellow.removeActionListener(Listener);
Blue.removeActionListener(Listener);
if(sequence1 == 1)
{
Green.doClick(300); //Programmatically clicks the button
}
else if(sequence1 == 2)
{
Red.doClick(300);
}
else if(sequence1 == 3)
{
Yellow.doClick(300);
}
else if(sequence1 == 4)
{
Blue.doClick(300);
}
if(sequence2 == 1)
{
Green.doClick(300);
}
else if(sequence2 == 2)
{
Red.doClick(300);
}
else if(sequence2 == 3)
{
Yellow.doClick(300);
}
else if(sequence2 == 4)
{
Blue.doClick(300);
}
Green.addActionListener(Listener);
Red.addActionListener(Listener);
Yellow.addActionListener(Listener);
Blue.addActionListener(Listener);
}
我是 java 的新手,所以我不擅长多线程处理或以这种方式处理事件调度线程。但如果这是唯一的解决方案,我将需要更多帮助。
我在一个 zip 文件中有完整的代码,如果有帮助的话,之前的尝试被注释掉了。
https://drive.google.com/file/d/0Bxg4WleC9jD2VFhoZmZBNjV6Vkk/view?usp=sharing
调用 doClick()
可能是一个尴尬的选择,因为它使用 Timer
internally. Instead, use a JToggleButton
, which will allow you to control each button's appearance based on its selected
state using setSelected()
. A complete example is shown in the game Buttons
. In the ActionListener
of your Swing Timer
, select the current button, play 它的注释并增加 sequence
索引。播放完所有音符后,取消选择所有按钮。
附录:你能展示一下你是如何实现计时器的吗?
概括地说,给定一个合适的切换按钮列表:
private static final int MAX = 4;
List<JToggleButton> buttons = new ArrayList<JToggleButton>(MAX);
private int i;
计时器的侦听器可能如下所示:
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
JToggleButton b = buttons.get(i);
if (i > MAX) { // reset i and all the buttons
for (JToggleButton b : buttons) {
b.setSelected(false);
}
timer.stop();
i = 0;
} else {
b.setSelected(true);
// play tone i
i++;
}
}
切换按钮的项目侦听器应根据其状态指示更新按钮的外观:
@Override
public void itemStateChanged(ItemEvent e) {
JToggleButton b = (JToggleButton) e.getItem();
if (b.isSelected()) {
// change icon, color etc.
} else {
// restore icon, color etc.
}
}
嗨,我是 Whosebug 的新手,如果我犯了错误,请多多包涵。
我正在为一个 class 项目制作这个 Java 西蒙说游戏。它由每个序列号的随机数生成器工作。我通过 doClick() 显示序列,但事先删除 actionlisteners 并在之后添加它。
问题是在按下所有其他按钮之前,按钮不会解除按下或解除武装。我已经尝试使用 thread.sleep 在每个 if...else 语句之间放置一个延迟,但它只会保持更长的时间。我已经尝试通过 thread.sleep 的 try...catch 中的 repaint()、revalidate()、updateUI() 更新 gui,但这也不起作用。
我意识到这个问题主要是表面问题,因为当我尝试实施 setPressed 或 setArmed 时,它说它没有被按下,但它看起来是被按下的。
这是最简单形式的代码片段,没有 thread.sleep 或我之前的评论尝试。
public void sequence2() //This is where the issue happens. The buttons won't unpress until every button has been pressed.
{
level.setText(" Level 2"); //Level indicator
Green.removeActionListener(Listener);
Red.removeActionListener(Listener);
Yellow.removeActionListener(Listener);
Blue.removeActionListener(Listener);
if(sequence1 == 1)
{
Green.doClick(300); //Programmatically clicks the button
}
else if(sequence1 == 2)
{
Red.doClick(300);
}
else if(sequence1 == 3)
{
Yellow.doClick(300);
}
else if(sequence1 == 4)
{
Blue.doClick(300);
}
if(sequence2 == 1)
{
Green.doClick(300);
}
else if(sequence2 == 2)
{
Red.doClick(300);
}
else if(sequence2 == 3)
{
Yellow.doClick(300);
}
else if(sequence2 == 4)
{
Blue.doClick(300);
}
Green.addActionListener(Listener);
Red.addActionListener(Listener);
Yellow.addActionListener(Listener);
Blue.addActionListener(Listener);
}
我是 java 的新手,所以我不擅长多线程处理或以这种方式处理事件调度线程。但如果这是唯一的解决方案,我将需要更多帮助。
我在一个 zip 文件中有完整的代码,如果有帮助的话,之前的尝试被注释掉了。 https://drive.google.com/file/d/0Bxg4WleC9jD2VFhoZmZBNjV6Vkk/view?usp=sharing
调用 doClick()
可能是一个尴尬的选择,因为它使用 Timer
internally. Instead, use a JToggleButton
, which will allow you to control each button's appearance based on its selected
state using setSelected()
. A complete example is shown in the game Buttons
. In the ActionListener
of your Swing Timer
, select the current button, play 它的注释并增加 sequence
索引。播放完所有音符后,取消选择所有按钮。
附录:你能展示一下你是如何实现计时器的吗?
概括地说,给定一个合适的切换按钮列表:
private static final int MAX = 4;
List<JToggleButton> buttons = new ArrayList<JToggleButton>(MAX);
private int i;
计时器的侦听器可能如下所示:
@Override
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
JToggleButton b = buttons.get(i);
if (i > MAX) { // reset i and all the buttons
for (JToggleButton b : buttons) {
b.setSelected(false);
}
timer.stop();
i = 0;
} else {
b.setSelected(true);
// play tone i
i++;
}
}
切换按钮的项目侦听器应根据其状态指示更新按钮的外观:
@Override
public void itemStateChanged(ItemEvent e) {
JToggleButton b = (JToggleButton) e.getItem();
if (b.isSelected()) {
// change icon, color etc.
} else {
// restore icon, color etc.
}
}