繁体   English   中英

Javafx GUI没有显示

[英]Javafx GUI doesn't show up

您好,我是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没有出现,并且显示了很多类似的错误。

使用平台/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home/jre/bin/java在应用程序启动方法java.lang中执行/Users/pyaephyohlaing/NetBeansProjects/table/dist/run1649041788/table.jar java.sun的sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)的sunreflect.InvocationTargetException.java.sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处的sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)上的.reflect.Method.invoke(Method.java:497),com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)上的。 )在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在java.lang.reflect处在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)处sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) Method.invoke(Method.java:4 97)at sun.launcher.LauncherHelper $ FXHelper.main(LauncherHelper.java:767)由以下原因引起:java.lang.RuntimeException:com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java: 917)at com.sun.javafx.application.LauncherImpl.lambda $ launchApplication $ 156(LauncherImpl.java:182)at java.lang.Thread.run(Thread.java:745)原因:java.lang.ClassCastException:javafx。 scene.control.TableColumn不能转换为com.sun.javafx.scene.control.skin上的javafx.scene.control.cell.PropertyValueFactory.call(PropertyValueFactory.java:98)上的javafx.scene.control.TableColumn $ CellDataFeatures。 com.sun.javafx.scene.control.skin.TableRowSkin.getCell(TableRowSkin.java:53)的TableRowSkin.getCell(TableRowSkin.java:87)com.sun.javafx.scene.control.skin.TableRowSkinBase.createCell(TableRowSkin.java:53) com.sun.javafx.scene.control.skin上的TableRowSkinBase.java:698)。com.sun.javafx.scene.control.skin.TableRowSkinBase上的TableRowSkinBase.recreateCells(TableRowSkinBase.java:692)。 javafx的com.sun.javafx.scene.control.skin.TableRowSkin。(TableRowSkin.java:64)的init(TableRowSkinBase.java:146).javafx的javafx.scene.control.TableRow.createDefaultSkin(TableRow.java:212) com.sun.javafx上的javafx.scene.Node.processCSS(Node.java:9056)上的scene.control.Control.impl_processCSS(Control.java:859)在com.sun.javafx上的javafx.scene.Node.applyCss(Node.java:9153) com.sun.javafx.scene.control.skin.VirtualFlow.getCell(VirtualFlow.java:1797)上的.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1964)在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 javafx.scene.Parent.layout(Parent.java:1079)的.layoutChildren(VirtualFlow.java:1189)javafx.scene.Parent.layout(Parent.javafx.scene.Parent.layout(Parent.java:1085) javafx.scene.Scene.doLayoutPass(Scene.java:552)处的javafx.scene.Scene.preferredSize(Sc ene.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)at javafx.bean.property.BooleanPropertyBase.set(BooleanPropertyBase.java:144)at javafx.stage.Window.setShowing(Window.java:922)at javafx.stage.Window.show(Window.java:937) ),位于表的javafx.stage.Stage.show(Stage.java:259).com.sun.javafx.application.LauncherImpl.lambda $ launchApplication1 $ 163(LauncherImpl.java:863)的Table.start(Table.java:27) )的com.sun.javafx.application.PlatformImpl.lambda $ runAndWait $ 176(PlatformImpl.java:326)com.sun.javafx.application.PlatformImpl.lambda $ null $ 174(PlatformImpl.java:295)处的java.security。 com.sun.javafx.application.PlatformImpl.lambda $ runLater $ 175(PlatformImpl.java:294)处的com.sun.glass.ui.InvokeLaterDispatcher $ Future.run(InvokeLaterDispatcher.java:95)的AccessController.doPrivileged(本机方法)运行应用程序表的异常 e.Table Java结果:1删除目录/ Users / pyaephyohlaing / NetBeansProjects / table / dist / run1649041788 jfxsa-run:BUILD SUCCESSFUL(总时间:6秒)

我不知道我的代码有什么问题,有人可以帮助我修复错误吗? 谢谢。

在FXMLDocumentController中,当我认为要为名称设置cellValueFactory时,可以为Id设置单元格工厂。

更改:

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

至:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM