开发者

java swing GridBagLayout space left

I am stuck with java GridBagLayout. Here is only one issue left. I want the middle panel ("Testing Label 2") take all vertical spaces left.

How to achieve this ?

Thanks.

 package com;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

public class Test1 {
    private void _panel1(JPanel panel){
        panel.setBorder(new LineBorder(Color.RED));

        panel.setLayout(new GridBagLayout());

        JPanel panel1 = new JPanel();
        panel1.add(new JLabel(" Testing Label 1 "));

        panel1.setBorder(new TitledBorder("P1"));
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTH;
        c.gridx = 0;
        c.gridy = 0;
        c.weightx = 1.0;
        c.weighty = 0.0;    

        panel.add(panel1, c);

        JPanel panel2 = new JPanel();
        panel2.add(new JLabel(" Testing Label 2"));

        panel2.setBorder(new TitledBorder("P2"));
        c = new GridBagConstraints();   
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTH;
        c.gridx = 0;
        c.gridy = 1;    
        c.weightx = 1.0;
        c.weighty = 1.0;        

        panel.add(panel2, c);

        JPanel panel3 = new JPanel();
        panel3.setBorder(new TitledBorder("P3"));
        c = new GridBagConstraints();   
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.NORTH;
        c.gridx = 0;
        c.gridy = 2;    
        c.weightx = 1.0;
        c.weighty = 0.0;

        panel3.setLayout(new GridLayout(5,1));
        for(int i=0; i<5; i++){
            panel3.add(new JButton("button "+i));
        }

        panel.add(panel3, c);   
    }
    public Test1(){
        JFrame frame = new JFrame();
        frame.setTitle("test 1");
        frame.getContentPane().setLayout(new GridLayout(1,3));

        JPanel panel1 = new JPanel();
        _panel1(panel1);

        JPanel panel2 = new JPanel();
        JPanel panel3 = new JPanel();

        frame.getContentPane().add(panel1);
        frame.getContentPane().add(panel2);
        frame.getContentPane().add(panel3);

        frame.setSize(800, 600);
        frame.setVisible(true);     
    }

   开发者_运维技巧 public static void main(String[] args) {
        Test1 t = new Test1();
    }

}


I just changed one line (27):

    final JPanel panel2 = new JPanel();
    panel2.add(new JLabel(" Testing Label 2"));

    panel2.setBorder(new TitledBorder("P2"));
    c = new GridBagConstraints();
    // You have to use BOTH for the panel to take
    // all vertical and horizontal space
    c.fill = GridBagConstraints.BOTH;

You were setting the grid constraint for panel2 to HORIZONTAL.

java swing GridBagLayout space left

Regards,


Consider changing the GridBagConstraint's fill field to BOTH for panel2:

  panel2.setBorder(new TitledBorder("P2"));
  c = new GridBagConstraints();
  c.fill = GridBagConstraints.BOTH; // *****
  c.anchor = GridBagConstraints.NORTH;

Either that or for a much simpler solution, just use a BorderLayout instead:

  panel.setBorder(new LineBorder(Color.RED));

  panel.setLayout(new BorderLayout());

  JPanel panel1 = new JPanel();
  panel1.add(new JLabel(" Testing Label 1 "));

  panel1.setBorder(new TitledBorder("P1"));
  panel.add(panel1, BorderLayout.NORTH);

  JPanel panel2 = new JPanel();
  panel2.add(new JLabel(" Testing Label 2"));

  panel2.setBorder(new TitledBorder("P2"));

  panel.add(panel2, BorderLayout.CENTER);

  JPanel panel3 = new JPanel();
  panel3.setBorder(new TitledBorder("P3"));

  panel3.setLayout(new GridLayout(5, 1));
  for (int i = 0; i < 5; i++) {
     panel3.add(new JButton("button " + i));
  }

  panel.add(panel3, BorderLayout.SOUTH);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜