GUI在使用Eclipse在Mac上使用JavaFX 15时没有显示出来。

huangapple go评论74阅读模式
英文:

GUI not showing up with JavaFX 15 using Eclipse on Mac

问题

I've been following instructions to build a Hello World JavaFX application using Java JDK 15 and JavaFX 15. I've added a run configuration described here: [https://openjfx.io/openjfx-docs/#IDE-Eclipse][1]. When I run the program on my Mac I receive no errors in my console but the GUI does not appear. Building the same application on my Windows computer I am able to build and see the GUI. 

Main.java

    package hellofx;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception{
            Parent root = FXMLLoader.load(getClass().getResource("hellofx.fxml"));
            primaryStage.setTitle("Hello World");
            primaryStage.setScene(new Scene(root, 400, 300));
            primaryStage.show();
        }
    
    
        public static void main(String[] args) {
            launch(args);
        }
    }

Controller.java

    package hellofx;
    
    import javafx.fxml.FXML;
    import javafx.scene.control.Label;
    
    public class Controller {
    
        @FXML
        private Label label;
    
        public void initialize() {
            String javaVersion = System.getProperty("java.version");
            String javafxVersion = System.getProperty("javafx.version");
            label.setText("Hello, JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + ".");
        }
    }

hellofx.fxml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.Label?>
    <?import javafx.scene.layout.StackPane?>
    
    
    <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hellofx.Controller">
        <children>
            <Label fx:id="label" text="Label" />
        </children>
    </StackPane&gt


  [1]: https://openjfx.io/openjfx-docs/#IDE-Eclipse
英文:

I've been following instructions to build a Hello World JavaFX application using Java JDK 15 and JavaFX 15. I've added a run configuration described here: https://openjfx.io/openjfx-docs/#IDE-Eclipse. When I run the program on my Mac I receive no errors in my console but the GUI does not appear. Building the same application on my Windows computer I am able to build and see the GUI.

Main.java

package hellofx;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("hellofx.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 400, 300));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Controller.java

package hellofx;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class Controller {
@FXML
private Label label;
public void initialize() {
String javaVersion = System.getProperty("java.version");
String javafxVersion = System.getProperty("javafx.version");
label.setText("Hello, JavaFX " + javafxVersion + "\nRunning on Java " + javaVersion + ".");
}
}

hellofx.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.StackPane?>
<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="hellofx.Controller">
<children>
<Label fx:id="label" text="Label" />
</children>
</StackPane>

答案1

得分: 6

这个问题的解决方案可以在这里找到。

> 前往主类的运行配置,在“参数”选项卡中,取消勾选“在使用SWT启动时使用-XstartOnFirstThread参数”。

英文:

The solution to this problem can be found here.

> Go to Run Configurations for the main class, and on the "Arguments"
> tab, uncheck the box where it says "Use the -XstartOnFirstThread
> argument when launching with SWT".

huangapple
  • 本文由 发表于 2020年10月23日 10:42:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64493118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定