设计面板与GridBagLayout正确

时间:2016-03-22 18:34:30

标签: java swing jpanel layout-manager gridbaglayout

我有GridBagLayout,我添加了一个JLabel,一个JTextfield。但它出现了不可预测的范围

来源

public void siswa(){ 
    panel_siswa = new JPanel(); //The Panel
    panel_siswa.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.insets = new Insets(0, 0, 0, 0);
    gbc.anchor = GridBagConstraints.CENTER;
    label = new JLabel("CEK NILAI");
    label.setFont(new Font("Arial", Font.BOLD, 18));
    label_id = new JLabel("ID :");
    label_name = new JLabel("Name :");
    label_id2 = new JLabel("");
    label_name2 = new JLabel("");
    label_semester = new JLabel("Semester :");
    label_semester2 = new JLabel("");
    label_jurusan = new JLabel("Jurusan :");
    label_jurusan2 = new JLabel("");
    label_nilai1 = new JLabel(MP1);
    label_nilai2 = new JLabel(MP2);
    label_nilai3 = new JLabel(MP3);
    label_nilai4 = new JLabel(MP4);
    label_nilai5 = new JLabel(MP5);
    tf_nilai1 = new JTextField();
    tf_nilai2 = new JTextField();
    tf_nilai3 = new JTextField();
    tf_nilai4 = new JTextField();
    tf_nilai5 = new JTextField();

    send = new JButton("Send to my email");

    gbc.weightx = 0.0;
    gbc.gridwidth = 4;
    gbc.gridx = 1;
    gbc.gridy = 0;
    panel_siswa.add(label,gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    panel_siswa.add(label_id,gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 1;
    panel_siswa.add(label_id2,gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 1;
    panel_siswa.add(label_jurusan,gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 4;
    gbc.gridy = 1;
    panel_siswa.add(label_jurusan2,gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 0;
    gbc.gridy = 2;
    panel_siswa.add(label_name, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 2;
    panel_siswa.add(label_name2, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 2;
    panel_siswa.add(label_semester,gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 4;
    gbc.gridy = 2;
    panel_siswa.add(label_semester2,gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 4;
    panel_siswa.add(label_nilai1, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 4;
    panel_siswa.add(tf_nilai1, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 5;
    panel_siswa.add(label_nilai2, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 5;
    panel_siswa.add(tf_nilai2, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 6;
    panel_siswa.add(label_nilai3, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 6;
    panel_siswa.add(tf_nilai3, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 7;
    panel_siswa.add(label_nilai4, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 7;
    panel_siswa.add(tf_nilai4, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 1;
    gbc.gridy = 8;
    panel_siswa.add(label_nilai5, gbc);
    gbc.weightx = 0.5;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.gridx = 3;
    gbc.gridy = 8;
    panel_siswa.add(tf_nilai5, gbc);
    gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridy++;
    panel_siswa.add(send);
}
}

输出

enter image description here

问题陈述

我原以为它会像下面的图片一样出现,但不是。我想我的来源有问题。

enter image description here

问题

我的关于gridbaglayout的来源已经很棒?如何正确设计?

1 个答案:

答案 0 :(得分:2)

使用嵌套的JPanels创建这样的GUI。每个JPanel都可以使用最适合特定JPanel的布局管理器。

这是GUI:

Student Data Editor

我创建了一个主JPanel来保存所有从属JPanel。主JPanel使用BoxLayout,页面方向。

持有标题的JPanel使用FlowLayout。

保存学生信息的JPanel使用GridBagLayout。

持有MP信息的JPanel使用不同的 GridBagLayout。

持有提交按钮的JPanel使用FlowLayout。

这是代码。这就是解决方案的简短,自包含,可运行示例。

package com.ggl.testing;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class StudentDataEditor implements Runnable {

    private static final Insets normalInsets = new Insets(10, 10, 0, 10);
    private static final Insets topInsets = new Insets(30, 10, 0, 10);

    private Student student;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new StudentDataEditor());
    }

    public StudentDataEditor() {
        this.student = new Student("00000017108", "Sutandi",
                "Information Systems", 2);
    }

    @Override
    public void run() {
        JFrame frame = new JFrame("Student Data Editor");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(createMainPanel());
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createMainPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));

        panel.add(createTitlePanel());
        panel.add(createStudentPanel());
        panel.add(createMPPanel());
        panel.add(Box.createVerticalStrut(30));
        panel.add(createEmailPanel());
        panel.add(Box.createVerticalStrut(10));

        return panel;
    }

    private JPanel createTitlePanel() {
        JPanel panel = new JPanel();

        JLabel titleLabel = new JLabel("CEK NILAI");
        titleLabel.setFont(titleLabel.getFont().deriveFont(24F));

        panel.add(titleLabel);

        return panel;
    }

    private JPanel createStudentPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        int gridy = 0;

        JLabel idLabel = new JLabel("ID:");
        addComponent(panel, idLabel, 0, gridy, 1, 1, topInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField idTextField = new JTextField(15);
        idTextField.setEditable(false);
        idTextField.setText(student.getId());
        addComponent(panel, idTextField, 1, gridy, 1, 1, topInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel jurusanLabel = new JLabel("Jurusan:");
        addComponent(panel, jurusanLabel, 2, gridy, 1, 1, topInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField jurusanTextField = new JTextField(15);
        jurusanTextField.setEditable(false);
        jurusanTextField.setText(student.getJurusan());
        addComponent(panel, jurusanTextField, 3, gridy++, 1, 1, topInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel nameLabel = new JLabel("Name:");
        addComponent(panel, nameLabel, 0, gridy, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField nameTextField = new JTextField(15);
        nameTextField.setEditable(false);
        nameTextField.setText(student.getName());
        addComponent(panel, nameTextField, 1, gridy, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel semesterLabel = new JLabel("Semester:");
        addComponent(panel, semesterLabel, 2, gridy, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField semesterTextField = new JTextField(15);
        semesterTextField.setEditable(false);
        semesterTextField.setText(Integer.toString(student.getSemester()));
        addComponent(panel, semesterTextField, 3, gridy++, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        return panel;
    }

    private JPanel createMPPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());

        int gridy = 0;

        JLabel mp1Label = new JLabel("MP1");
        addComponent(panel, mp1Label, 0, gridy, 1, 1, topInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField mp1TextField = new JTextField(25);
        addComponent(panel, mp1TextField, 1, gridy++, 1, 1, topInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel mp2Label = new JLabel("MP2");
        addComponent(panel, mp2Label, 0, gridy, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField mp2TextField = new JTextField(25);
        addComponent(panel, mp2TextField, 1, gridy++, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel mp3Label = new JLabel("MP3");
        addComponent(panel, mp3Label, 0, gridy, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField mp3TextField = new JTextField(25);
        addComponent(panel, mp3TextField, 1, gridy++, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel mp4Label = new JLabel("MP4");
        addComponent(panel, mp4Label, 0, gridy, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField mp4TextField = new JTextField(25);
        addComponent(panel, mp4TextField, 1, gridy++, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JLabel mp5Label = new JLabel("MP5");
        addComponent(panel, mp5Label, 0, gridy, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        JTextField mp5TextField = new JTextField(25);
        addComponent(panel, mp5TextField, 1, gridy++, 1, 1, normalInsets,
                GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);

        return panel;
    }

    private JPanel createEmailPanel() {
        JPanel panel = new JPanel();

        JButton submitButton = new JButton("Send to my email");

        panel.add(submitButton);

        return panel;
    }

    private void addComponent(Container container, Component component,
            int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
            int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                gridwidth, gridheight, 0.0D, 0.0D, anchor, fill, insets, 0, 0);
        container.add(component, gbc);
    }

    public class Student {
        private final int semester;

        private final String id;
        private final String name;
        private final String jurusan;

        public Student(String id, String name, String jurusan, int semester) {
            this.id = id;
            this.name = name;
            this.jurusan = jurusan;
            this.semester = semester;
        }

        public int getSemester() {
            return semester;
        }

        public String getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public String getJurusan() {
            return jurusan;
        }

    }

}