JavaFX的初學者,只是第一次嘗試。我按照教程一步一步,但不知道爲什麼它不工作後,我添加標籤。 在系統中顯示它很好,但只是不用於標籤,並且不瞭解控制檯信息。我搜索了很多並嘗試過,仍然無法工作。 請幫幫我爲什麼會發生這種情況?以及如何改變它。無法用javaFX在標籤中顯示文本
這是主要的java。
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
//import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
//BorderPane root = new BorderPane();
Parent root =
FXMLLoader.load(getClass().getResource("/application/main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().
add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
主控制器: 包裝應用;
import java.awt.Label;
import java.util.Random;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
public class mainController {
@FXML
private Label myMessage;
public void generateRandom(ActionEvent event){
Random rand = new Random();
int myrand = rand.nextInt(50)+1;
myMessage.setText(Integer.toString(myrand));
//System.out.println(Integer.toString(myrand));
}
}
FXML文件:
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="300" prefWidth="500"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="application.mainController">
<children>
<Button layoutX="165.0" layoutY="174.0" mnemonicParsing="false"
onAction="#generateRandom" text="click me" />
<Label fx:id="myMessage" layoutX="65.0" layoutY="38.0" prefHeight="121.0"
prefWidth="284.0" /> //warning: You can not assign 'Label' to the controller field 'Label'
</children>
</AnchorPane>
這裏是我得到的時候嘗試運行此程序錯誤。
javafx.fxml.LoadException:
/C:/code/eclipse/FXprac4/bin/application/main.fxml:12
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:16)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Can not set java.awt.Label field application.mainController.myMessage to javafx.scene.control.Label
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:764)
at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
... 17 more
謝謝,明白了。有用。我不熟悉java swing和這個awt。現在得到它~~還有很多東西要學。謝謝 ! – CindyL