JEdi​​torPane 阻塞了 JSplitPane 的分隔线

发布于 2025-01-11 05:58:18 字数 1335 浏览 3 评论 0原文

在我的应用程序中,我使用 JSplitPane 将 GUI 分为两侧。每一侧都将包含一些组件,其中之一是 JEditorPane。这些组件水平相邻放置。除了 JEditorPanel 之外,所有组件都应具有固定大小。这应该占用所有可用的水平空间。因此,如果 splitPane 的分隔线移动,JEditorPane 应更改其宽度。如果分隔线朝 JSplitPane 增加的方向移动,我当前的实现工作正常。然而,不可能沿另一个方向移动分隔器。下面是我的问题的一个简单示例。在示例中,我使用 GridbagLayout。然而,对于其他布局管理器来说,这个问题也仍然存在。同样有趣的是,这个问题似乎是 JEditorPane 所独有的。当 JEditorPane 被替换(例如被 JButton)替换时,实现工作正常。 这是我的代码:

import javax.swing.*;
import java.awt.*;


public class Test {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panelRight = new JPanel();
        JPanel panelLeft = new JPanel();

        panelRight.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JButton b = new JButton("button");
        c.gridx = 1;
        c.gridy = 0;
        panelRight.add(b, c);

        JEditorPane ep = new JEditorPane();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        panelRight.add(ep, c);

        JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLeft, panelRight);
        sp.setResizeWeight(0.5);

        f.add(sp);
        f.setVisible(true);
    }
}

有谁知道为什么会发生这种情况和/或如何解决它?

In my application I'm using a JSplitPane to devide the GUI into two sides. Every side will contain some components, one of them being a JEditorPane. The components are placed horizontally next to each other. All components should have a fixed size exept for the JEditorPanel. This should take all the available horizontal space. So, if the divider of the splitPane is moved the JEditorPane should change its width. My current implementation works fine if the divider is moved in a direction where the with of the JSplitPane increases. However, it is not possible to move the divider in the other direction. Below is a minimalistic example of my problem. In the example I'm using a GridbagLayout. However, this problem remains also for other layout manager. Also interesting is that this problem seems to be unique for the JEditorPane. The implementation works fine when the JEditorPane is replaced, for example by a JButton.
Here is my code:

import javax.swing.*;
import java.awt.*;


public class Test {
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setSize(500, 500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panelRight = new JPanel();
        JPanel panelLeft = new JPanel();

        panelRight.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        JButton b = new JButton("button");
        c.gridx = 1;
        c.gridy = 0;
        panelRight.add(b, c);

        JEditorPane ep = new JEditorPane();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1;
        panelRight.add(ep, c);

        JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLeft, panelRight);
        sp.setResizeWeight(0.5);

        f.add(sp);
        f.setVisible(true);
    }
}

Does anyone have an idea why this is happending and/or how to fix it?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文