当前位置: 首页 > 知识库问答 >
问题:

如何在JavaFX后台工作的GUI中进行更改?

司寇照
2023-03-14

从所有的搜索和阅读中可以清楚地看出,我需要调用platform.runlater()来更改GUI。似乎我还需要使用Runnable接口。也许我也应该使用任务?

但我不知道该怎么用。另外,我不确定我该把他们放到哪一个班。我是JavaFX的超级新手。

我的试用JavaFX项目只有一个Label和一个TextField。标签包含一个问题,而TextField用于回答问题。足够简单。

我遇到了这个问题:

答案检查方法在一个单独的类中。我不知道如何访问GUI/FXML的组件并更改它们。其他类中的方法是静态的,而GUI/FXML的组件是非静态的。

因为我的实际项目会有很多测验,所以我热衷于使用单独的类来检查答案。

这里只有3个小班是相关的:

    null
package com.dan.ans;

import com.dan.qn.Qn;
import com.dan.view.ViewController;
public class Ans {
public static void checkAns() {

    // Checks if the ans is correct.
    if (ViewController.getTextFieldInput().equalsIgnoreCase(Qn.getAns())) {

        System.out.println("Correct!");     // Here I want the label to say 'Correct!' rather than it be print out in the console.

        Qn.setQuestion();                   // This gets the next question from the database. But again, I don't know how to make the changes show on the screen. (In the actual code I'd have a separate Label for each of these things)

    } else { // Runs if it's not correct.

        System.out.println("Incorrect!");    // Here I want the label to say 'Incorrect' rather than it be print out in the console.
    }
  }
}
package com.dan.view;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import com.dan.ans.Ans;
import com.dan.qn.Qn;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

public class ViewController implements Initializable {

private static String textFieldInput;      // I don't know how to access the typed info in the textField from another class. So I store it here and get it from it.

// This is the getter I use for it. (See above)
public static String getTextFieldInput() {
    return textFieldInput;
}

@FXML
private Label label;

@FXML
private TextField textField;

@Override
public void initialize(URL location, ResourceBundle resources) {

    Qn.setQuestion();                       // This method is in the Qn class. It retrieves data from the db file and keeps them in variables.

    label.setText(Qn.getQn());              // This sets the label's text using the retrieved data. So you see the first question when the program opens.
}

// Event Listener on TextField[#textField].onAction
public void enter(ActionEvent event) throws IOException {

    textFieldInput = textField.getText();    // Stores the typed info in the variable to be accessed from elsewhere.

    Ans.checkAns();                         // Runs the checkAns to check if the typed answer is correct or not.

  }

}

“Launcher”方法看起来就像任何带有主类的方法。所以我没有在这里分享它的代码。

有人能告诉我如何从其他类(如“ans”)更新GUI中的组件吗?我很确定应该使用platform.runlater()和runnable。也可能是任务。我看过几个例子,但不清楚如何在上下文中使用它。

提前多谢了!:)

共有1个答案

步弘和
2023-03-14

现在还不是很清楚这里的问题是什么。自然的方法(反正对我来说)就是让checkAnswer(...)方法成为一个简单的“按方框所说的做”的方法,即将答案作为参数,检查它,并向调用方返回一个值,以指示它是否正确。

这样还可以避免所有丑陋的静态攻击。

public class Ans {

    public boolean checkAns(String answer) {
        // not really sure what Qn is here, but you can also clean this up and
        // get rid of the static methods
        if (answer.equalsIgnoreCase(Qn.getAns()) {
            // not sure if this really belongs here?
            Qn.setQuestion(); // really takes no parameters? Sets it to what, then?
            return true ;
        } else {
            return false ;
        }
    }
}

然后在你的控制器里,你可以做

public class ViewController implements Initializable {

    private Ans ans ;

    @FXML
    private Label label;

    @FXML
    private TextField textField;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

        ans = new Ans();
        // ...
    }

    // ...

    public void enter(ActionEvent event) {
        if (ans.checkAns(textField.getText())) {
            // update UI to show answer was correct, etc
        } else {
            // update UI to show answer was incorrect...
        }
    }

    // ...
}
 类似资料:
  • 我目前正在尝试为我的程序创建一个启动屏幕,因为启动需要一些时间。问题是创建GUI需要一段时间(创建对话、更新表格等)。而且我不能将GUI创建移动到后台线程(如“Task”类),因为我会得到“Not on FXApplication thread”异常。我尝试使用: 以及任务的“调用”方法: 当我在Swing中编写程序时,我可以在EventDispatchThread上显示和更新Splash屏幕,而

  • 问题内容: 在我的数据库中,我可以说有5000多个用户,现在,如果我在主父节点中使用来获得多少人, 我知道getChildrenCount会返回一个带有父级内部子级数量的long,但这会如何影响性能? 几个月前,我制作了一个应用程序来管理用户,该应用程序比通过Web控制台更有效地管理用户,因为页面变得静态并且用户不断增长。 现在,我想知道是否每当我请求用户数量时,该应用程序就会循环遍历5000+个

  • 问题内容: 我在Qt工作,当我按下GO按钮时,我需要不断将软件包发送到网络并使用收到的信息修改界面。 问题是我在按钮中有一个,所以按钮永不结束,所以界面永不更新。我想在按钮中创建一个线程并将代码放在那里。 我的问题是如何从线程修改接口?(例如,如何从线程修改textBox? 问题答案: 关于Qt的重要一点是, 必须 仅从GUI线程(即主线程)使用Qt GUI。 这就是为什么执行此操作的正确方法是从

  • 我有一个函数/GUI问题。我正在编码一个轻函数,它开始一个例程,并检查时间是否在上午8点和一些停止时间之间。这个例程从早上8点开始,在那个任意的时间结束。问题是,一旦我点击这个例程的开始,图形用户界面就不会让我带着开始按钮离开窗口,因为它卡在定时例程里面了。我真的希望能够设置计时器在后台运行,然后能够离开图形用户界面的窗口。看起来线程是做到这一点的关键,但我不确定。 我将GUI代码放在一个单独的文

  • 问题内容: 在我的应用程序中,当用户登录时,我具有此逻辑,它将使用用户拥有的所有符号调用以下方法。 我的问题是,由于此发送符号的任务可以缓慢完成,但必须完成,所以无论如何,我可以将其作为后台任务吗? 这可能吗 ?? 请分享您的意见。 问题答案: 您正在寻找类似这样的东西。 创建执行程序服务。这将保留线程池以供重用。与每次为每个异步方法调用创建一个新线程相比,效率要高得多。 如果您需要对Execut

  • 我最初的fxml(例如)有很多功能,因此完全加载需要大量时间。因此,为了避免程序启动和fxml加载之间的时间间隔,我引入了另外一个fxml()和一个gif图像,该图像应该在加载主fxml时出现。问题是我的加载器中的gif图像。fxml不会移动,就像程序中的挂起一样,直到home.fxml被完全加载。为了避免这种情况,我将home.fxml加载移到一个线程中,如下代码所示。 但是在这段代码之后,我的