【发布时间】:2014-09-14 21:33:54
【问题描述】:
我正在制作一个示例应用程序,只是为了学习基础知识。我有一个问题,我有 3 个JTextFields,当我启动应用程序时,底部的 2 个JTextFields 没有出现,但第一个有 focus 的却出现了。但只有我打算的最终大小的一小部分。但是,当我单击它们或开始输入字段时,它们会扩展为我最初想要的大小。
尽管它们在启动时显示不正确,但它们都位于正确的位置。有什么想法吗?
package password;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class password implements ActionListener{
static int width = 220, height = 250;
JPanel textPanel, panelForTextFields, completionPanel;
JLabel serviceLabel, usernameLabel, passwordLabel;
JTextField serviceField, usernameField, passwordField;
JButton Submit;
public JPanel setupPane (){
// We create a bottom JPanel to place everything on.
JPanel mainPanel = new JPanel();
mainPanel.setLayout(null);
textPanel = new JPanel();
textPanel.setLayout(null);
textPanel.setLocation(0, 0);
textPanel.setSize(width, height);
mainPanel.add(textPanel);
panelForTextFields = new JPanel();
panelForTextFields.setLayout(null);
panelForTextFields.setLocation(0, 0);
panelForTextFields.setSize(width, height);
mainPanel.add(panelForTextFields);
//---------------------------------------------------------------------------------------------------------------------------------
// Service text field and label
//---------------------------------------------------------------------------------------------------------------------------------
serviceLabel = new JLabel("Service:");
serviceLabel.setLocation(60, 0);
serviceLabel.setSize(80, 40);
textPanel.add(serviceLabel);
serviceField = new JTextField();
serviceField.setLocation(60, 30);
serviceField.setSize(100, 20);
panelForTextFields.add(serviceField);
//---------------------------------------------------------------------------------------------------------------------------------
// Username text field and label
//---------------------------------------------------------------------------------------------------------------------------------
usernameLabel = new JLabel("Username:");
usernameLabel.setLocation(60, 45);
usernameLabel.setSize(80, 40);
textPanel.add(usernameLabel);
usernameField = new JTextField();
usernameField.setLocation(60, 75);
usernameField.setSize(100, 20);
panelForTextFields.add(usernameField);
//---------------------------------------------------------------------------------------------------------------------------------
// Password text field and label
//---------------------------------------------------------------------------------------------------------------------------------
passwordLabel = new JLabel("Password:");
passwordLabel.setLocation(60, 90);
passwordLabel.setSize(80, 40);
textPanel.add(passwordLabel);
passwordField = new JTextField();
passwordField.setLocation(60, 120);
passwordField.setSize(100, 20);
panelForTextFields.add(passwordField);
//---------------------------------------------------------------------------------------------------------------------------------
// Submit button
//---------------------------------------------------------------------------------------------------------------------------------
Submit = new JButton("Submit");
Submit.setLocation(60, 165);
Submit.setSize(100, 20);
panelForTextFields.add(Submit);
Submit.addActionListener(this);
return mainPanel;
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == Submit) {
JOptionPane.showMessageDialog(null," information added.","Success!", JOptionPane.PLAIN_MESSAGE);
}
}
private static void password() {
JFrame mainF = new JFrame("Password Application");
password demo = new password();
mainF.setContentPane(demo.setupPane());
mainF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainF.setSize(width, height);
mainF.setResizable(false);
mainF.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
password();
}
});
}
}
【问题讨论】:
-
罪魁祸首是这里的
AbsoluteLayout,其他没有为有问题的JTextField提供列。做JTextField tField = new JTextField(10),这足以设置它的大小:-) -
要获得布局方面的帮助,请提供 ASCII 艺术或简单的图形,说明 GUI 应如何以默认大小显示,并且(如果可调整大小)具有额外的宽度/高度。
标签: java swing jtextfield layout-manager null-layout-manager