简体   繁体   English

如何将多个元素相互对齐?

[英]How to align multiple elements below each other?

I am having difficulty in aligning Swing elements flow below each other.我很难对齐 Swing 元素在彼此下方的流动。 Using GridLayout does not help me because it divides screen to rows which have equal sizes.使用GridLayout对我没有帮助,因为它将屏幕划分为具有相同大小的行。 I need to put one component to each row and the next component should be in very bottom of the last component.我需要将一个组件放在每一行,下一个组件应该在最后一个组件的最底部。

Mi problem is that if the choices of the question are more than one line, this layout squeeze them to the rows and there is a huge gap between picture and question "3+3?"我的问题是,如果题的选择不止一行,这种布局将它们挤到行上,图片和题“3+3?”之间存在巨大的差距。

There are a number of possible solutions, probably the most useful however is GridBagLayout , while complex, it is very flexible.有许多可能的解决方案,可能最有用的是GridBagLayout ,虽然复杂,但非常灵活。

网格包布局

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

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

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

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());

            JLabel label = new JLabel("Cool");
            label.setFont(label.getFont().deriveFont(Font.BOLD, 48f));
            label.setOpaque(true);
            label.setBackground(Color.WHITE);

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;

            add(label, gbc);
            add(new JLabel("3+5="), gbc);
            add(new JCheckBox("5"), gbc);
            add(new JCheckBox("3"), gbc);
            add(new JCheckBox("6"), gbc);
            add(new JCheckBox("9"), gbc);
            add(new JCheckBox("23"), gbc);
        }

    }

}

You could even use an anchor to move the components to different positions within there cells...您甚至可以使用anchor将组件移动到单元格内的不同位置...

gbc.anchor = GridBagConstraints.WEST;

锚

These can, obviously, be applied to individual components as well, so each component can have it's own set of constraints显然,这些也可以应用于单个组件,因此每个组件都可以拥有自己的一组约束

Have a look at How to Use GridBagLayout for more details查看如何使用 GridBagLayout了解更多详细信息

You can use GridBagLayout for this.您可以为此使用GridBagLayout It is very easy to use.这是非常容易使用。 Here is a sample implementation.这是一个示例实现。

在此处输入图片说明

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

class MyFrame extends JFrame {

    public MyFrame() {
        JPanel panel = new JPanel(new GridBagLayout());

        JLabel image = new JLabel();
        image.setIcon(new ImageIcon(getClass().getResource("/image.png")));

        JLabel question = new JLabel("3 + 3 = ?");

        JRadioButton rb1 = new JRadioButton("5");
        JRadioButton rb2 = new JRadioButton("3");
        JRadioButton rb3 = new JRadioButton("6");
        JRadioButton rb4 = new JRadioButton("9");
        JRadioButton rb5 = new JRadioButton("23");

        GridBagConstraints c = new GridBagConstraints();

        c.gridx = 0;//set the x location of the grid for the next component
        c.gridy = 0;//set the y location of the grid for the next component
        panel.add(image,c);

        c.gridy = 1;//change the y location
        c.anchor=GridBagConstraints.WEST;//left align components after this point
        panel.add(question,c);

        c.gridy = 2;//change the y location
        panel.add(rb1,c);

        c.gridy = 3;//change the y location
        panel.add(rb2,c);

        c.gridy = 4;//change the y location
        panel.add(rb3,c);

        c.gridy = 5;//change the y location
        panel.add(rb4,c);

        c.gridy =6;//change the y location
        panel.add(rb5,c);

        this.getContentPane().add(panel);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.pack();
    }

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

}

References: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html参考资料: https : //docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM