gpt4 book ai didi

java - 以编程方式在 TextField 中触发事件 - JavaFX

转载 作者:行者123 更新时间:2023-11-30 08:39:35 27 4
gpt4 key购买 nike

假设有一个 TextField

TextField tf = new TextField();

我想模拟用户按下一个键。

KeyEvent ke = new KeyEvent(KeyEvent.KEY_RELEASED, 
KeyCode.A.toString(), KeyCode.A.toString(),
KeyCode.A, false, false, false, false);
tf.fireEvent(ke);

如果我检查 tf 的文本内容,我会看到这个状态成立

tf.getText() == ""

我的 KeyEvent 发生了什么,为什么文本不等于“a”?我已经用 KEY_RELEASED、KEY_PRESSED 和 KEY_TYPED 试过了

包含示例:

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

import java.util.concurrent.CountDownLatch;

import javax.swing.SwingUtilities;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

public class TextFieldExample {

@Rule public JavaFXThreadingRule javafxRule = new JavaFXThreadingRule();
@Test
public void onlyAcceptValidInputs() {
TextField tf = new TextField();
KeyEvent ke = new KeyEvent(KeyEvent.KEY_RELEASED,
KeyCode.A.toString(), KeyCode.A.toString(),
KeyCode.A, false, false, false, false);
tf.fireEvent(ke);
Assert.assertEquals("A", tf.getText());
}
}

class JavaFXThreadingRule implements TestRule {
/**
* Flag for setting up the JavaFX, we only need to do this once for all tests.
*/
private static boolean jfxIsSetup;

@Override
public Statement apply(Statement statement, Description description) {

return new OnJFXThreadStatement(statement);
}

private static class OnJFXThreadStatement extends Statement {

private final Statement statement;

private OnJFXThreadStatement(Statement aStatement) {
statement = aStatement;
}

private Throwable rethrownException = null;

@Override
public void evaluate() throws Throwable {

if(!jfxIsSetup) {
setupJavaFX();

jfxIsSetup = true;
}

final CountDownLatch countDownLatch = new CountDownLatch(1);

Platform.runLater(new Runnable() {
@Override
public void run() {
try {
statement.evaluate();
} catch (Throwable e) {
rethrownException = e;
}
countDownLatch.countDown();
}});

countDownLatch.await();

// if an exception was thrown by the statement during evaluation,
// then re-throw it to fail the test
if(rethrownException != null) {
throw rethrownException;
}
}

protected void setupJavaFX() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// initializes JavaFX environment
new JFXPanel();
latch.countDown();
}
});

latch.await();
}

}
}

最佳答案

似乎文本字段正在监听 KEY_TYPED 事件。请注意,对于 KEY_TYPED 事件,text应该是空字符串,code应该是 KeyCode.UNDEFINED

另请注意,在您的测试用例中,文本字段永远不会添加到实时场景中。因此,它不会安装包含实际处理事件的逻辑的外观。因此,您需要通过调用 setSkin(...) 来强制安装皮肤,并传入 protected 方法 createDefaultSkin() 的结果>:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class TextFieldFireEvent extends Application {

@Override
public void start(Stage primaryStage) {
TextField tf = new TextField() {
{
setSkin(createDefaultSkin());
}
};

Button button = new Button("A");
button.setOnAction(e -> {
KeyEvent ke = new KeyEvent(KeyEvent.KEY_TYPED,
"a", "",
KeyCode.UNDEFINED, false, false, false, false);
tf.fireEvent(ke);
System.out.println(tf.getText());
});
VBox root =new VBox(10, /*tf,*/ button);

root.setAlignment(Pos.CENTER);
primaryStage.setScene(new Scene(root, 400, 400));
primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

关于java - 以编程方式在 TextField 中触发事件 - JavaFX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36069049/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com