JavaFX GUI窗口未显示

时间:2016-11-14 18:25:27

标签: java eclipse user-interface javafx fxml

所以我在Eclipse中使用JavaFX制作了这个应用程序(功能还不完全)。问题是,当我按下播放时,没有任何显示。没有GUI窗口。我顺便使用Scene Builder构建它。当我运行时,没有错误弹出。这是我的代码:

Main.Java

package application;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;


public class Main extends Application {
    private Stage primaryStage;
    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        mainWindow();
    }

    public void mainWindow() {
        try{
            FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainWindowView.fxml"));
            AnchorPane pane = loader.load();
            Scene scene = new Scene(pane);

            MainWindowController controller = loader.getController();
            controller.setMain(this);

            primaryStage.setScene(scene);
            primaryStage.setResizable(false);
            primaryStage.show();

        }catch(Exception e){
            e.printStackTrace();
        }
    }

    private ObservableList<Person> personData = FXCollections.observableArrayList();
    public ObservableList<Person> getPersonData() { return personData; }

    public Main(){
        personData.add(new Person("Dr", "Frankenstein", "1234345", "London", "666666"));
        personData.add(new Person("Steve", "Jobs", "112233", "Silicon Valley", "12345"));
    }

    public static void main(String[] args){

    }
}

MainWindowController.java

package application;

import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class MainWindowController {

    @FXML TableView<Person> tableView;
    @FXML TableColumn<Person, String> firstNameColumn;
    @FXML TableColumn<Person, String> lastNameColumn;

    private Main main;

    public void initialize(){
        firstNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("FirstName"));
        lastNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("LastName"));
    }

    public void setMain(Main main){
        this.main = main;
        tableView.setItems(main.getPersonData());
    }

    @FXML
    public void handleNew(){

    }

    @FXML
    public void handleEdit(){

    }

    @FXML
    public void handleDelete(){

    }

}

MainWindowView.fxml

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

<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>


<AnchorPane prefHeight="500.0" prefWidth="800.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="application.MainWindowController">
   <children>
      <TableView fx:id="tabelView" layoutX="14.0" layoutY="14.0" prefHeight="200.0" prefWidth="350.0" AnchorPane.bottomAnchor="25.0" AnchorPane.leftAnchor="20.0" AnchorPane.topAnchor="25.0">
        <columns>
          <TableColumn fx:id="firstNameColomn" prefWidth="75.0" text="First Name" />
          <TableColumn fx:id="lastNameColumn" prefWidth="75.0" text="Last Name" />
        </columns>
         <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
         </columnResizePolicy>
      </TableView>
      <GridPane layoutX="418.0" layoutY="61.0">
        <columnConstraints>
          <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="45.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="45.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="45.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="45.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="45.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Last Name" GridPane.rowIndex="3" />
            <Label text="Details">
               <font>
                  <Font name="System Bold" size="18.0" />
               </font>
            </Label>
            <Label text="First Name" GridPane.rowIndex="2" />
            <Label text="Phone" GridPane.rowIndex="4" />
            <Label text="City" GridPane.rowIndex="5" />
            <Label text="Postcode" GridPane.rowIndex="6" />
            <Label fx:id="firstNameLabel" GridPane.columnIndex="1" GridPane.rowIndex="2" />
            <Label fx:id="lastNameLabel" GridPane.columnIndex="1" GridPane.rowIndex="3" />
            <Label fx:id="phoneLabel" GridPane.columnIndex="1" GridPane.rowIndex="4" />
            <Label fx:id="cityLabel" GridPane.columnIndex="1" GridPane.rowIndex="5" />
            <Label fx:id="postcodeLabel" GridPane.columnIndex="1" GridPane.rowIndex="6" />
         </children>
      </GridPane>
      <HBox layoutX="418.0" layoutY="418.0" spacing="20.0">
         <children>
            <Button layoutX="418.0" layoutY="418.0" mnemonicParsing="false" text="New" />
            <Button layoutX="518.0" layoutY="418.0" mnemonicParsing="false" text="Edit" />
            <Button layoutX="618.0" layoutY="418.0" mnemonicParsing="false" text="Delete" />
         </children>
      </HBox>
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:4)

您的main方法错过了对launch()的调用:

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