JavaFx如何避免创建一个巨大的控制器 [英] JavaFx how to avoid creating one huge controller

查看:126
本文介绍了JavaFx如何避免创建一个巨大的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaFX中有一个应用程序,它有一个带有菜单和工具栏的主场景,以及在按下一个菜单按钮后注入这个主场景的较小场景。

I have an app in JavaFX, which has main scene with menu and toolbar, and smaller scenes, which are injected into this main scene, after one of menu buttons are being pressed.

现在,HomeCntroller负责任一场景组件:Home Scene(带工具栏和菜单)和注入场景。如果注入的场景数量超过一个,这会导致我创建庞大,巨大且非常不专业的控制器。

Now, HomeCntroller is responsible for either scene components: Home Scene (with toolbar and menu), and injected scene. This leads me to create massive, huge and very unprofessional controller if number of injected scenes is more than one.

如何拆分控制器责任?

How to split controller responsibility?

现在我的控制器看起来像这样:
changeDashboardPane方法将较小的窗格注入我的主HomePane。

Now my Controller looks like this: changeDashboardPane method injects smaller Pane into my main HomePane.

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired) )
public class HomeController extends AbstractController {
    private static final Logger LOG = Logger.getLogger(HomeController.class);

    private final BudgetProfileService budgetProfileService;

    @FXML
    private Label usernameLabel;

    @FXML
    private ComboBox<String> budgetProfilesComboBox; 

    @FXML
    private AnchorPane dashBoardPane;

    @FXML
    public void initialize() {
        refreshUsernameLabel();
        getAllBudgetProfiles();
        changeDashboardPane(PaneFactoryKeys.FINANCES_PANE);
    }

    private void refreshUsernameLabel() {
        String username = UserAccountProvider.getLoggedUser().getUsername();
        usernameLabel.setText(username);
    }

    private void getAllBudgetProfiles() {
        List<String> budgetProfileNames = budgetProfileService.getAllBudgetProfileNames();
        if (!budgetProfileNames.isEmpty()) {
            budgetProfilesComboBox.getItems().clear();
            budgetProfilesComboBox.getItems().addAll(budgetProfileNames);
        }
    }

    @FXML
    public void handleFinancesButtonAction() {
        changeDashboardPane(PaneFactoryKeys.FINANCES_PANE);
    }

    @FXML
    public void handlePeriodButtonAction() {
        changeDashboardPane(PaneFactoryKeys.PERIOD_PANE);
    }

    @FXML
    public void handleStatisticsButtonAction() {
        changeDashboardPane(PaneFactoryKeys.STATISTICS_PANE);
    }

    @FXML
    public void handleSettingsButtonAction() {
        changeDashboardPane(PaneFactoryKeys.SETTINGS_PANE);
    }

    private final void changeDashboardPane(String paneFactoryKey) {
        double injectedPanePosition = 0.0;
        Pane paneToChange = getPaneFromFactory(paneFactoryKey);
        dashBoardPane.getChildren().clear();
        AnchorPane.setTopAnchor(paneToChange, injectedPanePosition);
        dashBoardPane.getChildren().add(paneToChange);
    }

}

为了更清楚,屏幕:

未注入第二个窗格
注入第二个窗格

任意想法家伙?

推荐答案

我建议你将主场景分成较小的场景,例如你可以有一个工具场景,一个标题场景,内容场景等。然后你应该为每个场景都有一个控制器。

I would recommend you to divide your main scene in smaller ones, for example you can have a tools scene, a header scene, a content scene and so on. Then you should have one controller for every scene.

之后我会使用发布者 - 订阅者模式来处理行为,比如你在设置场景上按下按钮时,它会触发其他场景听到的事件,然后他们会相应地改变它们的状态。

After that I would use a publisher-subscriber pattern to deal with behaviors, like when you press a button on settings scene, it triggers an event that other scenes listen to and then they handle it changing their state accordingly.

我希望它很清楚,可以提供帮助!

I hope it was clear and can help!

这篇关于JavaFx如何避免创建一个巨大的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆