gpt4 book ai didi

java - 正确的 JFrame 组件,用于查看输入的数据,并在加载时正确查看布局

转载 作者:太空宇宙 更新时间:2023-11-03 23:42:48 25 4
gpt4 key购买 nike

截至目前,我的所有标签和输入字段都堆叠在 JFrame 的中心。

当我运行该程序时,“公司名称”标 checkout 现在顶部中心,在我调整窗口大小时之前什么都没有出现...

谁能告诉我为什么会这样以及如何解决它?

我还想在 JFrame 的左侧添加一个区域,当您单击“保存”时将显示联系人,我将使用哪种类型的组件来执行此操作?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import javax.swing.*;

public class Driver extends JFrame {
private JFrame f;
private JPanel p;
private JTextField fieldBN;
private JTextField fieldFN;
private JTextField fieldLN;
private JTextField fieldP;
private JTextField fieldE;
private JTextField fieldA;
private JTextField aLine2;
private JTextField fieldW;
private JLabel labelBN;
private JLabel labelFN;
private JLabel labelLN;
private JLabel labelP;
private JLabel labelE;
private JLabel labelA;
private JLabel labelW;
private JButton buttonS;

// Constructor:
public Driver() {
gui();
}

public void gui() {
f = new JFrame("Contact Book");
f.setVisible(true);
f.setSize(900,800); // default size is 0,0
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

GridBagConstraints c = new GridBagConstraints();

c.insets = new Insets(10, 10, 10, 10);

p = new JPanel(new GridBagLayout());
f.getContentPane().add(p, BorderLayout.NORTH);

c.gridx = 100;
c.gridy = 0;
labelBN = new JLabel ("Business Name");
p.add(labelBN, c);

c.gridx = 110;
c.gridy = 0;
fieldBN = new JTextField(10);
p.add(fieldBN, c);

c.gridx = 100;
c.gridy = 10;
labelFN= new JLabel ("First Name");
p.add(labelFN, c);

c.gridx = 110;
c.gridy = 10;
fieldFN = new JTextField (10);
p.add(fieldFN, c);

c.gridx = 100;
c.gridy = 20;
labelLN= new JLabel ("Last Name");
p.add(labelLN, c);

c.gridx = 110;
c.gridy = 20;
fieldLN = new JTextField (10);
p.add(fieldLN, c);

c.gridx = 100;
c.gridy = 30;
labelP = new JLabel ("Phone Number");
p.add(labelP, c);

c.gridx = 110;
c.gridy = 30;
fieldP = new JTextField (10);
p.add(fieldP, c);

c.gridx = 100;
c.gridy = 40;
labelE = new JLabel ("Email");
p.add(labelE, c);

c.gridx = 110;
c.gridy = 40;
fieldE = new JTextField (10);
p.add(fieldE, c);

c.gridx = 100;
c.gridy = 50;
labelA = new JLabel ("Address");
p.add(labelA, c);

c.gridx = 110;
c.gridy = 50;
fieldA = new JTextField (10);
p.add(fieldA, c);
c.gridx = 110;
c.gridy = 60;
aLine2 = new JTextField (10);
p.add(aLine2, c);

c.gridx = 100;
c.gridy = 70;
labelW = new JLabel ("Website");
p.add(labelW, c);

c.gridx = 110;
c.gridy = 70;
fieldW = new JTextField (10);
p.add(fieldW, c);

c.gridx = 110;
c.gridy = 80;
buttonS = new JButton("Save");
p.add(buttonS, c);

// Window Listeners
addWindowListener(new WindowAdapter() {

}
);
} // End Gui

public static void main(String[] args) {

new Driver();
} // End main Method

} // End class Driver

最佳答案

可能的原因就在这里...

public void gui() {
f = new JFrame("Contact Book");
f.setVisible(true);
f.setSize(900,800); // default size is 0,0

您正在提前调用 setVisible

简单的解决方案是在按照您想要的方式设置框架后调用 setVisible...

public void gui() {
f = new JFrame("Contact Book");
// Add everything to the frame...
// pack the frame for better cross platform support
f.pack();
// Make it visible
f.setVisible(true);
}

此外,从 JFrame 扩展然后创建 JFrame 的实例非常令人困惑。通常,您不需要从 JFrame 进行扩展,只需根据需要继续创建实例即可。

您还应该看看 Initial Threads

例如……

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new Driver();
}
});
} // End main Method

关于java - 正确的 JFrame 组件,用于查看输入的数据,并在加载时正确查看布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22394754/

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