doClick()和Simon:所有按钮将同时取消,而不是单独按下 [英] doClick(), and Simon: All buttons will unpress at the same time instead of individually

查看:139
本文介绍了doClick()和Simon:所有按钮将同时取消,而不是单独按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是stackoverflow的新手,所以如果我犯错误请耐心等待。

Hi I'm new to stackoverflow so bear with me if I make mistakes.

我正在做这个 Java Simon说游戏对于一个班级项目。它通过随机数生成器为每个序列#工作。我通过 doClick()显示序列,但事先删除actionlisteners并在之后添加它。

I'm making this Java Simon Says Game for a class project. It works by a random number generator for each sequence#. I show the sequence through doClick() but remove the actionlisteners beforehand and add it afterwards.

问题是按钮在按下所有其他按钮之前不会解压缩或解除锁定。我已经尝试使用thread.sleep在每个if ... else语句之间加一个延迟,但它只会持续按下更长时间。我已经尝试通过try.sleep的try ... catch中的repaint(),revalidate(),updateUI()来更新gui,但这也没有用。

The problem is the buttons won't unpress or unarm until all other buttons have been pressed. I've tried using thread.sleep to put a delay between each if...else statements yet it only stays pressed for longer. I've tried updating the gui through repaint(), revalidate(), updateUI() within the try... catch of the thread.sleep but that didn't work either.

我已经意识到这个问题主要是装饰性的,因为当我尝试实现setPressed或setArmed时,它说它没有被按下但看起来很紧张。

I've realized this issue is mainly cosmetic because when I tried implementing setPressed or setArmed it said it wasn't being pressed but it looked pressed.

这里是没有thread.sleep或我先前在评论中尝试的最简单形式的代码片段。

Here is the code snippet in it's most simplest form without thread.sleep or my previous attempts in comments.

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很新,所以我不是熟练掌握多线程或以这种方式处理事件调度线程。但如果这是唯一的解决方案,我需要更多的帮助。

I'm very new to java so I'm not skilled in multithreading or working on the Event Dispatch Thread for that manner. But if that's the only solution I'll need some more help with that.

我在zip文件中有完整的代码,之前的尝试已注释掉,如果这样做有帮助的话。
https://drive.google.com/file/d / 0Bxg4WleC9jD2VFhoZmZBNjV6Vkk / view?usp = sharing

I have the full code in a zip file with previous attempts commented out if that will help. https://drive.google.com/file/d/0Bxg4WleC9jD2VFhoZmZBNjV6Vkk/view?usp=sharing

推荐答案

调用 doClick()可能是一个尴尬的选择,因为它使用内部计时器 。相反,请使用 JToggleButton ,这将允许您使用 setSelected()状态基于选择状态控制每个按钮的外观code>。 按钮游戏中显示了一个完整的示例。在Swing的 ActionListener 计时器 ,选择当前按钮,播放它的注释并递增序列索引。播放完所有音符后,取消选中所有按钮。

Invoking doClick() may be an awkward choice for this, as it uses a 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 its note and increment the sequence index. When all notes have been played, unselect all the buttons.

附录:您能说明如何实现计时器吗?

概括地说,给出一个合适的切换按钮列表:

In outline, given a suitable list of toggle buttons:

private static final int MAX = 4;
List<JToggleButton> buttons = new ArrayList<JToggleButton>(MAX);
private int i;

计时器的监听器可能如下所示:

The timer's listener might look like this:

@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++;
    }
}

切换按钮的项目监听器应该将按钮的外观更新为由其州表示:

A toggle button's item listener should update the button's appearance as indicated by its state:

@Override
public void itemStateChanged(ItemEvent e) {
    JToggleButton b = (JToggleButton) e.getItem();
    if (b.isSelected()) {
        // change icon, color etc.
    } else {
        // restore icon, color etc.
    }
}

这篇关于doClick()和Simon:所有按钮将同时取消,而不是单独按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆