实验5 组件及事件处理

一、实验目的

        学习使用组件类;学习使用布局类;学习Java的事件处理模式及各种事件的处理。

二、实验内容

        实现如图所示的注册窗口和登录窗口,在注册窗口中填写好注册信息,点击注册按钮,将切换到登录窗口,在登录窗口中输入用户名和密码,如果用户名和密码与注册时输入的一致则弹出登录成功对话框,否则弹出登录失败对话框。

        思考:修改程序,将注册信息保存到文件,登录时,输入的用户名和密码与保存的一致则弹出登录成功对话框,否则弹出登录失败对话框。

三、代码实现

//登录界面
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class LoginFrame extends JFrame {
    public LoginFrame() {
        init();
    }

    public void init() {
        JFrame frame = new JFrame("登录窗口");
        frame.setBounds(400, 100, 480, 410);
        frame.setLayout(null);
        Font font = new Font("Serief", Font.BOLD, 18);

        Label lb_username = new Label("用户名:");
        lb_username.setBounds(100, 76, 76, 20);
        lb_username.setFont(font);
        JTextField jtf_username = new JTextField();
        jtf_username.setBounds(199, 73, 127, 30);
        jtf_username.setFont(font);
        frame.add(lb_username);
        frame.add(jtf_username);

        Label lb_password = new Label("密码:");
        lb_password.setBounds(100, 143, 76, 20);
        lb_password.setFont(font);
        JPasswordField jpf_password = new JPasswordField();
        jpf_password.setBounds(199, 140, 127, 30);
        jpf_password.setFont(font);
        frame.add(lb_password);
        frame.add(jpf_password);

        JButton btn_login = new JButton("login");
        btn_login.setBounds(190, 220, 80, 40);
        btn_login.setFont(font);
        btn_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = jtf_username.getText().trim();
                String password = String.valueOf(jpf_password.getPassword());
                File file = new File("./user.txt");
                boolean result = false;
                if (file.exists()) {
                    try {
                        BufferedReader bfr = new BufferedReader(new FileReader(file));
                        String curLine = null;
                        while ((curLine = bfr.readLine()) != null) {
                            System.out.println(curLine);
                            String[] infos = curLine.split("---");
                            if (username.equals(infos[0]) && password.equals(infos[1])) {
                                result = true;
                                break;
                            }
                        }
                        bfr.close();
                        if (result) {
                            frame.setVisible(false);
                            JOptionPane.showMessageDialog(null, "登录成功!", "登录成功", JOptionPane.INFORMATION_MESSAGE);
                        } else {
                            JOptionPane.showMessageDialog(null, "登录失败!", "登录失败", JOptionPane.ERROR_MESSAGE);
                        }
                    } catch (Exception ep) {
                        ep.printStackTrace();
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "登录失败!", "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        frame.add(btn_login);

        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
//注册界面
import javafx.scene.control.ComboBox;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;

public class RegisterFrame {
    public RegisterFrame() {
        init();
    }

    public void init() {
        JFrame frame = new JFrame("注册窗口");
        frame.setLayout(new FlowLayout(FlowLayout.LEFT, 50, 30));
        frame.setBounds(400, 100, 760, 600);

        Font font = new Font("Serief", Font.BOLD, 18);

        JPanel row1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 5));
        Label lb_username = new Label("用户名:");
        lb_username.setSize(76, 20);
        lb_username.setFont(font);
        row1.add(lb_username);

        JTextField jtf_username = new JTextField();
        jtf_username.setPreferredSize(new Dimension(140, 30));
        jtf_username.setFont(font);
        row1.add(jtf_username);

        Label lb_password = new Label("密码:");
        lb_password.setSize(66, 20);
        lb_password.setFont(font);
        row1.add(lb_password);

        JPasswordField jpf_password = new JPasswordField();
        jpf_password.setPreferredSize(new Dimension(140, 30));
        jpf_password.setFont(font);
        row1.add(jpf_password);

        JPanel row2 = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 5));

        Label lb_sex = new Label("性别:");
        lb_sex.setSize(66, 20);
        lb_sex.setFont(font);
        row2.add(lb_sex);

        ButtonGroup rbtgp = new ButtonGroup();
        JRadioButton rb1 = new JRadioButton("男");
        rb1.setFont(font);
        rb1.setSelected(true);
        JRadioButton rb2 = new JRadioButton("女");
        rb2.setFont(font);
        rbtgp.add(rb1);
        rbtgp.add(rb2);

        row2.add(rb1);
        row2.add(rb2);

        Label lb_hobby = new Label("爱好:");
        lb_hobby.setSize(66, 20);
        lb_hobby.setFont(font);
        row2.add(lb_hobby);
        JCheckBox cb1 = new JCheckBox("音乐");
        cb1.setFont(font);
        JCheckBox cb2 = new JCheckBox("国内旅游");
        cb2.setFont(font);
        JCheckBox cb3 = new JCheckBox("阅读");
        cb3.setFont(font);

        row2.add(cb1);
        row2.add(cb2);
        row2.add(cb3);

        JPanel row3 = new JPanel(new FlowLayout(FlowLayout.LEFT, 20, 5));
        Label lb_job = new Label("职业:");
        lb_job.setSize(66, 20);
        lb_job.setFont(font);
        row3.add(lb_job);

        JComboBox<String> cb_job = new JComboBox<>();
        cb_job.setFont(font);
        cb_job.setEnabled(true);
        cb_job.addItem("学生");
        cb_job.addItem("企业员工");
        cb_job.addItem("教师");
        row3.add(cb_job);

        Label lb_note = new Label("Note:");
        lb_note.setSize(66, 20);
        lb_note.setFont(font);
        row3.add(lb_note);

        JTextArea ta_note = new JTextArea(10,10);
        ta_note.setFont(font);
        row3.add(ta_note);

        JButton btn_register = new JButton("register");
        btn_register.setFont(font);
        btn_register.setPreferredSize(new Dimension(130, 35));
        btn_register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = jtf_username.getText().trim();
                String password = String.valueOf(jpf_password.getPassword()).trim();
                if (username.equals("") || password.equals("")) {
                    JOptionPane.showMessageDialog(null, "用户名或密码不能为空!", "注册失败", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                try {
                    File file = new File("./user.txt");
                    // 创建文件
                    file.createNewFile();
                    //将用户信息写入文件中
                    FileWriter fw = new FileWriter(file, true);
                    String content = username + "---" + password + "\n";
                    fw.write(content);
                    fw.close();

                    // 跳转到登录界面
                    frame.setVisible(false);
                    new LoginFrame();
                } catch (Exception ep) {
                    ep.printStackTrace();
                }
            }
        });
        row3.add(btn_register);

        frame.add(row1);
        frame.add(row2);
        frame.add(row3);

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);

    }
}
//主程序
public class Test5 {
    public static void main(String[] args) {
        new RegisterFrame();
    }
}

四、运行结果

 

 

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值