使用 Java JSplitPane 调整控件大小

发布于 2024-12-17 21:32:24 字数 303 浏览 5 评论 0 原文

请在链接中查看我的图片,然后阅读下面的内容以获取有关我的问题的更多详细信息。

Sample image

想象一下,这是一个用 JSplitPane 一分为二的基本框架,默认情况下,当您调整框架大小时,灰色部分会发生变化它的大小,但我希望白色部分能够根据框架大小的调整而调整大小。

任何朝着正确方向的帮助将不胜感激,因为我现在正在开发一个项目,并且我正在尝试各种奇怪的东西,为我在新的一年中最大的项目做好准备。 :)

问候 塞隆

Please see my image below at the link and then read below it for more details on my problem.

Sample image

Imagine that is a Basic frame splited into two with a JSplitPane, by default when you resize your frame the gray part changes it's size, but I would like the white part to resize accordingly to the frame resizing.

Any help into the right direction would be appreciated as I am working on a project now and I am trying out all kind of weird stuff to be prepared for my biggest project set in the new year. :)

Regards
Theron

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

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

发布评论

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

评论(1

述情 2024-12-24 21:32:24

您需要使用 setResizeWeight 来让左右在 JFrame 调整大小时占用额外的空间或减小大小,示例代码如下:

    import java.awt.BorderLayout;

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

    public class TestJSplitPane {
    private void init(){
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setRightComponent(new JButton("Here I Am"));
        splitPane.setLeftComponent(new JButton("Me Too"));
        splitPane.setResizeWeight(0.5);
        frame.add(splitPane, BorderLayout.CENTER);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TestJSplitPane().init();
    }
}

Java Doc for setResizeWeight

希望这有帮助。

You need to use setResizeWeight to get the left and right take the extra space or reduce in size on JFrame resize, sample code below:

    import java.awt.BorderLayout;

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

    public class TestJSplitPane {
    private void init(){
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        splitPane.setRightComponent(new JButton("Here I Am"));
        splitPane.setLeftComponent(new JButton("Me Too"));
        splitPane.setResizeWeight(0.5);
        frame.add(splitPane, BorderLayout.CENTER);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new TestJSplitPane().init();
    }
}

Java Doc for setResizeWeight.

Hope this helps.

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