gpt4 book ai didi

java - 无法准确理解 GridBagConstraints 的用途和使用方法。现状与应然

转载 作者:行者123 更新时间:2023-12-01 15:51:51 25 4
gpt4 key购买 nike

我正在摆弄 GridBagLayout。我已经理解了一些,因此能够做出这个布局。但我的现状与我应该的不匹配。这是屏幕。

原样:( As-Is :(

Should Be :s
应该是:s

我知道我必须稍微调整一下才能设置大小(setSize())。但真正棘手的是让“添加联系人”JLabel 位于顶部的中心。
等待您的回复。提前致谢。

这是我的代码

package SimpleCRUD;

import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ContactListFrame extends JFrame{
JButton Button1, Button2;
JTextField textField1, textField2, textField3;
JLabel label1, label2, label3 , label4;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints Constraint = new GridBagConstraints();

public ContactListFrame() {
super("All Contacts");
Button1 = new JButton("Add");
Button2 = new JButton("Cancel");
textField1 = new JTextField(15);
textField2 = new JTextField(15);
textField3 = new JTextField(15);
label4 = new JLabel("Add Contact");
label4.setFont (new Font("fallan", 1, 25));
label1 = new JLabel("First Name:");
label2 = new JLabel("Last Name:");
label3 = new JLabel("Phone Number:");

setLayout(layout);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(400, 200);
setResizable(false);
Constraint.fill = GridBagConstraints.NONE;
Constraint.anchor = GridBagConstraints.NORTH;
addComponent(label4, 0, 1, 1, 1);
addComponent(textField1, 1, 1, 1, 1);
addComponent(textField2, 2, 1, 1, 1);
addComponent(textField3, 3, 1, 1, 1);
addComponent(label1, 1, 0, 1, 1);
addComponent(label2, 2, 0, 1, 1);
addComponent(label3, 3, 0, 1, 1);
addComponent(Button1, 4, 0, 2, 1);
addComponent(Button2, 4, 1, 2, 1);
}

public void addComponent (Component comp, int row, int col, int width, int height){
Constraint.gridx = col;
Constraint.gridy = row;
Constraint.gridwidth = width;
Constraint.gridheight = height;
layout.setConstraints(comp, Constraint);
add(comp);
}

}

最佳答案

如果你看一下documentation of GridBagLayout ,有一个相当好的 GridBagConstraints 用法示例。

这是修改为使用 GridBagConstraints 的代码

import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class ContactListFrame extends JFrame{
JButton Button1, Button2;
JTextField textField1, textField2, textField3;
JLabel label1, label2, label3 , label4;
GridBagLayout layout = new GridBagLayout();
GridBagConstraints Constraint = new GridBagConstraints();

public ContactListFrame() {
super("All Contacts");
Button1 = new JButton("Add");
Button2 = new JButton("Cancel");
textField1 = new JTextField(15);
textField2 = new JTextField(15);
textField3 = new JTextField(15);
label4 = new JLabel("Add Contact");
label4.setFont (new Font("fallan", 1, 25));
label1 = new JLabel("First Name:");
label2 = new JLabel("Last Name:");
label3 = new JLabel("Phone Number:");

setLayout(layout);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(400, 200);
setResizable(false);
Constraint.fill = GridBagConstraints.BOTH;
Insets ins = new Insets(5, 5, 5, 5);
Constraint.insets = ins;//this does the padding
Constraint.weightx = 0.0;
Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
add(label4, Constraint);
Constraint.gridwidth = GridBagConstraints.RELATIVE;//next to last component
add(label1, Constraint);
Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
add(textField1, Constraint);
Constraint.gridwidth = GridBagConstraints.RELATIVE;//next to last component
add(label2, Constraint);
Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
add(textField2, Constraint);
Constraint.gridwidth = GridBagConstraints.RELATIVE;//next to last component
add(label3, Constraint);
Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
add(textField3, Constraint);
Constraint.gridwidth = GridBagConstraints.RELATIVE;//next to last component
add(Button1, Constraint);
Constraint.gridwidth = GridBagConstraints.REMAINDER;//end row
add(Button2, Constraint);
}

public static void main(String args[]) {
new ContactListFrame().setVisible(true);
}
}

输出看起来像这样

enter image description here

关于java - 无法准确理解 GridBagConstraints 的用途和使用方法。现状与应然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871817/

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