- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以在按钮不可见后调用 doClick
吗?
喜欢:
StopBtn.setVisible( false );
StopBtn.doClick();
doClick()
还会继续工作吗?
最佳答案
发现这一点的最简单方法当然是测试它(例如,如果您担心 Oracle 的那些人会改变行为,则在单元测试中)
@Test
public void clickOnInvisibleButton(){
JButton button = new JButton( "test button" );
button.setVisible( false );
final boolean[] buttonClicked = new boolean[]{false};
button.addActionListener( new ActionListener(){
@Override
public void actionPerformed( ActionEvent e ){
buttonClicked[0] = true;
}
});
button.doClick();
assertTrue( "Button has not been clicked", buttonClicked[0] );
}
否则,您可以查看该方法的源代码
public void doClick(int pressTime) {
Dimension size = getSize();
model.setArmed(true);
model.setPressed(true);
paintImmediately(new Rectangle(0,0, size.width, size.height));
try {
Thread.currentThread().sleep(pressTime);
} catch(InterruptedException ie) {
}
model.setPressed(false);
model.setArmed(false);
}
在那里你没有找到可见性检查。进一步看(例如在模型的 setPressed
方法中),您会发现检查了 enabled
状态,但清楚地看到没有检查可见性当下。您还会看到触发了 ActionEvent
,这将触发按钮的 actionPerformed
方法
public void setPressed(boolean b) {
if((isPressed() == b) || !isEnabled()) {
return;
}
if (b) {
stateMask |= PRESSED;
} else {
stateMask &= ~PRESSED;
}
if(!isPressed() && isArmed()) {
int modifiers = 0;
AWTEvent currentEvent = EventQueue.getCurrentEvent();
if (currentEvent instanceof InputEvent) {
modifiers = ((InputEvent)currentEvent).getModifiers();
} else if (currentEvent instanceof ActionEvent) {
modifiers = ((ActionEvent)currentEvent).getModifiers();
}
fireActionPerformed(
new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
getActionCommand(),
EventQueue.getMostRecentEventTime(),
modifiers));
}
fireStateChanged();
}
关于swing - doClick 一个不可见的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12149378/
在 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,基本上三种颜色会按特定顺序隐藏,用户必须从四种可能的颜色中猜测它们是什么。该代码似
我是一名优秀的程序员,十分优秀!