content: package me.n1ar4.gui;
  
  import com.intellij.uiDesigner.core.GridConstraints;
  import com.intellij.uiDesigner.core.GridLayoutManager;
  
  import javax.swing.*;
  import java.awt.*;
  import java.util.HashMap;
  import java.util.Map;
  
  public class DemoForm {
      private JPanel rootPanel;
      private JTextField userNameText;
      private JPasswordField passwordText;
      private JButton cleanBtn;
      private JButton registerBtn;
      private JButton loginBtn;
      private JLabel usernameLabel;
      private JLabel passwordLabel;
      private JPanel actionPanel;
      private JLabel logoLabel;
  
      private static final Map<String, String> database = new HashMap<>();
  
      private static JFrame frame;
  
      public DemoForm() {
          // 参考 IconManager.getLogoIcon()
          // 如何获得 JAR 内部的图片文件作为 GUI 界面的图片
          logoLabel.setIcon(IconManager.getLogoIcon());
  
          // 如何给按钮绑定事件
          // e->{} 本质是一个 ActionListener 实现类
          // 你可以自己实现一个 ActionListener 传到这里
          // e->{} 是一个简略的写法
          cleanBtn.addActionListener(e -> {
              // 清空输入框
              userNameText.setText("");
              passwordText.setText("");
              // 提示用户清理完成
              JOptionPane.showMessageDialog(null, "clean finish");
          });
  
          loginBtn.addActionListener(e -> {
              // 获取用户名密码
              String username = userNameText.getText();
              String password = new String(passwordText.getPassword());
              if (username == null || password.isEmpty()) {
                  // 如何弹出提示
                  JOptionPane.showMessageDialog(null, "username or password is empty");
                  return;
              }
              System.out.println("username = " + username);
              System.out.println("password = " + password);
              // 查询用户
              String userPass = database.get(username);
              if (userPass != null && userPass.equals(password)) {
                  JOptionPane.showMessageDialog(null, "login finish");
                  // 如何跳转另一个页面
                  frame.dispose();
                  IndexForm.show(username);
              } else {
                  JOptionPane.showMessageDialog(null, "username or password is incorrect");
              }
          });
  
          registerBtn.addActionListener(e -> {
              // 如何注册
              String username = userNameText.getText();
              String password = new String(passwordText.getPassword());
              if (username == null || password.isEmpty()) {
                  JOptionPane.showMessageDialog(null, "username or password is empty");
                  return;
              }
              database.put(username, password);
              JOptionPane.showMessageDialog(null, "register " + username + " finish");
          });
      }
  
      // 可以使用 Alt + Insert 快捷键打开 Generate 菜单
      // 点击 Form main() 即可根据 IDEA GUI Designer 生成 main 方法
      // 注意要至少启动一次 才可以生成下方完整的 GUI 代码
      public static void start() {
          frame = new JFrame("java-swing-gui-starter");
  
          // 参考 MenuUtil.createMenuBar()
          // 如何设置一个菜单
          frame.setJMenuBar(MenuUtil.createMenuBar());
          // 居中显示
          frame.setLocationRelativeTo(null);
          frame.setContentPane(new DemoForm().rootPanel);
          // 退出后程序完全退出
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          // 展示
          frame.pack();
          frame.setVisible(true);
      }
  
      // 下面的代码是 IDEA GUI Designer 生成的
      // ！！！不要改动！！！
  
      {
  // GUI initializer generated by IntelliJ IDEA GUI Designer
  // >>> IMPORTANT!! <<<
  // DO NOT EDIT OR ADD ANY CODE HERE!
          $$$setupUI$$$();
      }
  
      /**
       * Method generated by IntelliJ IDEA GUI Designer
       * >>> IMPORTANT!! <<<
       * DO NOT edit this method OR call it in your code!
       *
       * @noinspection ALL
       */
      private void $$$setupUI$$$() {
          rootPanel = new JPanel();
          rootPanel.setLayout(new GridLayoutManager(3, 2, new Insets(8, 8, 8, 8), -1, -1));
          usernameLabel = new JLabel();
          usernameLabel.setText("Username");
          rootPanel.add(usernameLabel, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
          passwordLabel = new JLabel();
          passwordLabel.setText("Password");
          rootPanel.add(passwordLabel, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
          userNameText = new JTextField();
          rootPanel.add(userNameText, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, new Dimension(300, -1), new Dimension(300, -1), null, 0, false));
          passwordText = new JPasswordField();
          rootPanel.add(passwordText, new GridConstraints(1, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, new Dimension(300, -1), new Dimension(300, -1), null, 0, false));
          actionPanel = new JPanel();
          actionPanel.setLayout(new GridLayoutManager(1, 4, new Insets(0, 0, 0, 0), -1, -1));
          rootPanel.add(actionPanel, new GridConstraints(2, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
          cleanBtn = new JButton();
          cleanBtn.setText("CLEAN");
          actionPanel.add(cleanBtn, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
          registerBtn = new JButton();
          registerBtn.setText("REGISTER");
          actionPanel.add(registerBtn, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
          loginBtn = new JButton();
          loginBtn.setText("LOGIN");
          actionPanel.add(loginBtn, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
          logoLabel = new JLabel();
          logoLabel.setText("");
          actionPanel.add(logoLabel, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
      }
  
      /**
       * @noinspection ALL
       */
      public JComponent $$$getRootComponent$$$() {
          return rootPanel;
      }
  
  }
  