读书人

写一个登陆框 用户跟 密码由记事本中读

发布时间: 2011-12-25 23:21:20 作者: rapoo

写一个登陆框 用户和 密码由记事本中读取
记事本中的用户和密码是用空格隔开的
比如
admin 123
jTextField1如何进入记事本中读取用户
jPasswordField1怎么进去读取密码呢
请高手们说详细下,小人学JAVA不久,


[解决办法]
用io流读取txt内容
[解决办法]
ini
[解决办法]
Properties props = new Properties();
props.load(new FileInputStream( "filename.properties "));
String Id= props.getProperty( "userId ");
String Password= props.getProperty( "passWord ");
filename.properties格式为
userId=root;
password=111111;
如果LZ有多个用户的话,建议用DOM4J解决
try{
SAXReader reader = new SAXReader();
Document document = reader.read( "c:/Demo.txt ");
Element root = document.getRootElement();
for ( Iterator node = root.elementIterator(); node.hasNext(); ) {

// CompanyBean a=new CompanyBean();

Element element = (Element) node.next();
Attribute attribute = (Attribute) element.attribute( "value ");
a.setBirthday(element.elementText( "birthday "));
a.setDepartment((String)attribute.getData());
a.setName(element.elementText( "name "));
a.setSalary(element.elementText( "salary "));
System.out.println(a.getBirthday()+ "B "+a.getDepartment()+ "D "+a.getName()+ " N "+a.getSalary()+ " S ");
list.add(a);

}
}catch(Exception e){System.out.println(e.getMessage());}
c:/Demo.txt的格式为XML
<?xml version= "1.0 " encoding= "UTF-8 "?>
<UserList>
<User>
ID..
PASSWORD..
</User>

<User>
ID..
PASSWORD..
</User> </UserList>

[解决办法]
你的要求是 swing界面读取文本框的值吧。是的话,我做了一个。
a.txt格式:(将a.txt建立在此项目中)
admin 1234
aaa 123
ccc 456
ddd 5555

以下是 代码部分

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

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 Login extends JFrame implements ActionListener {

private JTextField txtName = null;
private JPasswordField txtPwd = null;
private JLabel lblName = new JLabel( "姓名: ");
private JLabel lblPwd = new JLabel( "密码: ");
private JPanel pMain = new JPanel();
private JButton btnLogin = new JButton( "登陆 ");
private JButton btnReadUser = new JButton( "读取新用户 ");
private int click = 0;
private int row = 0;
private JLabel lblMsg = new JLabel();
FileInputStream fin = null;

public Login()
{
txtName = new JTextField(25);
txtPwd = new JPasswordField(25);
pMain.setLayout(new FlowLayout());
pMain.add(lblName);
pMain.add(txtName);
pMain.add(lblPwd);
pMain.add(txtPwd);
pMain.setBackground(Color.white);
pMain.add(btnLogin);
pMain.add(btnReadUser);
pMain.add(lblMsg);
//--------------------默认读取第一个用户



try {
fin = new FileInputStream( "a.txt ");
int flag = 0;
while (flag != -1)
{
flag = fin.read();
txtName.setText(txtName.getText()+(char)flag);
if(flag == 32)
{
break;
}
}
while(flag != -1)
{
flag = fin.read();
if(flag == 13)
{
break;
}
txtPwd.setText(txtPwd.getText()+(char)flag);

}

fin = new FileInputStream( "a.txt ");
while (flag != -1)
{
flag = fin.read();
if(flag == 13)
{
row ++ ;
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

//---------------------------
this.getContentPane().add(pMain);
this.setSize(350, 140);
this.setTitle( "登陆 ");
this.setVisible(true);
btnLogin.addActionListener(this);
btnReadUser.addActionListener(this);

}
public static void main(String[] args) {
new Login();
}

public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(btnReadUser))
{
click ++;
try {
if(click > row)
{
lblMsg.setText( "别乱点,哪来那么多用户 ");
return ;
}
fin = new FileInputStream( "a.txt ");
int flag = 0;
for(int i=0;i <click;i++)
{
while (flag != -1)
{
flag = fin.read();
if(flag == 10)
{
break;
}
}
}
txtName.setText( " ");
txtPwd.setText( " ");
while (flag != -1)
{
flag = fin.read();
txtName.setText(txtName.getText()+(char)flag);
if(flag == 32)
{
break;
}
}
while(flag != -1)
{
flag = fin.read();

if(flag == 13 || flag == -1)
{
break;
}
txtPwd.setText(txtPwd.getText()+(char)flag);
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}

}

不管怎么说,这只是个练习,可能你用的时候会有点错误,但是正常登陆都是读取数据库的,哈哈。。。lz拿去试试看看行吗
[解决办法]
你可以先读取文件, 用readLine();
在使用String 类的 split()方法 分割出各个部分

读书人网 >J2SE开发

热点推荐