2016-10-10 57 views
0

我试图运行下面的非常简单的应用程序不能使用的IntelliJ和1.8 JDK

Main.java运行FXML应用:

package sample; 

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("sample.fxml")); 
     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root, 600, 400)); 
     primaryStage.show(); 
    } 


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

Controller.java

package sample; 

import java.awt.*; 

public class Controller { 
    public TextField theight; 
    public TextField tweight; 


    public Label lbmi; 
    public Label lresult; 


    public void buttonclicked(){ 

     double height; 
     double weight; 
     double bmi; 

     String result; 


     height = Double.parseDouble(theight.getText()); 
     weight = Double.parseDouble(tweight.getText()); 

     double square = height * height; 
     bmi = weight/square; 
     lbmi.setText("The BMI is " + bmi); 
    } 
} 

和sample.fxml

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

<?import javafx.scene.control.Button?> 
<?import javafx.scene.control.Label?> 
<?import javafx.scene.control.TextField?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.RowConstraints?> 
<?import javafx.scene.text.Font?> 

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> 
    <columnConstraints> 
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
     <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
    </columnConstraints> 
    <rowConstraints> 
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
     <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
     <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
    </rowConstraints> 
    <children> 
     <Label alignment="CENTER" prefHeight="43.0" prefWidth="210.0" text="BMI Calculator" GridPane.columnIndex="1"> 
     <font> 
      <Font size="29.0" /> 
     </font> 
     </Label> 
     <TextField fx:id="theight" alignment="CENTER" promptText="Enter your height in meters" GridPane.columnIndex="1" GridPane.rowIndex="1" /> 
     <TextField fx:id="tweight" alignment="CENTER" promptText="Enter your weight in kg" GridPane.columnIndex="1" GridPane.rowIndex="2" /> 
     <Button mnemonicParsing="false" onAction="#buttonclicked" text="Calculate" GridPane.columnIndex="2" GridPane.rowIndex="3" /> 
     <Label fx:id="lbmi" alignment="CENTER" prefHeight="17.0" prefWidth="280.0" text="Your BMI will be displayed here" textOverrun="CLIP" GridPane.columnIndex="1" GridPane.rowIndex="3" /> 
     <Label fx:id="lresult" alignment="CENTER" prefHeight="17.0" prefWidth="211.0" text="What your BMI means" GridPane.columnIndex="1" GridPane.rowIndex="4" /> 
    </children> 
</GridPane> 

我有发现控制器默认没有添加,并将其添加为fx:controller =“sample.Controller”。不过,我仍然得到一个错误:

Caused by: javafx.fxml.LoadException: 
/D:/JAVA/BMIC/out/production/BMIC/sample/sample.fxml:30 

回答

1

在你的控制器类要导入awtTextFieldLabel。导入javafx类。

编辑:

您必须删除AWT进口并插入了JavaFX进口:

package sample; 

import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 

public class Controller { 
    public TextField theight; 
    public TextField tweight; 


    public Label lbmi; 
    public Label lresult; 


    public void buttonclicked() { 

     double height; 
     double weight; 
     double bmi; 

     String result; 


     height = Double.parseDouble(theight.getText()); 
     weight = Double.parseDouble(tweight.getText()); 

     double square = height * height; 
     bmi = weight/square; 
     lbmi.setText("The BMI is " + bmi); 
    } 
} 
+0

我该怎么办呢? import java.javafx。*;不起作用。 – user2742080

+0

import javafx。*;它显示为未使用的导入语句。也许,问题在于别的东西? – user2742080

+0

@ user2742080:您的javafx导入未使用,因为您正在导入awt类,请参阅我的更新答案。 –