FXML文件已加载但未显示在场景中

时间:2019-04-24 03:17:35

标签: javafx fxml scenebuilder

我试图做到这一点,因此当用户单击listView中的项目时,它将加载一个新场景。 listView是另一个场景的子级,并且我实质上是试图将listView场景与另一个场景交换。尝试执行此操作时,FXML文件已加载(通过控制台指示),但未出现在我的GUI中。

这是Main类的代码,具有阶段(存储原始场景)

package Controllers;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import javafx.application.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import util.Album;
import util.Artist;
import util.FileSystem;
import util.Song;

public class Main extends Application{

    private Stage mainStage = new Stage();
    private BorderPane mainLayout;
    private MainController mainController;
    private Scene scene;

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

@Override
public void start(Stage stage) throws Exception {
        stage.setTitle("Music Player");
        showMainView();
}

public void showMainView() throws Exception {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));

        mainLayout = loader.load();

        scene = new Scene(mainLayout);
        mainStage.setScene(scene);
        mainStage.show();
    }

这是试图调用XML文件的主控制器。

package Controllers;

import java.io.IOException;

import javax.sound.sampled.Clip;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.shape.SVGPath;
import util.*;

public class MainController {
     @FXML private ImageView nowPlayingImage;
     @FXML private Label nowPlayingSong;

     @FXML private HBox mediaControls; 
     @FXML private SVGPath shuffleButton;  
     @FXML private SVGPath repeatButton;  
     @FXML private SVGPath playButton;  
     @FXML private SVGPath pauseButton;   
     @FXML private SVGPath skipForward;  
     @FXML private SVGPath queueButton;  
     @FXML private Slider timeSlider; 
     @FXML private VBox playlists; 
     @FXML private VBox artists;
     @FXML private VBox albums; 
     @FXML private VBox songs;
     @FXML private TextField searchBar;
     @FXML private HBox subView;

     private Main main = new Main();
     private MusicPlayer music = new MusicPlayer();



    public void playListsScene(MouseEvent event) {
        System.out.println("Test Text");

        try {

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

    }

    public void artistsScene(MouseEvent event) throws Exception {
        ListView scene2 = new ListView();
        scene2= FXMLLoader.load(getClass().getResource("Artists.fxml"));
        subView.getChildren().setAll(scene2);
    }

    public void setSubView() {
        try {

            BorderPane pane = new BorderPane();
            pane = FXMLLoader.load(getClass().getResource("ArtistSub.fxml"));
            subView.getChildren().setAll(pane);
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

}

这里是ArtistController,它加载listView

package Controllers;

import java.io.IOException;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import util.Album;
import util.Artist;
import util.FileSystem;
import util.Song;

public class ArtistController {

    @FXML
    private ListView<Artist> artistList;
    private Song selectedSong;
    private Album selectedAlbum;
    private Artist selectedArtist;

    public FileSystem file = new FileSystem();

    public void initialize() {
        ObservableList<Artist> artists = file.getArtists();
        artistList.setItems(artists);
        /*
        int limit = artists.size();
        for(int i=0; i<artists.size(); i++) {
            String artist = artists.get(i).toString();
            System.out.println(artist);
            artistList.getItems().add(artist);
        }
        */
        artistList.setOnMouseClicked(event -> {

             if (event.getClickCount() == 2) {
                 selectedArtist=artistList.getSelectionModel().getSelectedItem();
                 /*
                 ObservableList<Song> songs = FXCollections.observableArrayList();
                 ObservableList<Album> albums = FXCollections.observableArrayList();
                 for (Album album : selectedArtist.getAlbums()) {
                     albums.add(album);
                     songs.addAll(album.getSongs());
                 }
                 */
                try {
                    MainController main = new MainController();
                    System.out.println("test");
                    main.setSubView();



                } catch (Exception e) {

                }


             }
        });
    }



}

我还是javafx的新手,所以我希望我不要错过任何可能导致此问题的大问题。

0 个答案:

没有答案