如何使组件尊重列和行权重?

时间:2013-05-14 00:38:56

标签: java swing layout layout-manager gridbaglayout

public class Tester {

    public static class Frame extends JFrame {
        public Frame() {

            // Layout 
            GridBagLayout layout=new GridBagLayout();
            layout.columnWeights=new double[] { 0.5, 0.5 };
            layout.rowWeights=new double[] { 1 };

            // Frame                
            setLayout(layout);
            setSize(500,500);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            // Constraints
            GridBagConstraints c=new GridBagConstraints();
            c.fill=GridBagConstraints.BOTH;
            // Panel 1
            JPanel p1=new JPanel();
            p1.setBackground(Color.green);
            c.gridx=0;
            c.gridy=0;
            add(p1,c);

            // Panel 2
            JLabel l1=new JLabel("TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST" +
                                 "TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST");
            l1.setBackground(Color.yellow);
            c.gridx=1;
            c.gridy=0;
            add(l1,c);
        }
    }

    public static void main(String[] args) {
        new Frame().setVisible(true);   
    }
}

在这种情况下,l1占据了整个空间,我希望它占一半,正如这个说: layout.columnWeights=new double[] { 0.5, 0.5 };

我放c.fill=GridBagConstraints.BOTH;因为我想要:如果框架调整大小,我也希望调整组件的大小,但要占用最多50%的空间。

2 个答案:

答案 0 :(得分:4)

您可以在“空”方面添加“填充”组件......

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout24 {

    public static void main(String[] args) {
        new TestLayout24();
    }

    public TestLayout24() {

        EventQueue.invokeLater(
                        new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                GridBagConstraints gbc = new GridBagConstraints();
                gbc.weightx = 0.5f;
                gbc.weighty = 0.1f;
                gbc.fill = GridBagConstraints.BOTH;

                JPanel left = new JPanel();
                left.setBackground(Color.RED);

                JPanel right = new JPanel();

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                frame.add(left, gbc);
                frame.add(right, gbc);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

}

您可以使用GridLayout代替......

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout24 {

    public static void main(String[] args) {
        new TestLayout24();
    }

    public TestLayout24() {

        EventQueue.invokeLater(
                        new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JPanel left = new JPanel();
                left.setBackground(Color.RED);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridLayout(0, 2));
                frame.add(left);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

答案 1 :(得分:-1)

我弄明白了,为标签设置这个,它让你的生活更轻松!!!!

l1.setMinimumSize(new Dimension(0,0));
l1.setPreferredSize(new Dimension(0, 0));

感谢您的帮助...