异常与File类实战
来源:互联网 发布:aws与阿里云差别 编辑:程序博客网 时间:2024/06/10 12:20
用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
- 异常与File类实战
- 19. 异常处理与File类
- 异常、file类
- day19<异常&File类>
- 异常和File类
- Day19 --异常 File类
- 黑马程序员——JAVA之异常与File类
- jvm之OutOfMemoryError异常与实战
- JVM(八)OutOfMemoryError异常与实战
- 20 Exception异常/File类
- 异常体系和File类
- 异常,file类(过滤器),递归
- Java内存区域与内存溢出异常-异常实战
- 异常#File
- 黑马程序员——Java编程语言学习总结 –异常与File类
- 黑马程序员_IO流之异常处理机制与File类
- Java基础学习第二十天——异常与FILE类
- 异常类-File类-IO流
- 如何生成SSH key及访问Github
- 求n个数中与某一个元素互质的整数个数
- 在spark集群中通过hive访问mysql数据库
- Java根据某一字段合并两个 Excel文件
- 2016CDA杯大数据竞赛排行榜出炉 人大统计学院摘得桂冠!
- 异常与File类实战
- MAT这个工具很强大,内存分析,以后用用(java特种兵172页)
- 51nod 1005 大数加法
- 机器学习基本概念
- Java反射小练习
- C语言中,malloc和free深入理解
- Spark分析窗口函数
- CRC循环冗余校验码的基本原理-笔记
- btn延迟问题