即使程序运行,我的GUI也不会显示。 Ť

时间:2018-04-27 03:01:57

标签: javafx

[在此输入图像描述] [1]这是驱动程序,程序将运行没有错误,但gui根本不会显示。我是FX的菜鸟,我只是想让它发挥作用。

public class Vacation extends Application {
private Stage stage;

 @Override
public void start(Stage stage) throws Exception {
  this.stage = stage;
  try{
    FXMLLoader loader = new 
FXMLLoader(getClass().getResource("Vacation.fxml"));
    AnchorPane pane = loader.load();

    MainControler mainController = loader.getController();
    Scene scene = new Scene(pane, 640, 450);
    stage.setScene(scene);
    stage.show();

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

    }
}

这是FXML。

<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="Application.MainControler">
<children>
  <AnchorPane layoutX="9.0" layoutY="8.0" prefHeight="131.0" prefWidth="572.0" style="-fx-background-color: #333333;">
     <children>
        <ImageView fitHeight="106.0" fitWidth="470.0" layoutX="51.0" layoutY="13.0">
           <image>
              <Image url="@../../../../../Desktop/FxPics/dc3.jpg" />
           </image>
        </ImageView>
        <Button fx:id="DC" layoutX="229.0" layoutY="53.0" mnemonicParsing="false" onAction="#DCbuttonPpess" text="Devils CourtHouse" textFill="#ee0707" />
     </children>
  </AnchorPane>
  <AnchorPane layoutX="9.0" layoutY="149.0" prefHeight="131.0" prefWidth="572.0" style="-fx-background-color: #333333;">
     <children>
        <ImageView fitHeight="100.0" fitWidth="476.0" layoutX="49.0" layoutY="17.0">
           <image>
              <Image url="@../../../../../Desktop/FxPics/reef.jpg" />
           </image>
        </ImageView>
        <Button fx:id="STB" layoutX="226.0" layoutY="55.0" mnemonicParsing="false" onAction="#STBbuttonPress" text="Scuba The Bahamas" textFill="#01d3f8" />
     </children>
  </AnchorPane>
  <AnchorPane layoutX="9.0" layoutY="295.0" prefHeight="131.0" prefWidth="572.0" style="-fx-background-color: #333333;">
     <children>
        <ImageView fitHeight="100.0" fitWidth="488.0" layoutX="42.0" layoutY="16.0">
           <image>
              <Image url="@../../../../../Desktop/FxPics/SD2.jpg" />
           </image>
        </ImageView>
        <Button fx:id="SDC" layoutX="230.0" layoutY="53.0" mnemonicParsing="false" onAction="#SDCButtonPress" text="Sky Dive Colorado" textFill="#ffa616" />
     </children>
  </AnchorPane>
  <AnchorPane layoutX="9.0" layoutY="445.0" prefHeight="131.0" prefWidth="572.0" style="-fx-background-color: #333333;">
     <children>
        <ImageView fitHeight="106.0" fitWidth="488.0" layoutX="42.0" layoutY="13.0">
           <image>
              <Image url="@../../../../../Desktop/FxPics/c3.jpg" />
           </image>
        </ImageView>
        <Button fx:id="BC" layoutX="248.0" layoutY="54.0" mnemonicParsing="false" onAction="#BCButtonpress" text="Baron Cliffs" textFill="#543801" />
     </children>
  </AnchorPane>

这是控制器。

public class MainControler implements Initializable {

     @FXML
   private Button DC;

    @FXML
    private Button STB;

    @FXML
    private Button SDC;

    @FXML
    private Button BC;


   @FXML public void BCButtonpress(ActionEvent event) {
try{
    FXMLLoader bcLoader = new 
FXMLLoader(getClass().getResource("BaronCliff.fxml"));
    Parent root1 = (Parent) bcLoader.load();
    Stage bc = new Stage();
    bc.setTitle("Baron Cliff");
    bc.setScene(new Scene(root1));
    bc.show();

}catch (IOException e){
    System.out.println("Cant Load new Window.");
}
}


  @FXML  public void DCbuttonpress(ActionEvent event) {
try{
    FXMLLoader bcLoader = new 
FXMLLoader(getClass().getResource("DevilsCourthouse.fxml"));
    Parent root1 = (Parent) bcLoader.load();
    Stage bc = new Stage();
    bc.setTitle("Devils Courthouse");
    bc.setScene(new Scene(root1));
    bc.show();

}catch (IOException e){
    System.out.println("Cant Load new Window.");
}
}


  @FXML  public void STBbuttonPress(ActionEvent event) {
 try{
    FXMLLoader bcLoader = new 
    FXMLLoader(getClass().getResource("Scuba.fxml"));
    Parent root1 = (Parent) bcLoader.load();
    Stage bc = new Stage();
    bc.setTitle("Scuba the Bahamas");
    bc.setScene(new Scene(root1));
    bc.show();

}catch (Exception e){
    System.out.println("Cant Load new Window.");
}
}


    @FXML public void SDC(ActionEvent event) {
 try{
   FXMLLoader bcLoader = new 
 FXMLLoader(getClass().getResource("Skydive.fxml"));
   Parent root1 = (Parent) bcLoader.load();
   Stage bc = new Stage();
   bc.setTitle("Skydive Colorado");
   bc.setScene(new Scene(root1));
   bc.show();

  }catch (Exception e){
   System.out.println("Cant Load new Window.");
   }
    }

在此先感谢任何帮助,我终于崩溃并决定发布这个,我找不到任何人,我的问题可能是一些小问题,但我没有一个新鲜的眼睛真的看看。道歉。

放入e.printStackTrace();在catch块中我得到了一个例外列表。

  javafx.fxml.LoadException: Error resolving 
                   onAction='#DCbuttonPpess', either the event handler is 
                not in the Namespace or there is an error in the script.

       file:/C:/Users/Kalamar/Documents/NetBeansProjects/Vacation/di 
           st/run201542084/Vacation.jar!/Application/Vacation.fxml:17

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$Element.processEventHandlerAttributes(FXMLLoader.java:610)
at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:770)
at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at Application.Vacation.start(Vacation.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)

这是拼写错误。

<Button fx:id="DC" layoutX="229.0" layoutY="53.0" mnemonicParsing="false" 
 onAction="#DCbuttonpress" text="Devils CourtHouse" textFill="#ee0707" 
 />enter 

这是修复后运行后的异常。

            Executing C:\Users\Kalamar\Documen      
           ts\NetBeansProjects\Vacation\dist  \run1238622098\Vacation 
        .jar using platform C:\Program Files\Java\jdk1.8.0_151\jre/bin/java
       javafx.fxml.LoadException: Error resolving onAction='#DCbuttonpress', 
        either the event handler is not in the Namespace or there is an 
         error in the script 
                file:/C:/Users/Kalamar/Documents/NetBeansProjects/Vacation/d 
               ist/run1238622098/V acation.jar!/Application/Vacation.fxml:17

1 个答案:

答案 0 :(得分:0)

堆栈跟踪:

javafx.fxml.LoadException: Error resolving 
               onAction='#DCbuttonPpess', either the event handler is 
            not in the Namespace or there is an error in the script.

   file:/C:/Users/Kalamar/Documents/NetBeansProjects/Vacation/di 
       st/run201542084/Vacation.jar!/Application/Vacation.fxml:17

表示onAction文件的第17行指定的Vacation.fxml处理程序存在问题。那条线是:

<Button fx:id="DC" ... onAction="#DCbuttonPpess" ... />

onAction属性中有一个明显的拼写错误:控制器类中定义的方法是

@FXML public void DCbuttonpress(ActionEvent event) {
    /* ... */
}

所以FXML应该是

<Button fx:id="DC" ... onAction="#DCbuttonpress" ... />