你如何修复GridBagLayout的对齐方式?

时间:2015-09-27 18:02:58

标签: java swing alignment gridbaglayout

我想询问有关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();
            }
        });
    }
}

1 个答案:

答案 0 :(得分: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();
            }
        });
    }
}