【问题标题】:javafx causing running errorsjavafx导致运行错误
【发布时间】:2015-12-10 20:11:47
【问题描述】:

这个问题已经解决了!谢谢大家! :)

我只是尝试为 tableview 和 combobox 设置值,但我什至无法让它运行,即使它只是代码的一小部分......

这是我构建的场景。 media scene

这里是主要方法。

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

public class Project3 extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("Media.fxml"));
        primaryStage.setTitle("Media GUI");
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }


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

这是我的控制器:

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

public class Project3Controller{
//for media
@FXML
private TextField titleField;

@FXML
private ComboBox<String> ownerDrop;

@FXML
private ComboBox<String> formatDrop;

@FXML
private ComboBox<String> locationDrop;

@FXML
private ComboBox<String> rippedDrop;

@FXML
private DatePicker releasePicker;

@FXML
private DatePicker purchasePicker;

@FXML
private Button add;

//for owner
@FXML
private TextField fNameField;

@FXML
private TextField lNameField;

@FXML
private TextField phoneField;

@FXML
private ComboBox<String> filterDrop;

@FXML
private Button filter;

//for table view
@FXML
private TableView<MediaViewModel> mediaTable;

@FXML
private TableColumn<MediaViewModel, String> addTitle;

@FXML
private TableColumn<MediaViewModel, String> addLocation;

@FXML
private TableColumn<MediaViewModel, String> addRDate;

@FXML
private TableColumn<MediaViewModel, String> addOwner;

@FXML
private TableColumn<MediaViewModel, String> addRipped;

@FXML
private TableColumn<MediaViewModel, String> addFormat;

@FXML
private TableColumn<MediaViewModel, String> addPDate;

//labels and AnchorPane
@FXML
private Label lastName;

@FXML
private Label purchaseDate;

@FXML
private Label media;

@FXML
private Label title;

@FXML
private Label ripped;

@FXML
private AnchorPane owner;

@FXML
private Label releaseDate;

@FXML
private Label format;

@FXML
private Label firstName;

@FXML
private Label owner2;

@FXML
private Label phone;

@FXML
private Label location;

    //Declare a Observable list for storing TableView's data
    ObservableList<MediaViewModel> mediaData = FXCollections.observableArrayList();
    //int mediaCount = 1;

    public void initialize() {

        //add items in the ComboBox
            final ObservableList<String> formatOption= FXCollections.observableArrayList("DVD","Blu-Ray","Game","Music");
            formatDrop.setItems(formatOption);
            final ObservableList<String> locationOption= FXCollections.observableArrayList("Home","iTunes","Amazon","Microsoft");
            locationDrop.setItems(locationOption);
            final ObservableList<String> rippedOption= FXCollections.observableArrayList("true","false");
            rippedDrop.setItems(rippedOption);
            final ObservableList<String> filterOption= FXCollections.observableArrayList("DVD","Blu-Ray","Game","Music");
            filterDrop.setItems(filterOption);

            mediaTable.setItems(mediaData);

    //add items in the table
            addTitle.setCellValueFactory(new PropertyValueFactory<MediaViewModel, String>("title"));
            addFormat.setCellValueFactory(new PropertyValueFactory<MediaViewModel, String>("format"));
            addLocation.setCellValueFactory(new PropertyValueFactory<MediaViewModel, String>("location"));
            addRipped.setCellValueFactory(new PropertyValueFactory<MediaViewModel, String>("ripped"));
            addRDate.setCellValueFactory(new PropertyValueFactory<MediaViewModel, String>("releaseDate"));
            addPDate.setCellValueFactory(new PropertyValueFactory<MediaViewModel, String>("purchaseDate"));
            addOwner.setCellValueFactory(new PropertyValueFactory<MediaViewModel, String>("owner"));

    }
}

这是 MediaViewModel 类:

import javafx.beans.property.SimpleStringProperty;
import java.io.*;
import java.time.LocalDate;
import javafx.beans.property.*;

public class MediaViewModel{
        private final SimpleStringProperty title = new SimpleStringProperty();
        private final SimpleStringProperty format = new SimpleStringProperty();
        private final SimpleStringProperty location = new SimpleStringProperty();
        private final SimpleStringProperty ripped = new SimpleStringProperty();
        private final SimpleStringProperty releaseDate = new SimpleStringProperty();
        private final SimpleStringProperty purchaseDate = new SimpleStringProperty();
        private final SimpleStringProperty owner = new SimpleStringProperty();

    public MediaViewModel(String t, String f, String l, String r, LocalDate rD, LocalDate pD, String o){
        this.title.set(t);
        this.format.set(f);
        this.location.set(l);
        this.ripped.set(r);
        this.releaseDate.set(rD.toString());
        this.purchaseDate.set(pD.toString());
        this.owner.set(o);
    }


    public String getTitle(){
        return title.get();
    }

    public String getFormat(){
        return format.get();
    }

    public String getLocation(){
        return location.get();
    }

    public String getRipped(){
        return ripped.get();
    }

    public String getReleaseDate(){
        return releaseDate.get();
    }

    public String getPurchaseDate(){
        return purchaseDate.get();
    }

    public String getOwner(){
        return owner.get();
    }
}

当我运行它时,我得到以下错误:

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:497)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    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:497)
    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:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: javafx.fxml.LoadException: 
/Users/shachuan/Desktop/Media.fxml:7

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at Project3.start(Project3.java:10)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.IllegalArgumentException: Can not set javafx.scene.control.Label field Project3Controller.media to javafx.scene.layout.AnchorPane
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:764)
    at javafx.fxml.FXMLLoader.injectFields(FXMLLoader.java:1163)
    at javafx.fxml.FXMLLoader.access$1600(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$ValueElement.processValue(FXMLLoader.java:857)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:751)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    ... 14 more

对不起,代码过长...我对java非常陌生,所以欢迎任何帮助。谢谢!:)

【问题讨论】:

  • 它说“无法将 javafx.scene.control.Label 字段 Project3Controller.media 设置为 javafx.scene.layout.AnchorPane”。你不明白什么?
  • 在您的问题中包含您的 Media.fxml 文件的内容。
  • Media.fxml 的图表位于问题的首位,如果这就是您的意思...我不知道如何直接显示图表。
  • "无法将 javafx.scene.control.Label 字段 Project3Controller.media 设置为 javafx.scene.layout.AnchorPane" 对不起,我刚开始学习 javafx,我真的不知道这是什么意思上周
  • 这意味着您正在尝试将 AnchorPane 的某些内容(可能通过 @FXML 注入)分配给声明为 Label 的名为 media 的变量。请发布您的 FXML 代码

标签: java javafx combobox tableview


【解决方案1】:

AnchorPane 的 fx:id 是媒体,而有一个 Label 的 fx:id 也是媒体。清除fx:ids即可解决。

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 2023-03-18
    • 1970-01-01
    • 2021-12-20
    • 2016-08-05
    • 2015-11-02
    相关资源
    最近更新 更多