如何访问已启动应用程序的字段
how to get access to the field of already launched application
控制器内部有一个TextArea
检查它是否已经运行的应用程序只是退出而不让另一个实例启动
public class Main extends Application {
private static Controller controller;
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader fxmlLoader = new FXMLLoader();
AnchorPane anchorPane = fxmlLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setScene(new Scene(anchorPane));
primaryStage.setTitle("Hello World");
primaryStage.show();
}
public static void main(final String[] args) throws IOException {
if (Application Launch)) {
//how to access the open application
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
controller = fxmlLoader.getController();
controller.doSomeThing("myText");
System.exit(0);
}else {
launch(args);
}
那是控制器
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
public class Controller {
@FXML
private TextArea textArea;
public void doSomeThing(String myText) {
textArea.setText(myText);
}
}
给你举个小例子:
private static Controller controller;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent anchorPane = fxmlLoader.load();
controller = fxmlLoader.getController();
controller.doSomeThing("myText");
...
并在Controller
定义一个public
函数。
我的控制器:
public void doSomeThing(String myText) {
myTextField.setText(myText);
}
控制器内部有一个TextArea
检查它是否已经运行的应用程序只是退出而不让另一个实例启动
public class Main extends Application {
private static Controller controller;
@Override
public void start(Stage primaryStage) throws Exception{
FXMLLoader fxmlLoader = new FXMLLoader();
AnchorPane anchorPane = fxmlLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setScene(new Scene(anchorPane));
primaryStage.setTitle("Hello World");
primaryStage.show();
}
public static void main(final String[] args) throws IOException {
if (Application Launch)) {
//how to access the open application
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
controller = fxmlLoader.getController();
controller.doSomeThing("myText");
System.exit(0);
}else {
launch(args);
}
那是控制器
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
public class Controller {
@FXML
private TextArea textArea;
public void doSomeThing(String myText) {
textArea.setText(myText);
}
}
给你举个小例子:
private static Controller controller;
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
Parent anchorPane = fxmlLoader.load();
controller = fxmlLoader.getController();
controller.doSomeThing("myText");
...
并在Controller
定义一个public
函数。
我的控制器:
public void doSomeThing(String myText) {
myTextField.setText(myText);
}