java swing 登录界面

悠悠 2023-03-14 10:45 102阅读 0赞

java swing 登录界面

      • 源代码

源代码

  • Login.java

    import javax.swing.;
    import java.awt.event.
    ;
    public class Login {

    1. public static void main(String[] args) {
    2. // 创建 JFrame 实例
    3. JFrame frame = new JFrame("登录页面");
    4. // Setting the width and height of frame
    5. frame.setSize(350, 200);
    6. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    7. /* 创建面板,这个类似于 HTML 的 div 标签 * 我们可以创建多个面板并在 JFrame 中指定位置 * 面板中我们可以添加文本字段,按钮及其他组件。 */
    8. JPanel panel = new JPanel();
    9. // 添加面板
    10. frame.add(panel);
    11. /* * 调用用户定义的方法并添加组件到面板 */
    12. placeComponents(panel);
    13. // 设置界面可见
    14. frame.setVisible(true);
    15. }
    16. private static void placeComponents(JPanel panel) {
    17. /* 布局部分我们这边不多做介绍 * 这边设置布局为 null */
    18. panel.setLayout(null);
    19. // 创建 JLabel
    20. JLabel userLabel = new JLabel("用户名:");
    21. /* 这个方法定义了组件的位置。 * setBounds(x, y, width, height) * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。 */
    22. userLabel.setBounds(10,20,80,25);
    23. panel.add(userLabel);
    24. /* * 创建文本域用于用户输入 */
    25. JTextField userText = new JTextField(20);
    26. userText.setBounds(100,20,165,25);
    27. panel.add(userText);
    28. // 输入密码的文本域
    29. JLabel passwordLabel = new JLabel("密码:");
    30. passwordLabel.setBounds(10,50,80,25);
    31. panel.add(passwordLabel);
    32. /* *这个类似用于输入的文本域 * 但是输入的信息会以点号代替,用于包含密码的安全性 */
    33. JPasswordField passwordText = new JPasswordField(20);
    34. passwordText.setBounds(100,50,165,25);
    35. panel.add(passwordText);
    36. // 创建登录按钮
    37. JButton loginButton = new JButton("登录");
    38. loginButton.setBounds(10, 80, 80, 25);
    39. panel.add(loginButton);
    40. loginButton.addActionListener(new ActionListener(){
    41. @Override
    42. public void actionPerformed(ActionEvent e) {
    43. if(userText.getText().equals("admin")&&new String(passwordText.getPassword()).equals("123456")){
    44. new Index();
    45. }
    46. }
    47. });
    48. //创建取消按钮
    49. JButton cancelButton = new JButton("取消");
    50. cancelButton.setBounds(100, 80, 80, 25);
    51. panel.add(cancelButton);
    52. cancelButton.addActionListener(new ActionListener(){
    53. @Override
    54. public void actionPerformed(ActionEvent e) {
    55. System.exit(0);
    56. }
    57. });
  1. }
  2. }
  • Index.java

    import javax.swing.;
    import java.awt.event.
    ;

    public class Index {

    1. private static final int WIDTH = 300;
    2. private static final int HEIGHT = 200;
    3. public Index() {
    4. // 普通按钮控件
    5. final JFrame jf = new JFrame("首页");
    6. jf.setSize(WIDTH, HEIGHT);
    7. jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    8. jf.setVisible(true);
    9. JPanel contentPane = new JPanel();
    10. jf.setContentPane(contentPane);
    11. JButton close = new JButton("取消");
    12. contentPane.add(close);
    13. close.addActionListener(new ActionListener() {
    14. @Override
    15. public void actionPerformed(ActionEvent e) {
    16. jf.dispose();
    17. }
    18. });
    19. }
    20. public static void main(String[] args)
    21. {
    22. new Index();
    23. }

    }

发表评论

表情:
评论列表 (有 0 条评论,102人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java Swing计算器界面的实现

    在本节之前已经详细介绍了 Swing 中容器、布局管理器以及常用的基本组件。本案例将综合运用这些知识实现一个计算器的布局。在本实例中使用两种布局管理器来进行界面设计。 计算器