异常与File类实战

用File类的文件操作和异常处理来制作一个<<登录窗口>>

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class ButtonAction implements ActionListener{
    public JTextField jf;
    public JTextField jf2;
    public ButtonGroup bg;
    public String command;
    public ButtonAction(JTextField jf4,JTextField jf24,ButtonGroup bg1){
        this.jf=jf4;
        this.jf2=jf24;
        bg=bg1;
    }

    public void actionPerformed(ActionEvent e) {
        String name=jf.getText();
        String pwd=jf2.getText();

        ButtonModel cba=bg.getSelection();
        command=e.getActionCommand();
        System.out.println(command);
        if("pic1".equals(command)){
            if(checkLogin(name,pwd)){
                JOptionPane.showMessageDialog(null, "登陆成功");
            }else{
                JOptionPane.showMessageDialog(null,"登陆失败");
            }
        }else if("pic2".equals(command)){
            Login.databases.put(name, pwd);
            if(checkLogin2(name,pwd)){
                JOptionPane.showMessageDialog(null, "注册失败");

            }else{
                JOptionPane.showMessageDialog(null, "注册成功");
            //  Login.databases.
            }
        }


    }
    public boolean checkLogin2(String name,String pwd){
        if(Login.databases.containsKey(name)){
            String password=Login.databases.get(name);
            if(password.equals(pwd)){

                return false;
            }else{
                return true;
            }




        }
        return true;
    }
    public boolean checkLogin(String name,String pwd){
        if(Login.databases.containsKey(name)){
            String password=Login.databases.get(name);
            if(password.equals(pwd)){
                return true;

            }
        }
        return false;       
    }

}
package Login;

import java.awt.FlowLayout;
import java.util.HashMap;

import javafx.scene.layout.Background;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;

public class Login extends JFrame{
    public static HashMap<String,String> databases=new HashMap<String,String>();
    static{
        for(int i=0;i<10;i++){
            databases.put("user"+i,"pwd"+i);
        }
    }
    public void initment(){
        this.setSize(300,150);
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        this.setTitle("登录界面");

        JLabel jtfUser=new JLabel("账号:");
        this.add(jtfUser);

        JTextField jf=new JTextField(20);
        this.add(jf);

        JLabel jtfPwd=new JLabel("密码:");
        this.add(jtfPwd);

        JTextField jf2=new JTextField(20);
        this.add(jf2);
        ButtonGroup bg=new ButtonGroup();
        JButton button=new JButton();
        button.setText("登录");
        this.add(button);
        button.setActionCommand("pic"+1);
        bg.add(button);
        JButton button2=new JButton();
        button2.setText("注册");
        this.add(button2);
        button2.setActionCommand("pic"+2);
        bg.add(button2);

        ButtonAction ba=new ButtonAction(jf,jf2,bg);
        button.addActionListener(ba);
        button2.addActionListener(ba);


        this.setVisible(true);
    }



}
package Login;

public class test {
    public static void main(String[]args){
        Login login=new Login();
        login.initment();
    }

面板
新建
打开
保存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值