Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package foo02;
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.Insets;
- import java.awt.event.KeyEvent;
- import javax.swing.*;
- @SuppressWarnings("serial")
- public class SignIn extends JPanel {
- int txtFieldColumns = 15;
- private JTextField loginField = new JTextField(txtFieldColumns);
- private JPasswordField passwordFieldl = new JPasswordField(txtFieldColumns);
- private JButton submitButton = new JButton("Log in");
- private JLabel loginLabel = new JLabel(" ");
- private JLabel passwordLabel = new JLabel(" ");
- public SignIn() {
- setLayout(new GridBagLayout());
- int ebGap = 5;
- setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
- add(new JLabel("Username:"), getGbc(0, 0));
- add(loginField, getGbc(1, 0));
- add(new JLabel("Password:"), getGbc(0, 1));
- add(passwordFieldl, getGbc(1, 1));
- add(submitButton, getGbc(0, 2, 2));
- add(new JLabel("Login Text: "), getGbc(0, 3));
- add(loginLabel, getGbc(1, 3));
- add(new JLabel("Password: "), getGbc(0, 4));
- add(passwordLabel, getGbc(1, 4));
- submitButton.addActionListener(e -> submit());
- submitButton.setMnemonic(KeyEvent.VK_L);
- }
- private void submit() {
- loginLabel.setText(loginField.getText());
- passwordLabel.setText(new String(passwordFieldl.getPassword()));
- }
- private GridBagConstraints getGbc(int x, int y, int width) {
- GridBagConstraints gbc = new GridBagConstraints();
- gbc.gridx = x;
- gbc.gridy = y;
- gbc.gridwidth = width;
- gbc.fill = GridBagConstraints.HORIZONTAL;
- int ins = 4;
- gbc.insets = new Insets(ins, ins, ins, ins);
- return gbc;
- }
- private GridBagConstraints getGbc(int x, int y) {
- return getGbc(x, y, 1);
- }
- public static void main(String[] args) {
- SwingUtilities.invokeLater(() -> {
- SignIn mainPanel = new SignIn();
- JFrame frame = new JFrame("GUI");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(mainPanel);
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true);
- });
- }
- }
Add Comment
Please, Sign In to add comment