简体   繁体   English

JavaFX - 在使用选项卡时将 GUI 拆分为单独的类

[英]JavaFX - Splitting GUI into separate classes while using tabs

I am trying to make a program with a tabbed GUI where each tab's contents are their own class.我正在尝试使用选项卡式 GUI 制作一个程序,其中每个选项卡的内容都是它们自己的 class。 I essentially want to do what is done here , but with tabs.我基本上想做这里所做的事情,但是使用标签。 Mere is the Manager class:只是经理 class:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;

public class Manager extends Application {

    private TabPane tabPane;
    private Tab dcTab;

    public Manager() {

        dcTab = new Tab();

        tabPane = new TabPane();

        DistributionCenter dcMenu = new DistributionCenter();

        dcTab.setText("Distribution Center");
        dcTab.setContent(dcMenu);
        tabPane.getTabs().add(dcTab);

    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        Group root = new Group();

        Scene scene = new Scene(tabPane, 600, 500);

        DistributionCenter dcMenu = new DistributionCenter();
        root.getChildren().add(dcMenu);
        dcTab.setContent(dcMenu);

        primaryStage.setScene(scene);
        primaryStage.setTitle("909th Wing");

        primaryStage.show();

    }

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

And here is my DistributionCenter class:这是我的 DistributionCenter class:

import javafx.scene.control.Button;
import javafx.scene.layout.Pane;

public class DistributionCenter extends Pane {

    private Button testBt1;

    public DistributionCenter() {

        Pane dcMenu = new Pane();

        testBt1 = new Button("Button");
        dcMenu.getChildren().addAll(testBt1);

        testBt1.setLayoutX(30);
        testBt1.setLayoutY(100);

    }
}

Note that the contents of DistributionCenter are just a test at this point.请注意,此时 DistributionCenter 的内容只是一个测试。

You are never adding your Button to the actual DistributionCenter pane.您永远不会将Button添加到实际的DistributionCenter窗格中。 Instead, you are creating a new Pane within that class and adding the button to it.相反,您正在该 class 中创建一个新Pane并将按钮添加到其中。

Remember, DistributionCenter is a Pane so it has its own children.请记住, DistributionCenter一个Pane ,因此它有自己的子级。 You do not need to create a new Pane within it.您无需在其中创建新Pane

Update your DistributionCenter class to just configure itself:更新您的DistributionCenter class 以仅配置自身:

public DistributionCenter() {

    testBt1 = new Button("Button");
    getChildren().addAll(testBt1);

}

That will solve your problem, but you should never extend a class unless you need to implement different behavior .这将解决您的问题,但您不应该扩展 class 除非您需要实现不同的行为 If you are just configuring a Node like this, you should just create an instance of a Pane and configure it properly.如果你只是像这样配置一个Node ,你应该只创建一个Pane的实例并正确配置它。

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

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