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

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

最佳答案

您可以在“空”一侧添加一个“填充”组件...

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

关于java - 如何使组件尊重列和行权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16533352/

相关文章:

java - 选择对象后更新同一查询中的对象

java - JAX-RS:是否可以有一个外部可配置的@PATH?

java - 渲染空 Jtable

java - Gui 在从按钮单击事件启动线程时被阻止

android - android 中 View 的 z-index

android - 关于android :foregroundInsidePadding的信息

java - spring jsp <%%> 在页面上可见

java - Android蓝牙周期性调用inputStream和outputStream : inconsistent timestamps

java - GUI 中的循环问题 (java)

Java - 是否可以在 GridLayout 的同一侧放置两个组件?