开发者

Get two Jpanel expand in a JFrame asymmetrically

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/329开发者_如何学JAVA8/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.


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.


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.


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.


I just used MIG Layout and presto.


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

}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜