Skip to content

Instantly share code, notes, and snippets.

@prem0862
Created May 18, 2018 18:23
Show Gist options
  • Save prem0862/1acfa4d76bcf76470de2b352c0dcf343 to your computer and use it in GitHub Desktop.
Save prem0862/1acfa4d76bcf76470de2b352c0dcf343 to your computer and use it in GitHub Desktop.
public class LoginFrame {
public static void main(String []args) {
JFrame frame = new JFrame("Login");
frame.setSize(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
placeComponents(frame);
frame.setVisible(true);
}
private static void placeComponents(JFrame jFrame) {
GroupLayout layout = new GroupLayout(jFrame.getContentPane());
jFrame.getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JLabel userLabel = new JLabel("User Name");
JTextField userText = new JTextField(20);
JLabel passwordLabel = new JLabel("Password");
JPasswordField passwordText = new JPasswordField(20);
JButton loginButton = new JButton("Submit");
JButton cancelButton = new JButton("Cancel");
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(userLabel)
.addComponent(passwordLabel)
.addComponent(cancelButton))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING))
.addComponent(userText)
.addComponent(passwordText)
.addComponent(loginButton)
);
// we would like the buttons to be always the same size
layout.linkSize(SwingConstants.HORIZONTAL, cancelButton, loginButton);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(userLabel)
.addComponent(userText))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(passwordLabel)
.addComponent(passwordText))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(cancelButton)
.addComponent(loginButton))
);
jFrame.pack();
jFrame.setLocationRelativeTo(null);
jFrame.setResizable(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment