有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java javafx导致运行错误

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

我只是尝试为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非常陌生,所以欢迎任何帮助。谢谢!:)


共 (1) 个答案

  1. # 1 楼答案

    主播的fx:id是媒体,而有一个标签的fx:id也是媒体。清除fx:id,问题就解决了