gpt4 book ai didi

Java Swing GUI 不可见

转载 作者:行者123 更新时间:2023-11-30 03:47:33 24 4
gpt4 key购买 nike

基本上我有一个非常简单的 GUI,但由于某种原因,当我运行代码时,JButton 仅在我将鼠标悬停在它们上时才出现,而 JTextField 仅在我单击它们时才出现。我该如何解决这个问题,以便它们在程序运行时可见?谢谢。

这是我的代码:

    //JFrame + settings
JFrame frmFormSubmission = new JFrame();

frmFormSubmission.setSize(new Dimension(350, 165));
frmFormSubmission.setTitle("Form Submission - Client");
frmFormSubmission.setLocationRelativeTo(null);
frmFormSubmission.setVisible(true);
frmFormSubmission.setResizable(false);
frmFormSubmission.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//JPanel
JPanel panel = new JPanel();
frmFormSubmission.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);

//JLabels
//Name
JLabel lblName = new JLabel("Name:");
lblName.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblName.setBounds(46, 12, 40, 14);
panel.add(lblName);

//Address
JLabel lblAddress = new JLabel("Address:");
lblAddress.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblAddress.setBounds(33, 37, 53, 14);
panel.add(lblAddress);

//Phone
JLabel lblPhone = new JLabel("Phone #:");
lblPhone.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblPhone.setBounds(28, 62, 58, 14);
panel.add(lblPhone);

//Email
JLabel lblEmail = new JLabel("Email:");
lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 14));
lblEmail.setBounds(46, 87, 36, 14);
panel.add(lblEmail);

//JTextFields
//Name
nameField = new JTextField();
nameField.setBounds(93, 11, 137, 20);
panel.add(nameField);
nameField.setColumns(10);

//Address
addressField = new JTextField();
addressField.setColumns(10);
addressField.setBounds(93, 36, 137, 20);
panel.add(addressField);

//Phone
phoneField = new JTextField();
phoneField.setColumns(10);
phoneField.setBounds(93, 61, 137, 20);
panel.add(phoneField);

//Email
emailField = new JTextField();
emailField.setColumns(10);
emailField.setBounds(93, 86, 137, 20);
panel.add(emailField);

//JButtons
//Submit
JButton btnSubmit = new JButton("Submit");
btnSubmit.setBounds(240, 10, 89, 23);
panel.add(btnSubmit);

//Cancel
JButton btnCancel = new JButton("Cancel");
btnCancel.setBounds(240, 44, 89, 23);
panel.add(btnCancel);

//Flush
JButton btnFlush = new JButton("Flush");
btnFlush.setBounds(240, 76, 89, 23);
panel.add(btnFlush);

//Checkbox
JCheckBox chckbxPromotions = new JCheckBox("Email me with new and promotions!");
chckbxPromotions.setBounds(25, 108, 205, 23);
panel.add(chckbxPromotions);

非常感谢任何帮助!

最佳答案

调用 frmFormSubmission.setVisible(true); 作为您所做的最后一件事。 setVisible 将布局您的组件,以便可以正确绘制它们。如果您添加组件而不对其进行布局,那么您将遇到图形问题。

有关这意味着什么的更多信息,请查看 validate 上的文档。方法。

关于Java Swing GUI 不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25248791/

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