小的不材,写了新用户注册界面,是利用文本文档的形式来替代数据库的,有文件的读和取的功能,但是我现在想在我所写的登录界面上,把用户的所填写的姓名和密码,和在新注册的那个程序中的姓名和密码做比较,看看是否相匹配,然后确定是否能登录,不知道哪位大侠能指点?请看下面代码,
第一个 是新用户注册功能
第二个 是登录界面(下面是登录界面,由于有别的功能(包括国际化),所以下面的代码,
只是想知道在监听事件中如何让用户所打的姓名和密码
与上面的文本相匹配
请大侠指教)[请不要在回复里写废话]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.applet.*;
public class Regit extends JApplet implements ActionListener,ItemListener//接口
{
String s=null;
//对象的声明
JLabel labelname,labelpass,labelsxe,labeladdress,labelmail,labelphone;//标签
JTextField textname,textaddress,textmail,textphone;//文本框
JRadioButton r1,r2;//单选按纽
ButtonGroup bg;//组
JPasswordField textpass;//密码域
JButton buttonregit,buttonreset;//注册按纽 重写按纽
JPanel p;//面板
String sex;//定义性别字符串
URL url;//统一资源定位
BufferedWriter out1,out2;//流
BufferedReader in;
//布局方式
GridBagLayout gbl;
GridBagConstraints gc;
AppletContext co;//接口
//初始化
public void init()
{
//new 对象
labelname=new JLabel("用 户 名:");
labelpass=new JLabel("用户密码:");
labelsxe=new JLabel("性别:");
labeladdress=new JLabel("地址:");
labelmail=new JLabel("电子邮件:");
labelphone=new JLabel("联系电话:");
textname=new JTextField(15); textname.setForeground(Color.red);
textname.setToolTipText("请在这输入你的用户名");
textaddress=new JTextField(15);textaddress.setForeground(Color.red);
textaddress.setToolTipText("请在这输入你的地址");
textmail=new JTextField(15); textmail.setForeground(Color.red);
textmail.setToolTipText("请在这里输入你的E-mail地址");
textphone=new JTextField(15); textphone.setForeground(Color.red);
textphone.setToolTipText("请在这输入你的电话号码");
r1=new JRadioButton("男"); r1.setBackground(new Color(47,177,210));//设置颜色
r2=new JRadioButton("女"); r2.setBackground(new Color(47,177,210));//设置颜色
bg=new ButtonGroup();
bg.add(r1);bg.add(r2);//加入组,实现单选
textpass=new JPasswordField(15);
textpass.setToolTipText("在这里输入密码");
textpass.setForeground(Color.red);
buttonregit=new JButton("注册"); buttonregit.setBackground(new Color(47,177,210));//设置颜色
buttonregit.setToolTipText("点击按纽完成注册");
buttonreset=new JButton("填写"); buttonreset.setBackground(new Color(47,177,210));//设置颜色
buttonreset.setToolTipText("点击按纽刷新重写");
gbl=new GridBagLayout(); ///////////////////////////////////////
gc=new GridBagConstraints(); //////采用GridBagLayout布局方式////////
p=new JPanel();
p.setLayout(gbl);
p.setBackground(new Color(47,177,210));
this.getContentPane().add(p);//加入面板
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=2;
gbl.setConstraints(labelname,gc);
p.add(labelname);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=2;
gbl.setConstraints(textname,gc);
p.add(textname);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=4;
gbl.setConstraints(labelpass,gc);
p.add(labelpass);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=4;
gbl.setConstraints(textpass,gc);
p.add(textpass);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=6;
gbl.setConstraints(labelsxe,gc);
p.add(labelsxe);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=6;
gbl.setConstraints(r1,gc);
p.add(r1);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=5;
gc.gridy=6;
gbl.setConstraints(r2,gc);
p.add(r2);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=8;
gbl.setConstraints(labeladdress,gc);
p.add(labeladdress);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=8;
gbl.setConstraints(textaddress,gc);
p.add(textaddress);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=10;
gbl.setConstraints(labelmail,gc);
p.add(labelmail);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=10;
gbl.setConstraints(textmail,gc);
p.add(textmail);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=12;
gbl.setConstraints(labelphone,gc);
p.add(labelphone);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=12;
gbl.setConstraints(textphone,gc);
p.add(textphone);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=2;
gc.gridy=16;
gbl.setConstraints(buttonregit,gc);
p.add(buttonregit);
gc.anchor=GridBagConstraints.NORTHWEST;
gc.gridx=4;
gc.gridy=16;
gbl.setConstraints(buttonreset,gc);
p.add(buttonreset);
/////////////////////////////////////////////
buttonregit.addActionListener(this);//按纽事件的监听
buttonreset.addActionListener(this);//按纽事件的监听
r1.addItemListener(this);//选择事件的监听
r2.addItemListener(this);//选择事件的监听
textphone.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//注册按纽事件
if(e.getSource()==buttonregit)
{
String s1=textname.getText(); ////////////////////
String s2=new String(textpass.getPassword());////////////////////
String s3=textaddress.getText(); // 定义字符串 //
String s4=textmail.getText(); ////////////////////
String s5=textphone.getText(); ////////////////////
//判断注册资料 信息 是否为空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"资料不能为空,请重新注册!","【温馨提示】",error);
return;//弹出对话框并返回
}
try//写入到txt文件
{
in=new BufferedReader(new FileReader("E:\\新建文件夹\\password.txt"));
}
catch(Exception ee){}
String ss=s1;
try
{
while((s=in.readLine())!=null)
{
if(s.startsWith(ss))
{
JOptionPane.showMessageDialog(null,"用户名已经存在,请更换名字!");
textname.setText("");//设置为空,重新输入
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
return;
}
}
}
catch(Exception ee){}
///////////////////////////以上代码判断是否有同名
{
try
{
out1=new BufferedWriter(new FileWriter("E:\\新建文件夹\\password.txt",true));
out2=new BufferedWriter(new FileWriter("E:\\新建文件夹\\message.txt",true));
}//创建文件
catch(Exception ee)
{}
try
{
out1.write(s1+"#"+s2);//写
out1.newLine();
out2.write("用户名:"+s1);
out2.newLine();
out2.write("密码:"+s2);
out2.newLine();
out2.write("性别:"+sex);
out2.newLine();
out2.write("地址:"+s3);
out2.newLine();
out2.write("电子邮件:"+s4);
out2.newLine();
out2.write("电话:"+s5);
out2.newLine();
out1.flush();
out2.flush();//清理缓冲
out1.close();
out2.close();
}
catch(Exception ee)
{}
JOptionPane.showMessageDialog(null,"注册成功!");
}
}
//////////////////////以下为回车事件
if(e.getSource()==textphone)
{
String s1=textname.getText(); ////////////////////
String s2=new String(textpass.getPassword());////////////////////
String s3=textaddress.getText(); // 定义字符串 //
String s4=textmail.getText(); ////////////////////
String s5=textphone.getText(); ////////////////////
//判断注册资料 信息 是否为空
if(s1.length()==0 || s2.length()==0 ||s3.length()==0 ||s4.length()==0 ||s5.length()==0)
{
int error=JOptionPane.INFORMATION_MESSAGE;
JOptionPane.showMessageDialog(null,"资料不能为空,请重新注册!","【温馨提示】",error);
return;//弹出对话框并返回
}
try//写入到txt文件
{
in=new BufferedReader(new FileReader("E:\\新建文件夹\\password.txt"));
}
catch(Exception ee){}
String ss=s1;
try
{
while((s=in.readLine())!=null)
{
if(s.startsWith(ss))
{
JOptionPane.showMessageDialog(null,"用户名已经存在,请更换名字!");
textname.setText("");//设置为空,重新输入
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
return;
}
}
}
catch(Exception ee){}
///////////////////////////以上代码判断是否有同名
{
try
{
out1=new BufferedWriter(new FileWriter("E:\\新建文件夹\\password.txt",true));
out2=new BufferedWriter(new FileWriter("E:\\新建文件夹\\message.txt",true));
}//创建文件
catch(Exception ee)
{ee.printStackTrace();}
try
{
out1.write(s1+"#"+s2);//写
out1.newLine();
out2.write("用户名:"+s1+"密码:"+s2+"性别:"+sex+"地址:"+s3+"电子邮件:"+s4+"电话:"+s5);//写
out2.newLine();
out1.flush();
out2.flush();//清理缓冲
out1.close();
out2.close();
JOptionPane.showMessageDialog(null,"注册成功!");
}
catch(Exception ee)
{ee.printStackTrace();}
}
}
if(e.getSource()==buttonreset)//刷新重写事件
{
textname.setText("");
textpass.setText("");
textaddress.setText("");
textmail.setText("");
textphone.setText("");
}
}
//////////////////////////////////////////
//
public void itemStateChanged(ItemEvent ex)
{
if(ex.getSource()==r1)
{
sex=new String("男");
}
else if(ex.getSource()==r2)
{
sex=new String("女");
}
}
public static void main(String[] args){
JFrame jf = new JFrame();
Regit applet = new Regit();
jf.add(applet);
applet.init();
jf.pack();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
下面是登录界面,由于有别的功能(包括国际化),所以下面的代码,
只是想知道在监听事件中如何让用户所打的姓名和密码
与上面的文本相匹配
请大侠指教
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JComboBox;
import javax.swing.border.*;
import java.util.*;
import java.text.*;
import java.lang.String;
import javax.swing.JLabel;
import java.util.ResourceBundle;
public class LoginGUI extends JFrame implements ActionListener,ItemListener{
private ResourceBundle rs = new BoundleBean("MyGUI").getRs();
JComboBox jcboLocale = new JComboBox();
TitledBorder comboBoxTitle = new TitledBorder("language");
Locale locales[] = Calendar.getAvailableLocales();//!!!!!!!!!!!!!!!!!!!!!!
public void initializeComboBox()//!!!!!!!!!!!!!!!!!!!!!
{
// Add locale names to the combo box
for (int i=0; i<locales.length; i++)
jcboLocale.addItem(locales[i].getDisplayName());
}
private JTextField loginName = new JTextField(15);
private JPasswordField loginPassword = new JPasswordField(15);
private JButton login= new JButton(rs.getString("login")/*"登录"*/);
private JButton register = new JButton(rs.getString("regist")/*"注册"*/);
private JLabel name = new JLabel(rs.getString("name"));
private JLabel password = new JLabel(rs.getString("password"));
static String verify = "null";
static String verify1 = "null";
public LoginGUI(){
this.getContentPane().setLayout(new BorderLayout());
JPanel p1 = new JPanel(new GridLayout(2,1));
JPanel p1_1 = new JPanel(new GridLayout(1,2));
p1_1.add(name/*"姓名"*/);
p1_1.add(loginName);
p1.add(p1_1);
JPanel p1_2 = new JPanel(new GridLayout(1,2));
p1_2.add(password/*"密码"*/);
p1_2.add(loginPassword);
p1.add(p1_2);
this.getContentPane().add(p1,BorderLayout.NORTH);
JPanel p2 = new JPanel();
p2.add(new JLabel(new ImageIcon("ohioMap.jpg")));
this.getContentPane().add(p2,BorderLayout.CENTER);
JPanel p3 = new JPanel(new GridLayout(2,1));
JPanel p3_1 = new JPanel(new GridLayout(1,2));
p3_1.add(login);
p3_1.add(register);
p3.add(p3_1);
JPanel p0 = new JPanel();
p0.setLayout(new FlowLayout());
p0.add(jcboLocale);
jcboLocale.addItemListener(this);
initializeComboBox();
p0.setBorder(comboBoxTitle);
p3.add(p0);
this.getContentPane().add(p3,BorderLayout.SOUTH);
login.setToolTipText(rs.getString("tishi1")/*"已是软件的会员请登录"*/);
register.setToolTipText(rs.getString("tishi2")/*"新会员注册"*/);
login.addActionListener(this);
register.addActionListener(this);
Toolkit kit = Toolkit.getDefaultToolkit();
Image minImage = kit.getImage("12.gif");
this.setIconImage(minImage);
this.setTitle(rs.getString("title")/*"心灵在此得到归宿"*/);
this.pack();
CenterFrame.setScreenCenter(this);
this.setSize(415,447);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == jcboLocale||e.getSource() == login||e.getSource() ==register)
{
SysLocale.theLocale = locales[jcboLocale.getSelectedIndex()];
updateStrings();
}
}
public void actionPerformed(ActionEvent e){
if(e.getSource() == login){;
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
new MainGUI().setVisible(true);
}
// verify = loginPassword.getText();
// verify1 = loginName.getText();
// BusinessLogin bl = new BusinessLogin();
// ReadIn newPeople = new ReadIn(loginName.getText(),loginPassword.getText());
// try{
// bl.verifyRole(newPeople);
// this.setVisible(false);
// this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// new MainGUI().setVisible(true);
// }
// catch (UserNotExistedException ex){
// JOptionPane.showMessageDialog(this,rs.getString("error")/*"出错"*/,rs.getString("notExisted")/*"该用户不存在"*/,JOptionPane.ERROR_MESSAGE);
// loginName.setText("");
// loginPassword.setText("");
// }catch(FalsePasswordException ex){
// JOptionPane.showMessageDialog(this,rs.getString("passwordError")/*"密码错误"*/,rs.getString("passworrdError")/*"密码错误"*/,JOptionPane.WARNING_MESSAGE);
// loginPassword.setText("");
// }
// }
else if(e.getSource() == register){
try{
/*新会员注册功能*/
JFrame jf = new JFrame();
Regit applet = new Regit();
jf.add(applet);
applet.init();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
catch(Exception ex){
}
}
}
public static void main(String[] args){
new LoginGUI().setVisible(true);
}
private void updateStrings()
{
rs = ResourceBundle.getBundle("MyGUI", SysLocale.theLocale);
login.setText(rs.getString("login"));
register.setText(rs.getString("regist"));
this.setTitle(rs.getString("title")/*"心灵在此得到归宿"*/);
name.setText(rs.getString("name"));
password.setText(rs.getString("password"));
login.setToolTipText(rs.getString("tishi1")/*"已是软件的会员请登录"*/);
register.setToolTipText(rs.getString("tishi2")/*"新会员注册"*/);
repaint();
}
}
[此贴子已经被作者于2006-12-23 15:25:00编辑过]