java GUI和文本读取方面的问题。
发布网友
发布时间:2022-05-09 07:26
我来回答
共2个回答
热心网友
时间:2024-01-29 04:42
看看你下面代码,是个注册界面,登陆界面在另外一个问题里面给了,经测试运行无任何问题。User类 ArrayList<User>写入文件,读出来的也是ArrayList<User>。根据下面的代稍少修改就可以得到修改密码的GUI。
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Registe extends JFrame implements ActionListener{
JTextField name=new JTextField(10);
JTextField email=new JTextField(10);
JPasswordField psw=new JPasswordField(10);
JPasswordField repsw=new JPasswordField(10);
public Registe(){
setLayout(new GridLayout(6,1));
JPanel jPanel=new JPanel();
JPanel jPanel2=new JPanel();
JPanel jPanel3=new JPanel();
JPanel jPanel4=new JPanel();
JPanel jPanel5=new JPanel();
JPanel jPanel6=new JPanel();
jPanel.add(new JLabel("欢迎注册"));
add(jPanel);
jPanel2.add(new JLabel(" 用户名 :"));
jPanel2.add(name);
add(jPanel2);
jPanel5.add(new JLabel("注册邮箱:"));
jPanel5.add(email);
add(jPanel5);
jPanel3.add(new JLabel("密 码:"));
jPanel3.add(psw);
add(jPanel3);
jPanel6.add(new JLabel("确认密码"));
jPanel6.add(repsw);
add(jPanel6);
JButton jButton=new JButton("确定");
JButton jButton2=new JButton("重置");
JButton jButton3=new JButton("退出");
jButton.addActionListener(this);
jButton3.addActionListener(this);
jButton2.addActionListener(this);
jPanel4.add(jButton);
jPanel4.add(jButton2);
jPanel4.add(jButton3);
add(jPanel4);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setLocation(500, 300);
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
String string=e.getActionCommand();
if("确定".equals(string)){
String userName=name.getText();
String userEmail=email.getText();
String userPsw=new String(psw.getPassword());
String userRepsw=new String(repsw.getPassword());
int flag=1;
User user=new User(userName,userPsw,userEmail);
if(name.getText()!=null&&name.getText().length()<=5){
System.out.println("用户名太短!");
flag=0;
name.setText("");
}else if(User.isExist(user)){
flag=0;
System.out.println("该用户名已注册!");
return;
}
String s1="^[a-zA-Z0-9]+@+[a-zA-Z0-9]+\\.com|\\.net$" ;
if(!email.getText().matches(s1)){
flag=0;
email.setText("");
System.out.println("邮箱格式不正确");
}else if(User.isExist(user)){
flag=0;
email.setText("");
System.out.println("该邮箱已被注册!");
}
if(String.valueOf(psw.getPassword()).length()<6){
flag=0;
System.out.println("密码太短!");
psw.setText("");
repsw.setText("");
}else if(!(String.valueOf(repsw.getPassword())).equals(String.valueOf(psw.getPassword()))){
flag=0;
psw.setText("");
repsw.setText("");
System.out.println("两次输入密码不一致!");
}
if(flag==1){
psw.setText("");
repsw.setText("");
name.setText("");
email.setText("");
User.writeUser(user);
System.out.println("注册成功!");
}
}else if("重置".equals(string)){
psw.setText("");
repsw.setText("");
name.setText("");
email.setText("");
}else
this.dispose();
}
public static void main(String[]args){
new Registe().setTitle("注册");
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class User implements Serializable{
private String name,psw,email;
public User() {
}
public User(String name, String psw,String email) {
super();
this.setEmail(email);
this.setName(name);
this.setPsw(psw);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int hashCode(){
int type=4;
return type*2+email.hashCode()+name.hashCode();
}
@Override
public boolean equals(Object o){
if(o==null)
return false;
else if(o instanceof User){
User u=(User)o;
return this.email.equals(u.email)||this.name.equals(u.name);
}
else
return false;
}
public String toString(){
return name;
}
public static void writeUser(User user){
ArrayList<User>userList=new ArrayList<User>();
ObjectOutputStream OOS;
try {
File file=new File("C:user.txt");
if(!file.exists())
file.createNewFile();
if(file.length()!=0){
System.out.println(file.length());
userList=readUser();
}
userList.add(user);
OOS=new ObjectOutputStream(new FileOutputStream(file));;
OOS.writeObject(userList);
OOS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ArrayList<User> readUser(){
ArrayList<User>userList=new ArrayList<User>();
ObjectInputStream OIS;
try {
File file=new File("C:user.txt");
if(!file.exists())
file.createNewFile();
if(file.length()!=0){
OIS= new ObjectInputStream(new FileInputStream(file));
userList=(ArrayList<User>)OIS.readObject();
}
return userList;
} catch (IOException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
return userList;
}
public static boolean isExist(User user){
ArrayList<User>userList=readUser();
return userList.contains(user);
}
public boolean isRight(String psw){
ArrayList<User>userList=readUser();
User user=null;
for(int i=0;i<userList.size();i++){
if(name.equals(userList.get(i).getName())){
user=userList.get(i);
}
}
if(user.psw.equals(psw))
return true;
else
return false;
}
public static void main(String[]args){
User user=new User("ou","kk","youxding");
if(!user.isExist(user))
User.writeUser(user);
ArrayList<User> list=new ArrayList<User>();
list=User.readUser();
for(int i=0;i<list.size();i++){
System.out.println(list.get(i).getName());
}
}
}
热心网友
时间:2024-01-29 04:42
是不是你第一个操作的流没有关闭?
我觉得应该这样做。
程序启动时读入文件到实体类中。
程序结束时,写入实体类到文件中。
其间的修改操作,都是更改这个实体类。而不是真正的修改文件。