我正在研究JavaFX应用程序,在我的场景中是显示在JavaFX中创建的密码提示,它带有两个选项的密码OK和Cancel.我已经返回了用户输入的密码.
我的密码对话框是 -
public static String showPasswordDialog(String title, String message, Stage parentStage, double w, double h) {
try {
Stage stage = new Stage();
PasswordDialogController controller = (PasswordDialogController) Utility.replaceScene("Password.fxml", stage);
passwordDialogController.init(stage, message, "/images/password.png");
if (parentStage != null) {
stage.initOwner(parentStage);
}
stage.initModality(Modality.WINDOW_MODAL);
stage.initStyle(StageStyle.UTILITY);
stage.setResizable(false);
stage.setWidth(w);
stage.setHeight(h);
stage.showAndWait();
return controller.getPassword();
} catch (Exception ex) {
return null;
}
Run Code Online (Sandbox Code Playgroud)
我的代码显示密码提示的位置如下,实际上这个提示将显示在其他UI上,所以我需要将其包含在内部Platform.runlater(),否则它会抛出Not on FX application thread.我需要这个密码提示才能显示,直到我得到正确的密码提示.如果我在runlater中显示密码,我怎样才能获得密码值.
还有其他更好的方法吗?
final String sPassword = null;
do {
Platform.runLater(new Runnable() { …Run Code Online (Sandbox Code Playgroud) 所以我知道JavaFx在使用线程时更新GUI的方法称为Task,但代码的工作方式是否相似或存在差异.让我举个例子:
GUI之外的另一个类作为线程运行
public void run(){
while (socket.isConnected()) {
String x = input.next();
System.out.println(x);
mg.updateChat(x)
}
}
Run Code Online (Sandbox Code Playgroud)
在实际的GUI里面
public void updateChat(final String input){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
txtChat.setText(input);
}
});
}
Run Code Online (Sandbox Code Playgroud)
任务的工作方式是否完全相同?或者是否存在差异,是否有如何修改此代码以在JavaFx项目中工作?
我正在尝试在应用程序GUI中模拟基本恒温器.
我想用新的温度值每2秒更新一个标签盒值.
例如,我的初始温度将显示为68度并更新为69,至70等,直到每2秒75次.
这是我用Java fx编写的一段代码. controlpanel是标签盒所在的te形式的对象.它仅将最终值更新为75.它不会每2秒更新一次.我写了一个方法暂停,导致2秒的延迟.所有标签都会更新其最终值,但不会每2秒更新一次.当我调试时,我可以看到值每2秒增加一个.此代码是在按钮onClick事件中编写的
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int i=0;
Timer asd = new Timer(1000,null);
asd.setDelay(1000);
while(i < 10)
{
jTextField1.setText(Integer.toString(i));
i++;
asd.start();
}
}
Run Code Online (Sandbox Code Playgroud)