Java 正确设计具有网格布局的面板
我有GridBagLayout,在这里我添加了一个JLabel,一个JTextfield。但它的范围是不可预测的 来源 输出 问题陈述 我原以为它会像下图一样出现,但事实并非如此。我想我在来源上有问题 问题:Java 正确设计具有网格布局的面板,java,swing,jpanel,layout-manager,gridbaglayout,Java,Swing,Jpanel,Layout Manager,Gridbaglayout,我有GridBagLayout,在这里我添加了一个JLabel,一个JTextfield。但它的范围是不可预测的 来源 输出 问题陈述 我原以为它会像下图一样出现,但事实并非如此。我想我在来源上有问题 问题: 我关于gridbaglayout的资料已经很好了吗?如何正确设计它 您可以使用嵌套的JPanel创建这样的GUI。每个JPanel都可以使用最适合特定JPanel的布局管理器 以下是GUI: 我创建了一个主JPanel来容纳所有的下级JPanel。主JPanel使用BoxLayout
我关于gridbaglayout的资料已经很好了吗?如何正确设计它 您可以使用嵌套的JPanel创建这样的GUI。每个JPanel都可以使用最适合特定JPanel的布局管理器 以下是GUI: 我创建了一个主JPanel来容纳所有的下级JPanel。主JPanel使用BoxLayout,即页面方向 保存标题的JPanel使用FlowLayout 保存学生信息的JPanel使用GridBagLayout 保存MP信息的JPanel使用不同的网格布局 持有submit按钮的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;
}
}
}
您可以使用嵌套的JPanel创建这样的GUI。每个JPanel都可以使用最适合特定JPanel的布局管理器 以下是GUI: 我创建了一个主JPanel来容纳所有的下级JPanel。主JPanel使用BoxLayout,即页面方向 保存标题的JPanel使用FlowLayout 保存学生信息的JPanel使用GridBagLayout 保存MP信息的JPanel使用不同的网格布局 持有submit按钮的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;
}
}
}
也许你可以展示你想要的输出的图像?1)为了更快地获得更好的帮助,发布一个或。2) 以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更宽和更高的图形。@user3437460uploaded@AndrewThompson但我不在那一层,当我打开链接“我真的不明白”时,我真的不明白。我可以解释任何你不明白的事情,因为这两份文件都是我写的。你还不明白什么?具体一点。也许你可以展示你想要的输出的图像?1)为了更快地获得更好的帮助,请发布一个或。2) 以最小尺寸提供ASCII艺术或GUI预期布局的简单绘图,如果可以调整大小,则提供更宽和更高的图形。@user3437460uploaded@AndrewThompson但我不在那一层,当我打开链接“我真的不明白”时,我真的不明白。我可以解释任何你不明白的事情,因为这两份文件都是我写的。你还不明白什么?具体点。非常感谢您,先生!非常感谢,先生!