TabPane无法处理我的应用程序

时间:2018-03-25 09:17:18

标签: java javafx javafx-8

我有一个来自uni的项目,我必须用Java创建一个应用程序(在模型视图控制器格式中),我想在我的应用程序中制作标签,但它似乎没有工作。

我查了很多教程,他们都告诉我如何使用TabPane,但它对我不起作用。

以下是我在Application Loader类中的代码:

    package main;

import controller.ModuleChooserController;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;
import model.StudentProfile;
import view.ModuleChooserRootPane;

public class ApplicationLoader extends Application {

    private ModuleChooserRootPane view;

    @Override
    public void init() {
        //create model and view and pass their references to the controller
        StudentProfile model = new StudentProfile();
        view = new ModuleChooserRootPane();
        new ModuleChooserController(view, model);   
    }

    @Override
    public void start(Stage stage) throws Exception {
        //whilst you can set a min width and height (example shown below) for the stage window,
        //you should not set a max width or height and the application should
        //be able to be maximised to fill the screen and ideally behave sensibly when resized
        stage.setMinWidth(530); 
        stage.setMinHeight(500);

        TabPane tabPane = new TabPane();
        Tab tab = new Tab("Testing");
        tabPane.getTabs().add(tab);

        stage.setTitle("Final Year Module Chooser Tool");
        stage.setScene(new Scene(view));
        stage.show();
    }

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

}

我实施了TabPane,但没有任何结果。我也尝试在我的“视图”包中实现TabPane,但我也没有运气。

以下是ModuleRootChooserPane的代码:

    package view;

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;

//You may change this class to extend another type if you wish
public class ModuleChooserRootPane extends BorderPane {

    private ButtonPane bp;
    private ProfileCreator profileCreator;


    public ModuleChooserRootPane() {

        //This sets the colour of background
        this.setStyle("-fx-background-color: #EDF1F3;");

        //Creates a new instance of the buttonPane (Used from ButtonPane.java) and ProfileCreator
        bp = new ButtonPane();
        profileCreator = new ProfileCreator();

        //This adds the padding on the left so that "submit" button is in line with text fields
        bp.setPadding(new Insets(0, 0, 0, 120));

        //Creates a new VBox which adds the ProfileCreator and the button pane
        VBox rootContainer = new VBox(profileCreator, bp);
        rootContainer.setPadding(new Insets(100,100,100,100));


        this.getChildren().add(rootContainer);
    }

}

1 个答案:

答案 0 :(得分:0)

您选择在没有fxml文件的情况下工作,因此您需要在类ModuleChooserRootPane中创建视图,每个图形元素都必须在此处,或者在此处使用的其他类中。

所以你必须在它的构造函数中添加TabPane

public ModuleChooserRootPane() {
    ...
    //this.getChildren().add(rootContainer);
    setLeft(rootContainer);         // or Top/Bottom/Right/Center 


    TabPane tabPane = new TabPane();
    Tab tab = new Tab("Testing");
    tabPane.getTabs().add(tab);
    setCenter(tabPane);             // or Top/Bottom/Right/Left

}

BorderPane对于根元素来说是个好主意,因为它有几个区域来添加元素,但是你需要使用setLeft(), setRight(), setCenter(), setTop() and setBottom()而不仅仅是getChildren().add(),你无法控制这个地方

enter image description here

在不同标签中添加内容的示例:

TabPane tabPane = new TabPane();
Tab tab = new Tab("Testing");
tab.setContent(new VBox(new Label("Here is the testing place"), new Circle(15, 12, 10)));

Tab tab2 = new Tab("Testing2");
HBox hboxContentTab2 = new HBox();
hboxContentTab2.getChildren().add(new Ellipse(10, 10, 10, 13));
hboxContentTab2.getChildren().add(new Label("Here is the BIS testing place"));
tab2.setContent(hboxContentTab2); // add a Node created before, ot can be whatever you wan, borderpane, gridpane, hbox, vbox, label ...

tabPane.getTabs().addAll(tab, tab2);
相关问题