fubarable

Untitled

Jul 9th, 2022 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. package foo02;
  2.  
  3. import java.awt.GridBagConstraints;
  4. import java.awt.GridBagLayout;
  5. import java.awt.Insets;
  6. import java.awt.event.KeyEvent;
  7. import javax.swing.*;
  8.  
  9. @SuppressWarnings("serial")
  10. public class SignIn extends JPanel {
  11.     int txtFieldColumns = 15;
  12.     private JTextField loginField = new JTextField(txtFieldColumns);
  13.     private JPasswordField passwordFieldl = new JPasswordField(txtFieldColumns);
  14.     private JButton submitButton = new JButton("Log in");
  15.     private JLabel loginLabel = new JLabel("   ");
  16.     private JLabel passwordLabel = new JLabel("    ");
  17.  
  18.     public SignIn() {
  19.         setLayout(new GridBagLayout());
  20.         int ebGap = 5;
  21.         setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
  22.  
  23.         add(new JLabel("Username:"), getGbc(0, 0));
  24.         add(loginField, getGbc(1, 0));
  25.         add(new JLabel("Password:"), getGbc(0, 1));
  26.         add(passwordFieldl, getGbc(1, 1));
  27.  
  28.         add(submitButton, getGbc(0, 2, 2));
  29.  
  30.         add(new JLabel("Login Text: "), getGbc(0, 3));
  31.         add(loginLabel, getGbc(1, 3));
  32.         add(new JLabel("Password: "), getGbc(0, 4));
  33.         add(passwordLabel, getGbc(1, 4));
  34.  
  35.         submitButton.addActionListener(e -> submit());
  36.         submitButton.setMnemonic(KeyEvent.VK_L);
  37.     }
  38.  
  39.     private void submit() {
  40.         loginLabel.setText(loginField.getText());
  41.         passwordLabel.setText(new String(passwordFieldl.getPassword()));
  42.     }
  43.  
  44.     private GridBagConstraints getGbc(int x, int y, int width) {
  45.         GridBagConstraints gbc = new GridBagConstraints();
  46.         gbc.gridx = x;
  47.         gbc.gridy = y;
  48.         gbc.gridwidth = width;
  49.         gbc.fill = GridBagConstraints.HORIZONTAL;
  50.  
  51.         int ins = 4;
  52.         gbc.insets = new Insets(ins, ins, ins, ins);
  53.  
  54.         return gbc;
  55.     }
  56.  
  57.     private GridBagConstraints getGbc(int x, int y) {
  58.         return getGbc(x, y, 1);
  59.     }
  60.  
  61.     public static void main(String[] args) {
  62.         SwingUtilities.invokeLater(() -> {
  63.             SignIn mainPanel = new SignIn();
  64.  
  65.             JFrame frame = new JFrame("GUI");
  66.             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  67.             frame.add(mainPanel);
  68.             frame.pack();
  69.             frame.setLocationRelativeTo(null);
  70.             frame.setVisible(true);
  71.         });
  72.     }
  73.  
  74. }
  75.  
Add Comment
Please, Sign In to add comment