【问题标题】:How Do You Fix the alignment of GridBagLayout?如何修复 GridBagLayout 的对齐方式?
【发布时间】:2015-12-24 23:52:15
【问题描述】:

我想问一些关于 GridBagLayout 的问题,因为每当我尝试将 lblStudentID 放在 lblName 下时,它只会产生一个输出,其中它位于 lblName 的右侧

import javax.swing.*;
import java.awt.*;

public class Student extends JPanel {

    JLabel picture;
    JLabel lblStudentID = new JLabel("Student ID:");
    JLabel lblName = new JLabel("Name:");
    JLabel lblProg = new JLabel("Program:");
    JLabel lblGen = new JLabel("Gender:");
    JPanel panel = new JPanel();

    JRadioButton rbtn1 = new JRadioButton("Male");
    JRadioButton rbtn2 = new JRadioButton("Female");

    JComboBox cmbProg = new JComboBox();

    TextField txtStudentID = new TextField();
    TextField txtName = new TextField();

    GridBagConstraints gbc = new GridBagConstraints();

    public void setName() {

    }

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

        picture = new JLabel(createImageIcon("images/student.jpg"));
        picture.setSize(100, 100);
        add(picture);

        add(lblName, gbc);
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(txtName, gbc);
        gbc.gridx = 1;
        gbc.gridy = 0;
        add(lblStudentID, gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        add(txtStudentID, gbc);
        gbc.gridx = 1;
        gbc.gridy = 1;
    }

    private static void createAndShowGUI() {
        // Create and set up the window.
        JFrame frame = new JFrame("Student");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(400, 300);
        // Display the window.

        Student LoginPane = new Student();

        LoginPane.setOpaque(true); // content panes must be opaque
        LoginPane.setLayout(new FlowLayout());
        LoginPane.setBackground(Color.white);
        frame.setContentPane(LoginPane);
        frame.setVisible(true);

    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Student.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public static void main(String[] args) {
        // Schedule a job for the event-dispatching thread:
        // creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

【问题讨论】:

    标签: java swing alignment gridbaglayout


    【解决方案1】:

    首先您更改 GridBagConstraints 为时已晚。

    gbc.gridx = 0;
    gbc.gridy = 0;
    add(lblName,gbc);
    gbc.gridx = 1;
    gbc.gridy = 0;
    add(txtName,gbc);
    gbc.gridx = 0;
    gbc.gridy = 1;
    add(lblStudentID,gbc);
    gbc.gridx = 1;
    gbc.gridy = 1;
    add(txtStudentID,gbc);
    

    其次,您将布局更改回 LoginPane 的 FlowLayout。

    编辑 我已经剥离了你的代码。也许这会对你有所帮助:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.TextField;
    import java.net.URL;
    
    import javax.swing.BorderFactory;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class Student extends JPanel {
    
        private static final long serialVersionUID = 8923497527096438302L;
    
        private JLabel picture;
        private JLabel lblStudentID = new JLabel("Student ID:");
        private JLabel lblName = new JLabel("Name:");
    
        TextField txtStudentID = new TextField();
        TextField txtName = new TextField();
    
        public Student() {
            setLayout(new BorderLayout());
            setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
            setBackground(Color.WHITE);
            JPanel formPanel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            formPanel.setOpaque(false);
    
            picture = new JLabel(createImageIcon("images/student.jpg"));
            picture.setPreferredSize(new Dimension(100, 100));
            picture.setBorder(BorderFactory.createLineBorder(Color.black));
    
            gbc.fill = GridBagConstraints.BOTH;
            gbc.gridy = 0;
            formPanel.add(picture, gbc);
    
            gbc.gridy = 1;
            gbc.gridwidth = 1;
            formPanel.add(lblName, gbc);
            gbc.gridy = 1;
            gbc.weightx = 1;
            formPanel.add(txtName, gbc);
            gbc.weightx = 0;
            gbc.gridy = 2;
            formPanel.add(lblStudentID, gbc);
            gbc.gridy = 2;
            formPanel.add(txtStudentID, gbc);
    
            add(formPanel, BorderLayout.PAGE_START);
        }
    
        private static void createAndShowGUI() {
            // Create and set up the window.
            JFrame frame = new JFrame("Student");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.setSize(400, 300);
            // Display the window.
    
            new Student().setBackground(Color.white);
            frame.setContentPane(new Student());
            frame.setVisible(true);
        }
    
        protected static ImageIcon createImageIcon(String path) {
            URL imgURL = Student.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    

    【讨论】:

    • 很抱歉,我对 Java 还很陌生,无法理解您所说的内容,很抱歉给您带来的麻烦
    • 谢谢你,抱歉给您添麻烦
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2021-11-08
    • 2014-09-11
    • 2014-06-04
    相关资源
    最近更新 更多