- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我是 stackoverflow 的新手,如果我犯了错误,请多多包涵。
我正在为一个类(class)项目制作这个Java Simon Says Game。它由每个序列号的随机数生成器工作。我通过 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
在内部。相反,使用 JToggleButton
,这将允许您使用 setSelected()
根据按钮的 selected
状态控制每个按钮的外观。游戏中显示了一个完整的示例 Buttons
.在您的 Swing 的 ActionListener
中 Timer
, 选择当前按钮, 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.
}
}
关于java - doClick() 和西蒙 : All buttons will unpress at the same time instead of individually,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27760416/
在 doClick() 中调用 doClick(pressTime) 时它会发送 68 毫秒吗?为什么他们决定使用 68 而不是更圆的数字?它是一个完全任意的数字吗? 来自 Java 抽象按钮: pu
AbstractButton.doClick 是否需要位于调度线程上? 如果我查看我读到的 jdk 6 源代码: public void doClick(int pressTime) {
我可以在按钮不可见后调用 doClick 吗? 喜欢: StopBtn.setVisible( false ); StopBtn.doClick(); doClick() 还会继续工作吗? 最佳答案
据我所知,doClick() 模拟用户有效地单击按钮(或其他组件)。这是否需要启用按钮(不是灰色的、可点击的等)?我可以假设 doClick() 只是调用操作监听器,并且可能从按钮中执行一些动画吗?
我正在用 Java 制作一个钢琴应用程序。这是功能之一, public void playOnce(int time) { play(); doClick(time); sto
我有一个 jbutton,当鼠标点击时它会执行一个功能。为了以编程方式执行此操作,我有另一个函数 void clickButton(){ backButton.doClick(); } 当
是否可以模拟单击按钮,类似于 doClick() 但只是图形模拟,而不生成任何 ActionEvent。如果知道我可以扩展该类,请使用简单的 if 语句执行我自己的 doClick。但还有其他可能吗?
JPanel 是否有等效的 doClick() ?我需要在创建面板的地方调用它?我尝试调用它,它建议我将面板转换到一个抽象按钮,但后来不可能这样做。 最佳答案 不,JPanel 没有 doClick(
JButton的doClick()方法触发了actionPerformed方法,但我想通过使用doClick()方法来触发paintComponent()!请帮忙! 这是建议的吗?这里我有一个标签,每
我有一个带有两个按钮的自定义 JOptionPane,如下所示: AgreementPanel panel = new AgreementPanel(this); // JPanel with som
我正在制作一个计算器,使用文本字段来显示结果并允许用户输入数字和运算(无需单击按钮)。在文本字段中,当用户输入某些内容时,我遇到问题,如果它是有效的字符,我想在相应的按钮中执行 .doClick()
我是java初学者,在制作doClick按钮时遇到一些麻烦。我不明白它是如何工作的,我的代码中需要它。 JButton easyb = new JButton("Easy");
当我的界面上的 JButton 按下按钮触发此事件监听器时,我遇到了问题: if (event.getSource() == goButton) { MainLauncher.doProgra
我正在使用 Swing 编写黑白棋游戏,这是一般代码:(该代码段不包括游戏结束/同一玩家再次进入的情况,这只是基本的游戏循环。) View / Controller : public class Ot
我正在开发一个 Java 项目,需要让按键模拟 JTextField 上的单击。我正在寻找相当于 JButton doClick() 方法。 我试图让按键“enter”执行与单击 JTextField
您好,我是 stackoverflow 的新手,如果我犯了错误,请多多包涵。 我正在为一个类(class)项目制作这个Java Simon Says Game。它由每个序列号的随机数生成器工作。我通过
如果有人可以帮我检查我的代码,给我一些指示等,我将非常感激!因此,在这款游戏中,如果您从未玩过 Mastermind,基本上三种颜色会按特定顺序隐藏,用户必须从四种可能的颜色中猜测它们是什么。该代码似
我是一名优秀的程序员,十分优秀!