gpt4 book ai didi

java - 模型 View 展示器和复合 View

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:33:22 25 4
gpt4 key购买 nike

我正在尝试在 java swing ui 应用程序中遵循 MVP(特别是被动 View )模式。

应用程序的基本设计让人想起向导控件。屏幕分为两个主要部分:

  • 一个活跃的观点。
  • 带有导航按钮的静态导航栏。

用户可以使用按钮来更改 Activity View ,但始终显示该栏。

为这个场景建模,我有一堆不同的屏幕,每个屏幕都有自己的演示器、 View 界面和 View 实现(使用 JPanel)。然后我有一个 Shell 演示器,使用 JFrame 查看 interfrace 和查看实现。这个想法是外壳将首先加载并始终显示,显示底部导航栏并为 Activity View 留出空间。 shell 演示器将允许设置当前 Activity 屏幕,有点像这样:

interface View {
}

class Presenter {
View view;

public Presenter(View view) {
this.view = view;
}

public View getView() {
return view;
}

}

interface ShellView extends View {
void setActiveView(View activeView);
}

class ShellPresenter extends Presenter {
private ShellView shellView;

public void setActivePresenter(Presenter activePresenter) {
shellView.setActiveView(activePresenter.getView());
}
}

class ShellFrame implements ShellView {
private JFrame frame;
private JPanel activePanel;
private JPanel navigationBar;

public ShellFrame() {
Container c = frame.getContentPane();
c.add(activePanel);
c.add(navigationBar);
}

public setActiveView(View activeView) {
???
}
}

问题出在 setActiveView 方法中:我不确定如何将 activeView 设置为 activePanel,当 View 界面是通用的并且因此对 JPanels 一无所知时。显然,我不希望我的演示者也了解 JPanel。

最佳答案

您能否将 View 的定义修改为:

interface View {
JComponent getContainer();
}

让每个 View 都能方便的获取 View 内容? shell 不需要知道返回的是什么 JComponent 实现。

关于java - 模型 View 展示器和复合 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2240407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com