Java 如何将JLabel与面板左侧对齐

Java 如何将JLabel与面板左侧对齐,java,Java,我想将所有JLabel与面板左侧对齐。下面是我的代码,但它不能正常工作,我不知道为什么。 JFrame frame1=新JFrame(“登记乘客”); frame1.setVisible(true); 框架1.设置大小(550200) 根据您的示例,您可以使用 JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 但这将使所有组件向左对齐 您还可以考虑使用不同的布局管理器或布局管理器的组合? 看一看更多的想法 已更新 Flo

我想将所有JLabel与面板左侧对齐。下面是我的代码,但它不能正常工作,我不知道为什么。 JFrame frame1=新JFrame(“登记乘客”); frame1.setVisible(true); 框架1.设置大小(550200)


根据您的示例,您可以使用

JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
但这将使所有组件向左对齐

您还可以考虑使用不同的布局管理器或布局管理器的组合?

看一看更多的想法

已更新

<代码> FlowLayout <代码>(这是默认的布局管理器,用于<代码> jPanels/Cuff>)不给您很多选项,而是考虑尝试使用不同的布局管理器或布局管理器的组合,例如…

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LayoutExample {

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

    public LayoutExample() {
        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());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            gbc.gridx = 0;
            gbc.gridy = 0;

            add(new JLabel("Name:"), gbc);
            gbc.gridy++;
            add(new JLabel("Activity:"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.NONE;

            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(20), gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridwidth = 2;

            add(new JButton("Register"), gbc);

        }

    }

}


我试过你的代码,但它仍然没有改变任何事情。布局是:命名活动我的意思是活动没有进入面板的左侧面板被放置在使用面板首选大小的面板内,因此,没有更改..但是如何更改?谢谢!基本上,您不能这样做,标签已经左对齐,布局管理器正在使用组件的首选尺寸将其布局出来,因此标签不会占用所需的更多空间。最好的选择是使用不同的布局管理器非常感谢,这对我理解布局有很大帮助。
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LayoutExample {

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

    public LayoutExample() {
        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());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.anchor = GridBagConstraints.WEST;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            gbc.gridx = 0;
            gbc.gridy = 0;

            add(new JLabel("Name:"), gbc);
            gbc.gridy++;
            add(new JLabel("Activity:"), gbc);

            gbc.gridx++;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.NONE;

            add(new JTextField(10), gbc);
            gbc.gridy++;
            add(new JTextField(20), gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.anchor = GridBagConstraints.CENTER;
            gbc.gridwidth = 2;

            add(new JButton("Register"), gbc);

        }

    }

}