Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使组件尊重列和行的权重?_Java_Swing_Layout_Layout Manager_Gridbaglayout - Fatal编程技术网

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

Java 如何使组件尊重列和行的权重?,java,swing,layout,layout-manager,gridbaglayout,Java,Swing,Layout,Layout Manager,Gridbaglayout,在这种情况下,l1占据了整个空间,我希望它占据一半,正如这个所说: layout.columnWeights=newdouble[]{0.5,0.5} 我把c.fill=GridBagConstraints.BOTH因为我想要:如果帧被调整大小,我也想要组件被调整大小,但要占用最大50%的空间。您可以在“空”侧添加一个“填充”组件 或 您可以使用GridLayout代替 import java.awt.BorderLayout; import java.awt.Color; import ja

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

我把
c.fill=GridBagConstraints.BOTH因为我想要:如果帧被调整大小,我也想要组件被调整大小,但要占用最大50%的空间。

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

或 您可以使用
GridLayout
代替

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

        });
    }

}


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

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

谢谢你的帮助

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

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

谢谢你的帮助

这实际上就是
GridBagLayout
的工作原理。它认为第二列不需要空间,因此将所有可用空间提供给第一列。您可以使用
GridLayout
代替GridLayout,因为GridLayout没有给我灵活性,我只是为这个简单的示例设置了0.5。。。在我的案例中,我使用了多个权重(0.2、0.5、0.3)。。。没有办法做到这一点?如果不在那一列中添加一个组件,是不行的……我不理解这个要求。为什么一个框架只有一个组件,而该组件只占可用空间的50%呢?那是浪费空间。如果您计划拥有多个组件,那么将多个组件添加到框架中以进行测试。我编辑了,希望您现在能够理解我所说的内容。这实际上就是
GridBagLayout
的工作原理。它认为第二列不需要空间,因此将所有可用空间提供给第一列。您可以使用
GridLayout
代替GridLayout,因为GridLayout没有给我灵活性,我只是为这个简单的示例设置了0.5。。。在我的案例中,我使用了多个权重(0.2、0.5、0.3)。。。没有办法做到这一点?如果不在那一列中添加一个组件,是不行的……我不理解这个要求。为什么一个框架只有一个组件,而该组件只占可用空间的50%呢?那是浪费空间。如果您计划拥有多个组件,那么将多个组件添加到框架中进行测试。我编辑了,希望您现在能够理解我所说的内容。