我正在学习Java(FX),但现在,我已经结束了。我有一个我不明白的错误。它应该绘制矩形。
错误:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/815033865.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException:
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3208)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
at cihly.Cihly.start(Cihly.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/84796442.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/584634336.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/873652227.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/501263526.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at
我该怎么办?
编辑:
这是我的代码:
的Class1:
public class VyplnObrazovku {
private int zakladna;
private int sirkaCihly;
private int vyskaCihly;
private int sirkaProgramu = 600;
private int vyska;
Canvas canvas = new Canvas(600, 330);;
private GraphicsContext gc = canvas.getGraphicsContext2D();
private void nakresliCihlu(int x, int y, int vyska, int sirka) {
gc.setFill(Color.BLACK);
gc.fillRoundRect(x,y,sirka,vyska,5,5);
gc.setFill(Color.RED);
gc.fillRect(x+5, x+5, sirka-5, vyska-5);
}
public void rozmery(int vyska){
this.zakladna = vyska;
this.sirkaCihly = sirkaProgramu - 10 / zakladna;
this.vyskaCihly = sirkaCihly / 2;
this.vyska = vyska;
}
public void vypln(){
int odecist = 0;
int x = 5;
int y = 5;
for(int i = 0; i < vyska; i++){
for(int j = 0; j < zakladna - odecist; j++){
nakresliCihlu(x, y, this.vyskaCihly, this.sirkaCihly);
x += sirkaCihly;
}
y += vyskaCihly;
x = (int) (5 + (i*0.5));
odecist++;
}
}
private void drawShapes(GraphicsContext gc) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
主类:
public class Cihly extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
控制器:
public class FXMLDocumentController implements Initializable {
@FXML
private Label labelCislo;
@FXML
private void handleButtonAction(ActionEvent event) {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
VyplnObrazovku plnic = new VyplnObrazovku();
plnic.rozmery(5);
plnic.vypln();
}
}
EDIT2:
我正在检查FXMLDocument.fxml,但我找不到任何错误。我很感激每一个建议。 FXMLDocument.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="320.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="cihly.FXMLDocumentController">
<children>
<Canvas height="330.0" layoutY="34.0" width="600.0" />
<HBox layoutX="2.0" layoutY="6.0" prefHeight="38.0" prefWidth="600.0">
<children>
<Label prefHeight="14.0" prefWidth="199.0" text="Zadejte výšku zdi v rozmezí 1 až 500:" />
<TextField fx:id="labelCislo" />
<Button fx:id="handleButtonAction" mnemonicParsing="false" text="OK" />
</children>
</HBox>
</children>
</AnchorPane>
答案 0 :(得分:0)
问题是,在fxml文件中,您已使用TextField
定义了fx:id = labelCislo
,但在控制器类中,您尝试将此fx:id变量注入Label
。因此FXMLLoader
无法将标签转换为文本字段。如果您发布了完整的异常错误文本,那么对我们来说会更具描述性。但是你截断了它......