不对称地在JFrame中扩展两个Jpanel

时间:2010-03-15 03:33:17

标签: java jpanel

我有一个内置两个JPanel的JFrame。一个在西边,另一个在东边有BorderLayout。问题是,它只显示两个10像素宽,100%JFrame高度条: alt text http://img641.imageshack.us/img641/3298/imagewg.png

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

非常感谢。

6 个答案:

答案 0 :(得分:2)

这取决于您的确切要求。

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

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

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

然而,这并没有给你一个确切的80/20分割。我创建了一个宽度为400的splitpane。正确的组件从312开始,应该是320.所以它取决于你想要的。

如果你想要一个真正的80/20分割,那么Relative Layout是专门为此而写的。

答案 1 :(得分:1)

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

答案 2 :(得分:1)

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

您也可以使用BoxLayout(沿X轴),然后将预先调整大小的组件添加到容器中。布局更容易使用。

答案 3 :(得分:1)

我刚刚使用了MIG Layout和presto。

答案 4 :(得分:0)

这里有三个选项:

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 );
    }

}

答案 5 :(得分:-3)

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

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

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