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
答案 0 :(得分:0)
在mainController类中导入了导入java.awt.Label;将此更改为 javafx.scene.control.Label然后上面的错误将会出现
答案 1 :(得分:0)
在Main Controller类中将标签java.awt.Label更改为javafx.scene.control.Label
import javafx.scene.control.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));
}
}