0
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% 的空间。

4

2 回答 2

4

您可以将“填充”组件添加到“空”侧......

在此处输入图像描述

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改用...

在此处输入图像描述

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);
            }
        });
    }
}
于 2013-05-14T00:48:33.447 回答
-1

我想通了,为标签设置这个,它会让你的生活更轻松!!!!

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

感谢帮助...

于 2013-05-14T01:12:24.123 回答