使两个 Jpanel 在 JFrame 中不对称展开

发布于 2024-08-24 22:53:20 字数 361 浏览 6 评论 0原文

我有一个 JFrame,里面有两个 JPanel。一个设置在西边,另一个设置在东边,带有 BorderLayout。问题是,它只显示两个 10 像素宽度、100% JFrame 高度的条带: 替代文本 http://img641.imageshack.us/img641/3298/imagewg.png< /a>

我想要做的是设置每个面板的大小,最终结果是西边的 jpanel 是 jframe 宽度的 80%,其余的 20% 是东边的。是否可以?我应该使用其他布局吗?

多谢。

I have a JFrame with two JPanels inside. One is set on west, other on east with BorderLayout. The thing is, it just shows two 10 pixel width, 100% JFrame height strips:
alt text http://img641.imageshack.us/img641/3298/imagewg.png

What i want to do is to setsize each panel having as end result that the jpanel on the west be 80% of the jframe width, the remaining 20% to the one on the east. Is it possible? Should I use another layout?

Thanks a lot.

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

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

发布评论

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

评论(6

终遇你 2024-08-31 22:53:20

这取决于您的具体要求。

使用分割窗格时,您可以将其禁用,这将阻止调整大小。

您还可以使用以下方法隐藏分隔线:

BasicSplitPaneUI ui = (BasicSplitPaneUI)splitPane.getUI();
ui.getDivider().setVisible(false);

但是,这不会为您提供精确的 80/20 分割。我创建了一个宽度为 400 的分割窗格。正确的组件从 312 开始,而应该是 320。所以这取决于你想要什么。

如果你想要真正的 80/20 分割,那么相对布局是专门为此编写的。

It depends on your exact requirement.

When using a split pane you can make it disabled which will prevent resizing.

You can also hide the divider using:

BasicSplitPaneUI ui = (BasicSplitPaneUI)splitPane.getUI();
ui.getDivider().setVisible(false);

However, this doesn't give you an exact 80/20 split. I created a splitpane of width 400. The right component started at 312 when is should be 320. So it depends what you want.

If you want a real 80/20 split then the Relative Layout was specifically written for this.

你的呼吸 2024-08-31 22:53:20

我建议尝试 GridBagLayout,您可以设置每个组件的水平空间的“权重”或优先级。

您也可以使用 BoxLayout (以及X 轴),然后将预先确定尺寸的组件添加到容器中。布局更容易使用。

I would recommend trying GridBagLayout, you can set the "weight" or priority of horizontal space per component.

You could also just go with a BoxLayout (along the X axis) and just add the pre-sized components to your container. The layout is a bit easier to work with.

娇妻 2024-08-31 22:53:20

您是否尝试过 JSplitPane,特别是使用 setDividerLocation(doubleproportionalLocation) 方法?这允许您设置封闭组件的宽度或高度的百分比。

Have you tried a JSplitPane, specifically using the setDividerLocation(double proportionalLocation) method? This allows you to set a percentage of the enclosing component's width or height.

万人眼中万个我 2024-08-31 22:53:20

我刚刚使用了 MIG Layout 和 presto。

I just used MIG Layout and presto.

怼怹恏 2024-08-31 22:53:20

这里有三个选项:

1 - 保留边框布局并在两个 JPanel 上设置首选大小。这适用于固定大小的窗口。

2 - 使用GridBagLayout:

public class TestJPanel extends JFrame {


    public TestJPanel() {

        JPanel rootPanel = new JPanel();
        rootPanel.setBackground( Color.WHITE );
        rootPanel.setPreferredSize( new Dimension( 0, 100 ) );

        rootPanel.setLayout( new GridBagLayout() );
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1;

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground( Color.BLUE );
        c.weightx = 0.2;
        c.gridx = 0;
        rootPanel.add( leftPanel, c );

        JPanel rightPanel = new JPanel();
        c.weightx = 0.8;
        c.gridx = 1;
        rightPanel.setBackground( Color.RED );
        rootPanel.add( rightPanel, c );

        this.add( rootPanel, BorderLayout.CENTER );
    }

    public static void main( String[] args ) {
        TestJPanel window = new TestJPanel();
        window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        window.setSize( 1024, 768 );
        window.setVisible( true );
    }

}

3 - 使用著名的SpringLayout。这个布局管理器确实很难掌握,但这里是一个入门者:

public class TestJPanel extends JFrame {


    public TestJPanel() {

        JPanel rootPanel = new JPanel();
        rootPanel.setBackground( Color.WHITE );
        rootPanel.setPreferredSize( new Dimension( 0, 100 ) );

        SpringLayout layout = new SpringLayout();
        rootPanel.setLayout( layout );

        SpringLayout.Constraints rootPaneCons = layout.getConstraints( rootPanel );
        rootPaneCons.setWidth( Spring.width( this ) );
        rootPaneCons.setHeight( Spring.height( this ) );

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground( Color.BLUE );
        rootPanel.add( leftPanel );
        SpringLayout.Constraints leftPaneCons = layout.getConstraints( leftPanel );
        leftPaneCons.setWidth( Spring.scale( rootPaneCons.getWidth(), .2f ) );
        leftPaneCons.setHeight( Spring.scale( rootPaneCons.getHeight(), 1 ) );

        JPanel rightPanel = new JPanel();
        rightPanel.setBackground( Color.RED );
        rootPanel.add( rightPanel );
        SpringLayout.Constraints rightPaneCons = layout.getConstraints( rightPanel );
        rightPaneCons.setX( Spring.scale( rootPaneCons.getWidth(), .2f ) );
        rightPaneCons.setWidth( Spring.scale( rootPaneCons.getWidth(), .8f ) );
        rightPaneCons.setHeight( Spring.scale( rootPaneCons.getHeight(), 1 ) );

        this.add( rootPanel, BorderLayout.CENTER );
    }

    public static void main( String[] args ) {
        TestJPanel window = new TestJPanel();
        window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        window.setSize( 1024, 768 );
        window.setVisible( true );
    }

}

Three options here:

1 - Keep the border layout and set a prefered size on the two JPanels. This will work with fixed size window.

2 - Use the GridBagLayout:

public class TestJPanel extends JFrame {


    public TestJPanel() {

        JPanel rootPanel = new JPanel();
        rootPanel.setBackground( Color.WHITE );
        rootPanel.setPreferredSize( new Dimension( 0, 100 ) );

        rootPanel.setLayout( new GridBagLayout() );
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.weighty = 1;

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground( Color.BLUE );
        c.weightx = 0.2;
        c.gridx = 0;
        rootPanel.add( leftPanel, c );

        JPanel rightPanel = new JPanel();
        c.weightx = 0.8;
        c.gridx = 1;
        rightPanel.setBackground( Color.RED );
        rootPanel.add( rightPanel, c );

        this.add( rootPanel, BorderLayout.CENTER );
    }

    public static void main( String[] args ) {
        TestJPanel window = new TestJPanel();
        window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        window.setSize( 1024, 768 );
        window.setVisible( true );
    }

}

3 - Use the famous SpringLayout. This layout manager is really hard to master but here is a starter:

public class TestJPanel extends JFrame {


    public TestJPanel() {

        JPanel rootPanel = new JPanel();
        rootPanel.setBackground( Color.WHITE );
        rootPanel.setPreferredSize( new Dimension( 0, 100 ) );

        SpringLayout layout = new SpringLayout();
        rootPanel.setLayout( layout );

        SpringLayout.Constraints rootPaneCons = layout.getConstraints( rootPanel );
        rootPaneCons.setWidth( Spring.width( this ) );
        rootPaneCons.setHeight( Spring.height( this ) );

        JPanel leftPanel = new JPanel();
        leftPanel.setBackground( Color.BLUE );
        rootPanel.add( leftPanel );
        SpringLayout.Constraints leftPaneCons = layout.getConstraints( leftPanel );
        leftPaneCons.setWidth( Spring.scale( rootPaneCons.getWidth(), .2f ) );
        leftPaneCons.setHeight( Spring.scale( rootPaneCons.getHeight(), 1 ) );

        JPanel rightPanel = new JPanel();
        rightPanel.setBackground( Color.RED );
        rootPanel.add( rightPanel );
        SpringLayout.Constraints rightPaneCons = layout.getConstraints( rightPanel );
        rightPaneCons.setX( Spring.scale( rootPaneCons.getWidth(), .2f ) );
        rightPaneCons.setWidth( Spring.scale( rootPaneCons.getWidth(), .8f ) );
        rightPaneCons.setHeight( Spring.scale( rootPaneCons.getHeight(), 1 ) );

        this.add( rootPanel, BorderLayout.CENTER );
    }

    public static void main( String[] args ) {
        TestJPanel window = new TestJPanel();
        window.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        window.setSize( 1024, 768 );
        window.setVisible( true );
    }

}
深空失忆 2024-08-31 22:53:20

我建议您使用 FreeDesign 布局,如下所示:

alt text http://sites.google.com/site/yan Chengcheok/Home/free-design-layout.PNG

Netbeans IDE 让所有这些工作变得有趣而轻松;)

I would suggest that you use FreeDesign layout, which will look like this :

alt text http://sites.google.com/site/yanchengcheok/Home/free-design-layout.PNG

Netbeans IDE makes all this job fun and easy ;)

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