gpt4 book ai didi

java - 需要帮助修复与 GUI 的对齐问题

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:50 27 4
gpt4 key购买 nike

import java.awt.*;
import javax.swing.*;

public class app extends JFrame{
public app(String title){
super(title);

GridBagLayout layout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
getContentPane().setLayout(layout);

JLabel l1 = new JLabel("First Name:");
constraints.gridx = 0;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l1, constraints);
getContentPane().add(l1);

JLabel l2 = new JLabel("Last Name:");
constraints.gridx = 0;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l2, constraints);
getContentPane().add(l2);

JLabel l3 = new JLabel("Street:");
constraints.gridx = 0;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l3, constraints);
getContentPane().add(l3);

JLabel l4 = new JLabel("City:");
constraints.gridx = 0;
constraints.gridy = 3;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l4, constraints);
getContentPane().add(l4);

JLabel l5 = new JLabel("Province:");
constraints.gridx = 0;
constraints.gridy = 4;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l5, constraints);
getContentPane().add(l5);

JLabel l6 = new JLabel("Postal Code:");
constraints.gridx = 0;
constraints.gridy = 5;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l6, constraints);
getContentPane().add(l6);

JLabel l7 = new JLabel("Home Phone:");
constraints.gridx = 2;
constraints.gridy = 0;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l7, constraints);
getContentPane().add(l7);

JLabel l8 = new JLabel("Cell Phone:");
constraints.gridx = 2;
constraints.gridy = 1;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l8, constraints);
getContentPane().add(l8);

JLabel l9 = new JLabel("Email Address:");
constraints.gridx = 2;
constraints.gridy = 2;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l9, constraints);
getContentPane().add(l9);

JLabel l10 = new JLabel("Age:");
constraints.gridx = 2;
constraints.gridy = 3;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l10, constraints);
getContentPane().add(l10);

JLabel l11 = new JLabel("Gender:");
constraints.gridx = 2;
constraints.gridy = 4;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(l11, constraints);
getContentPane().add(l11);

JTextField t1 = new JTextField();
constraints.gridx = 1;
constraints.gridy = 0;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(t1, constraints);
getContentPane().add(t1);

JTextField t2 = new JTextField();
constraints.gridx = 1;
constraints.gridy = 1;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(t2, constraints);
getContentPane().add(t2);

JTextField t3 = new JTextField();
constraints.gridx = 1;
constraints.gridy = 2;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(t3, constraints);
getContentPane().add(t3);

JTextField t4 = new JTextField();
constraints.gridx = 1;
constraints.gridy = 3;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(t4, constraints);
getContentPane().add(t4);

JTextField t5 = new JTextField();
constraints.gridx = 1;
constraints.gridy = 5;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(t5, constraints);
getContentPane().add(t5);

JTextField t6 = new JTextField();
constraints.gridx = 4;
constraints.gridy = 0;
constraints.gridwidth = 3;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(t6, constraints);
getContentPane().add(t6);

JTextField t7 = new JTextField();
constraints.gridx = 4;
constraints.gridy = 1;
constraints.gridwidth = 3;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(t7, constraints);
getContentPane().add(t7);

JTextField t8 = new JTextField();
constraints.gridx = 4;
constraints.gridy = 2;
constraints.gridwidth = 3;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(t8, constraints);
getContentPane().add(t8);

JComboBox c1 = new JComboBox();
constraints.gridx = 1;
constraints.gridy = 4;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(c1, constraints);
getContentPane().add(c1);

JSpinner s1 = new JSpinner();
constraints.gridx = 4;
constraints.gridy = 3;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(s1, constraints);
getContentPane().add(s1);

JRadioButton r1 = new JRadioButton();
constraints.gridx = 4;
constraints.gridy = 4;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(r1, constraints);
getContentPane().add(r1);

JRadioButton r2 = new JRadioButton();
constraints.gridx = 4;
constraints.gridy = 5;
constraints.gridwidth = 2;
constraints.gridheight = 1;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(r2, constraints);
getContentPane().add(r2);

JButton b1 = new JButton("Clear");
constraints.gridx = 2;
constraints.gridy = 6;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.ipadx = 5;
constraints.ipady = 5;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(b1, constraints);
getContentPane().add(b1);

JButton b2 = new JButton("Submit");
constraints.gridx = 3;
constraints.gridy = 6;
constraints.gridwidth = 3;
constraints.gridheight = 1;
constraints.ipadx = 5;
constraints.ipady = 5;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(b2, constraints);
getContentPane().add(b2);

JButton b3 = new JButton("Quit");
constraints.gridx = 6;
constraints.gridy = 6;
constraints.gridwidth = 1;
constraints.gridheight = 1;
constraints.ipadx = 5;
constraints.ipady = 5;
constraints.weightx = 0;
constraints.weighty = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.insets = new Insets(5,5,5,5);
layout.setConstraints(b3, constraints);
getContentPane().add(b3);

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(560,330);
setResizable(false);
}
public static void main(String[] args){
new app("Info App").setVisible(true);
}
}

当前图形用户界面:

Current GUI

预期的 GUI: Intended GUI

我试图以这样的方式对齐此 GridBagLayout,使左半部分的文本字段的宽度与右半部分的文本字段的宽度相等。另外,有没有办法将两侧展开以更好地贴合?谢谢。

最佳答案

您可以使用pack() Window实现的方法类是 Container 的子类类。

关于java - 需要帮助修复与 GUI 的对齐问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22140239/

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