【问题标题】:Javafx GUI doesn't show upJavafx GUI 不显示
【发布时间】:2016-03-02 09:38:15
【问题描述】:

您好,我是 javaFx fxml 的新学习者,我正在使用 fxml 测试表视图。但是 netbeans 在我的代码中没有显示错误,我无法运行我的 GUI 应用程序。

我的 FXMLDocument.fxml 代码在这里。

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

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

        <AnchorPane id="AnchorPane" prefHeight="359.0" prefWidth="473.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="table.FXMLDocumentController">
            <children>
                <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
              <TableView fx:id="tableview" layoutX="137.0" layoutY="90.0" prefHeight="200.0" prefWidth="200.0">
                <columns>
                  <TableColumn fx:id="id" prefWidth="93.0" text="Id" />
                  <TableColumn fx:id="name" prefWidth="106.0" text="Name" />
                </columns>
              </TableView>
            </children>
        </AnchorPane>

这是一个 fxml 控制器代码。

package table;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;

/**
 *
 * @author pyaephyohlaing
 */
public class FXMLDocumentController implements Initializable {

    //the table's view and columns
    @FXML
    TableView<Person>tableview;
    @FXML
    TableColumn id;
    @FXML
    TableColumn name;

    //the table data
    ObservableList<Person>data = FXCollections.observableArrayList();

    @Override
    public void initialize(URL url, ResourceBundle rb) {

    //setup the table data
    id.setCellValueFactory(
            new PropertyValueFactory<Person,Integer>("id")
    );
    name.setCellFactory(
            new PropertyValueFactory<Person,String>("name")
    );

    data.add(new Person(1,"Mg Mg"));
    tableview.setItems(data);
    System.out.println("hello");
    }    

}

这是 Person 类代码。 (Person.java)

package table;
import javafx.beans.property.*;

// a class that represents the person data to add into the table view
public class Person {

//a variable that represent the column data neeed to be JavaFx properties(i.e SimpleStringProperty)   
//sets as the JavaFx table data property   
    public SimpleIntegerProperty id;
    public SimpleStringProperty name;

    public Person(Integer id,String name) {
        System.out.println("Hee");
        this.id=new SimpleIntegerProperty(id);
        this.name=new SimpleStringProperty(name);
    }

//getter method
    public Integer getId() {
        return  id.get();
    }
    public String getName() {
        return name.get();
    }

}

这是主要的 Table.java 代码。

package table;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author pyaephyohlaing
 */
public class Table 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.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

当我运行我的项目时,没有出现 GUI,它显示了很多这样的错误。

执行 /Users/pyaephyohlaing/NetBeansProjects/table/dist/run1649041788/table.jar 使用平台 /Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/bin/java 应用程序启动方法中的异常 java.lang.reflect.InvocationTargetException 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:497) 在 com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 在 com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:497) 在 sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 原因:java.lang.RuntimeException:应用程序启动中的异常 方法在 com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 在 com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182) 在 java.lang.Thread.run(Thread.java:745) 引起: java.lang.ClassCastException:javafx.scene.control.TableColumn 不能 被转换为 javafx.scene.control.TableColumn$CellDataFeatures at javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:98) 在 com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:87) 在 com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:53) 在 com.sun.javafx.scene.control.skin.TableRowSkinBase.createCell(TableRowSkinBase.java:698) 在 com.sun.javafx.scene.control.skin.TableRowSkinBase.recreateCells(TableRowSkinBase.java:692) 在 com.sun.javafx.scene.control.skin.TableRowSkinBase.init(TableRowSkinBase.java:146) 在 com.sun.javafx.scene.control.skin.TableRowSkin.(TableRowSkin.java:64) 在 javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212) 在 javafx.scene.control.Control.impl_processCSS(Control.java:859) 在 javafx.scene.Node.processCSS(Node.java:9056) 在 javafx.scene.Node.applyCss(Node.java:9153) 在 com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1964) 在 com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797) 在 com.sun.javafx.scene.control.skin.VirtualFlow.getCellLength(VirtualFlow.java:1879) 在 com.sun.javafx.scene.control.skin.VirtualFlow.computeViewportOffset(VirtualFlow.java:2528) 在 com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1189) 在 javafx.scene.Parent.layout(Parent.java:1079) 在 javafx.scene.Parent.layout(Parent.java:1085) 在 javafx.scene.Parent.layout(Parent.java:1085) 在 javafx.scene.Scene.doLayoutPass(Scene.java:552) 在 javafx.scene.Scene.preferredSize(Scene.java:1646) 在 javafx.scene.Scene.impl_preferredSize(Scene.java:1720) 在 javafx.stage.Window$9.invalidated(Window.java:846) 在 javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:109) 在 javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144) 在 javafx.stage.Window.setShowing(Window.java:922) 在 javafx.stage.Window.show(Window.java:937) 在 javafx.stage.Stage.show(Stage.java:259) 在 table.Table.start(Table.java:27) 在 com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863) 在 com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326) 在 com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295) 在 java.security.AccessController.doPrivileged(Native Method) 在 com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294) 在 com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) 异常运行应用程序表。表 Java 结果:1 删除 目录 /用户/pyaephyohlaing/NetBeansProjects/table/dist/run1649041788 jfxsa-run:构建成功(总时间:6 秒)

我不知道我的代码有什么问题,谁能帮我修复错误?谢谢。

【问题讨论】:

    标签: java user-interface javafx tableview


    【解决方案1】:

    在 FXMLDocumentController 中,当我认为您打算为名称设置 cellValueFactory 时,您为 Id 设置了单元工厂。

    变化:

    id.setCellFactory(
            new PropertyValueFactory<Person,String>("name")
    );
    

    收件人:

    name.setCellValueFactory(
            new PropertyValueFactory<Person,String>("name")
    );
    

    【讨论】:

    • 感谢您的回答。但运行时错误仍然显示....GUI 不显示!! :(
    • @PyaePyoHlaing 请注意差异:setCellFactory 和 setCellValueFactory 也..
    • @PyaePyoHlaing 这是您问题的答案,也检查了我的 IDE 上的代码。我想说我们应该用它们各自的类型声明泛型类:TableColumn&lt;Person, Integer&gt; id 而不是TableColumn id。提高阅读能力。
    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2016-06-10
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多