I am making an example application just to learn basics. I am having a problem where I have 3 JTextField
s and when I launch the application the bottom 2 JTextField
s do not show up, but the first one which has the focus, does. But only a fraction of the final size i intend. When I click on them however or begin to type in the fields they expand to the size i originally intended.
They are all in the correct location though they are showing up incorrectly at launch. Any ideas?
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();
}
});
}
}