日期:2014-05-20  浏览次数:20696 次

写一个登陆框 用户和 密码由记事本中读取
记事本中的用户和密码是用空格隔开的
比如
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);
//--------------------默认读取第一个用户