gpt4 book ai didi

java - 正确使用 GridBagLayout 设计面板

转载 作者:行者123 更新时间:2023-12-02 03:47:45 26 4
gpt4 key购买 nike

我有 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 的资料已经很棒了吗?如何正确设计?

最佳答案

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

这是图形用户界面:

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;
}

}

}

关于java - 正确使用 GridBagLayout 设计面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com