jframe = new JFrame("Admin");
jpanel = new JPanel();
jpanel.setLayout(new FlowLayout());
jframe.add(jpanel);
userLabel = new JLabel("Username:");
jpanel.add(userLabel);
userLabel.setBounds(100, 100, 30, 30);
userTxtfield = new JTextField(15);
jpanel.add(userTxtfield);
passwordTxtfield = new JTextField(15);
jpanel.add(userTxtfield);
jpanel.add(passwordTxtfield);
passwordLabel = new JLabel("Password:");
jpanel.add(passwordLabel);
userLabel.setBounds(100, 100, 30, 30);
loginButton= new JButton("Login");
jpanel.add(loginButton);
jframe.pack();
jframe.setLocationRelativeTo(null);
jframe.setSize(350,350);
jframe.setVisible(true);
I am trying to make a neat login area for my program but I can't seem to get the two labels and text fields to line up with the button underneath. Is there a better way of doing this positioning of the components within this JFrame
?
Use a
JOptionPane
to display a log-in dialog. One way to layout the components is to use aGridBagLayout
.The code that produces that is:
Here is the complete example (an MCVE, as mentioned above).