JSplitPanel(或 JTabbedPane)有什么问题?

发布于 2024-07-27 19:40:33 字数 1432 浏览 5 评论 0原文

我希望向用户显示两个面板。 我决定将它们添加到 JTabbedPane 中。 我还想让用户同时看到它们的并排视图。 因此,我将两个面板添加到 JTabbedPane 中,然后创建了一个 JSplitPanel,如下所示:

    tabs.addTab("Align Image Points", imageControlPanel);
    tabs.addTab("Align Map Points", mapControlPanel);
    JSplitPane splitPane = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT, true, imageControlPanel,
            mapControlPanel);
    tabs.addTab("Side by side view", splitPane);

生成的 JTabbedPane 只有一个选项卡! 当我删除 JSplitPane 时,一切正常。 两个选项卡。 然后我尝试简化问题并发布在这里,我想出了这个:

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame("Test");
        JButton b1 = new JButton("First");
        JButton b2 = new JButton("Second");
        JSplitPane s = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,b1,b2);
        JTabbedPane tabs = new JTabbedPane();
        tabs.addTab("First", b1);
        tabs.addTab("Second", b2);
        tabs.addTab("Both", s);
        f.getContentPane().add(tabs);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

}

这给了我另一个问题! 它显示 3 个选项卡,但第三个选项卡的分割窗格不显示任何按钮!

所以我的问题是 JSplitPanel 有什么问题吗? 您不能让它显示已在另一个选项卡中显示的对象吗? 这个不成立。 请帮我。

(注意:我不想重复我要显示的组件,因为我希望它们是相同的引用)

I have two panels that i wish to display to the user. I decided to add them to a JTabbedPane. I also want to allow the user to have a side by side view of them both at the same time. So I added the two panels to my JTabbedPane and then i created a JSplitPanel as such :

    tabs.addTab("Align Image Points", imageControlPanel);
    tabs.addTab("Align Map Points", mapControlPanel);
    JSplitPane splitPane = new JSplitPane(
            JSplitPane.HORIZONTAL_SPLIT, true, imageControlPanel,
            mapControlPanel);
    tabs.addTab("Side by side view", splitPane);

The resulting JTabbedPane only has one tab! When i remove the JSplitPane everything works ok. Two tabs. I then tried simplifying the problem to post here and i came up with this :

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;

public class Test {

    public static void main(String[] args) {
        JFrame f = new JFrame("Test");
        JButton b1 = new JButton("First");
        JButton b2 = new JButton("Second");
        JSplitPane s = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,b1,b2);
        JTabbedPane tabs = new JTabbedPane();
        tabs.addTab("First", b1);
        tabs.addTab("Second", b2);
        tabs.addTab("Both", s);
        f.getContentPane().add(tabs);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }

}

This gave me another problem! It displays 3 tabs but the third tab's split pane displays no buttons!

So my question is What is wrong with JSplitPanel? You can't have it display objects that are already displayed in another tab? It makes no sense. Please help me.

(Note: i don't want to duplicate the components that i am about to display as i want them to be the same reference)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙幽之幻 2024-08-03 19:40:33

Swing UI 是分层的,您只能将组件添加到层次结构一次。 如果将一个组件添加到多个容器中,您将得到不可预测的结果。 您不想重复组件是正确的,但是您需要 JTabbedPane 上的侦听器来在选项卡的选择发生变化时从选项卡或 JSplitView 添加和删除每个组件。

tabs.addChangeListener( new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        // Reorganise the display based on the current tab selection.
    }
}

Swing UIs are hierarchical and you can only add a component to the hierarchy once. If you add a component to more than one container you'll get unpredictable results. You are correct to not want to duplicate the components, but you'll need a listener on the JTabbedPane to add and remove each component from the tab or the JSplitView as the selection of the tabs changes.

tabs.addChangeListener( new ChangeListener() {
    public void stateChanged(ChangeEvent e) {
        // Reorganise the display based on the current tab selection.
    }
}
凌乱心跳 2024-08-03 19:40:33

我和你有同样的问题。 我已经解决了这个问题,我将每个 GUI 作为 MVC 模式(模型-视图-控制器),控制器知道如何与 gui 组件进行迭代。

我在每个选项卡上创建了一个新的 GUI(视图)实例;但是,我为该 GUI 注入了相同的控制器实例作为构造函数参数,因为控制器知道如何处理 GUI 流和行为。

例如,

    GUIView1Controller controller1 = new GUIView1Controller();
    GUIView2Controller controller2 = new GUIView2Controller();

    // Add new instance GUI ; however , use the same instance of controller
    JSplitPane s = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,
                       new GUIView1(controller1), new GUIView2(controller2));

   JTabbedPane tabs = new JTabbedPane();
   tabs.addTab("First", new GUIView1(controller1));
   tabs.addTab("Second", new GUIView2(controller2));
   tabs.addTab("Both",  s );

GUIView1和GUIView2会将所有GUI侦听器注册到控制器,因此控制器将收到通知并为侦听器采取行动。 无论“第一个”选项卡上的 GUIView1 发生什么变化,“两个”选项卡上的 GUIView1 也会更新为“第一个”选项卡上的 GUIView1 的相同行为。

缺点是您必须在选项卡和 JSplitPane 上创建 GUIView 的新实例; 但是,控制器可以控制和共享所有 GUI 事件和行为。

我希望它有帮助。

老虎。

I had the same problem that you had. what I had resolved for this issue, I made the each GUI as MVC pattern(Model-view-controller) that controller knows how to iterative with gui components.

I created a new instance of GUIs(View) on each Tab;however, I injected the same instance of controller for that GUI as constructor parameter since the controller knows how to handle GUI flow and behaviors.

for example,

    GUIView1Controller controller1 = new GUIView1Controller();
    GUIView2Controller controller2 = new GUIView2Controller();

    // Add new instance GUI ; however , use the same instance of controller
    JSplitPane s = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,
                       new GUIView1(controller1), new GUIView2(controller2));

   JTabbedPane tabs = new JTabbedPane();
   tabs.addTab("First", new GUIView1(controller1));
   tabs.addTab("Second", new GUIView2(controller2));
   tabs.addTab("Both",  s );

GUIView1 and GUIView2 will register all GUI listeners to the controller, so the controller will be notified and take an action for the listeners. whatever the GUIView1 on "First" tab is changed, the GUIView1 on "Both" tab also is updated as the same behaviors of the GUIView1 on "First" tab.

The drawback was you have to create a new instance of the GUIView on the tab and JSplitPane; however, the controller can control and share all gui events and behaviors.

I hope it helps.

Tiger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文