首页 > 解决方案 > Java 12.0.1 中的 JavaFX

问题描述

我无法让 OpenJFX 在我的项目中工作。我将库导入到我的 IntelliJ 项目中,它解决了所有构建错误,但是当我启动程序时没有任何反应。控制台没有给我任何输出。该程序启动,然后无限期地运行,没有显示任何内容。当我停止程序时,它说Process finished with exit code 130 (interrupted by signal 2: SIGINT)

主.java

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
    }

    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("Binary Clock");
        stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("clock_gui.fxml"))));
        stage.show();
    }
}

clock_gui.fxml(由 SceneBuilder 创建)

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Rectangle?>


<AnchorPane prefHeight="276.0" prefWidth="344.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="ClockGui">
   <children>
      <HBox layoutX="100.0" layoutY="125.0" spacing="4.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <VBox id="hour_tenner" prefWidth="50.0" spacing="4.0">
               <children>
                  <Rectangle id="ht8" fx:id="ht8" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0">
                     <VBox.margin>
                        <Insets />
                     </VBox.margin>
                  </Rectangle>
                  <Rectangle id="ht4" fx:id="ht4" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
                  <Rectangle id="ht2" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
                  <Rectangle id="ht1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
               </children>
            </VBox>
            <VBox id="hour_single" prefWidth="50.0" spacing="4.0">
               <children>
                  <Rectangle id="hs8" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0">
                     <VBox.margin>
                        <Insets />
                     </VBox.margin>
                  </Rectangle>
                  <Rectangle id="hs4" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
                  <Rectangle id="hs2" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
                  <Rectangle id="hs1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
               </children>
            </VBox>
            <VBox id="minute_tenner" prefWidth="50.0" spacing="4.0">
               <children>
                  <Rectangle id="mt8" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0">
                     <VBox.margin>
                        <Insets />
                     </VBox.margin>
                  </Rectangle>
                  <Rectangle id="mt4" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
                  <Rectangle id="mt2" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
                  <Rectangle id="mt1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
               </children>
            </VBox>
            <VBox id="minute_single" prefWidth="50.0" spacing="4.0">
               <children>
                  <Rectangle id="ms8" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0">
                     <VBox.margin>
                        <Insets />
                     </VBox.margin>
                  </Rectangle>
                  <Rectangle id="ms4" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
                  <Rectangle id="ms2" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
                  <Rectangle id="ms1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="64.0" stroke="BLACK" strokeType="INSIDE" width="64.0" />
               </children>
            </VBox>
            <VBox prefHeight="200.0" prefWidth="100.0" spacing="4.0">
               <children>
                  <Button fx:id="start" mnemonicParsing="false" onMouseClicked="#ClockGui" prefHeight="30.0" prefWidth="64.0" text="start">
                     <VBox.margin>
                        <Insets bottom="102.0" />
                     </VBox.margin>
                  </Button>
                  <HBox prefHeight="100.0" prefWidth="200.0" spacing="4.0">
                     <children>
                        <VBox id="second_tenner" prefWidth="50.0" spacing="4.0">
                           <children>
                              <Rectangle id="st8" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="30.0" stroke="BLACK" strokeType="INSIDE" width="30.0">
                                 <VBox.margin>
                                    <Insets />
                                 </VBox.margin>
                              </Rectangle>
                              <Rectangle id="st4" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="30.0" stroke="BLACK" strokeType="INSIDE" width="30.0" />
                              <Rectangle id="st2" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="30.0" stroke="BLACK" strokeType="INSIDE" width="30.0" />
                              <Rectangle id="st1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="30.0" stroke="BLACK" strokeType="INSIDE" width="30.0" />
                           </children>
                        </VBox>
                        <VBox id="second_single" prefWidth="50.0" spacing="4.0">
                           <children>
                              <Rectangle id="ss8" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="30.0" stroke="BLACK" strokeType="INSIDE" width="30.0">
                                 <VBox.margin>
                                    <Insets />
                                 </VBox.margin>
                              </Rectangle>
                              <Rectangle id="ss4" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="30.0" stroke="BLACK" strokeType="INSIDE" width="30.0" />
                              <Rectangle id="ss2" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="30.0" stroke="BLACK" strokeType="INSIDE" width="30.0" />
                              <Rectangle id="ss1" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="30.0" stroke="BLACK" strokeType="INSIDE" width="30.0" />
                           </children>
                        </VBox>
                     </children>
                  </HBox>
               </children>
            </VBox>
         </children>
         <padding>
            <Insets bottom="4.0" left="4.0" right="4.0" top="4.0" />
         </padding>
      </HBox>
   </children>
</AnchorPane>

ClockGui.java(作为控制器)

import javafx.fxml.FXML;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class ClockGui {

    @FXML
    private Rectangle ht8;

    public ClockGui(){
        ht8.setFill(Color.rgb(225,250,30));
    }
}

我在 Ubuntu 18.04.2 LTS 上。

标签: javajavafxopenjfx

解决方案


你说你启动了虚拟机,但它“什么都不做”。原因主要在于:

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

这是您的应用程序的入口点,但您并没有要求它调用您的应用程序类......

正如您可以从 (Javadoc]( https://openjfx.io/javadoc/12/javafx.graphics/javafx/application/Application.html#launch(java.lang.String...) ) 中读到的那样,这是启动独立应用程序所需的方法,通常从main.

只需添加以下内容:

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

并再次运行。


推荐阅读