Cổng vào tri thức => Tin học => Topic started by: duyanh on 19/08/07, 17:23 Return to Full Version
Title: Giúp mình code ve java
Post by: duyanh on 19/08/07, 17:23
Post by: duyanh on 19/08/07, 17:23
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class SampleFile1 extends JFrame implements ActionListener
{
JLabel idLabel;
JLabel passLabel;
JTextField idText;
JPasswordField passText;
JButton loginButton;
JButton registerButton;
public SampleFile1()
{
super("Login");
setLayout(null);
idLabel=new JLabel("ID");
add(idLabel);
idLabel.setBounds(20,20,50,20);
idText=new JTextField();
add(idText);
idText.setBounds(70,20,210,20);
passLabel=new JLabel("Pass");
add(passLabel);
passLabel.setBounds(20,40,50,20);
passText=new JPasswordField();
add(passText);
passText.setBounds(70,40,210,20);
loginButton=new JButton("Login");
add(loginButton);
loginButton.setBounds(180,80,100,20);
registerButton=new JButton("Register");
add(registerButton);
registerButton.setBounds(80,80,100,20);
registerButton.addActionListener(this);
loginButton.addActionListener(this);
setSize(300,150);
show();
}
public void actionPerformed(ActionEvent e)
{
try
{
JButton bt;
Account a;
JFrame mainFrame=new JFrame("Main");
mainFrame.setSize(300,300);
boolean check=false;
bt=(JButton)e.getSource();
if(bt==registerButton)
{
FileOutputStream fOut=new FileOutputStream("user.dat",true);
ObjectOutputStream objOut=new ObjectOutputStream(fOut);
a=new Account(idText.getText(),passText.getText());
objOut.writeObject(a);
fOut.close();
objOut.close();
}
if(bt==loginButton)
{
FileInputStream fIn=new FileInputStream("uses.dat");
ObjectInputStream objIn=new ObjectInputStream(fIn);
a=(Account)objIn.readObject();
while(a!=null&&!check)
{
if(idText.getText().equals(a.id)&&passText.getText().equals(a.pass))
{
mainFrame.setVisible(true);
check=true;
}
a=(Account)objIn.readObject();
}
if(!check) JOptionPane.showMessageDialog(this,"Try again");
fIn.close();
objIn.close();
}
}
catch(IOException ex)
{
}
catch(ClassNotFoundException ex)
{
}
}
public static void main(String [] ts)
{
new SampleFile1();
}
}
Chương trình này kô đọc được cái file user.dat hay sao ấy, kô hiểu mình làm sai cái gì nữa
import java.awt.event.*;
import java.io.*;
public class SampleFile1 extends JFrame implements ActionListener
{
JLabel idLabel;
JLabel passLabel;
JTextField idText;
JPasswordField passText;
JButton loginButton;
JButton registerButton;
public SampleFile1()
{
super("Login");
setLayout(null);
idLabel=new JLabel("ID");
add(idLabel);
idLabel.setBounds(20,20,50,20);
idText=new JTextField();
add(idText);
idText.setBounds(70,20,210,20);
passLabel=new JLabel("Pass");
add(passLabel);
passLabel.setBounds(20,40,50,20);
passText=new JPasswordField();
add(passText);
passText.setBounds(70,40,210,20);
loginButton=new JButton("Login");
add(loginButton);
loginButton.setBounds(180,80,100,20);
registerButton=new JButton("Register");
add(registerButton);
registerButton.setBounds(80,80,100,20);
registerButton.addActionListener(this);
loginButton.addActionListener(this);
setSize(300,150);
show();
}
public void actionPerformed(ActionEvent e)
{
try
{
JButton bt;
Account a;
JFrame mainFrame=new JFrame("Main");
mainFrame.setSize(300,300);
boolean check=false;
bt=(JButton)e.getSource();
if(bt==registerButton)
{
FileOutputStream fOut=new FileOutputStream("user.dat",true);
ObjectOutputStream objOut=new ObjectOutputStream(fOut);
a=new Account(idText.getText(),passText.getText());
objOut.writeObject(a);
fOut.close();
objOut.close();
}
if(bt==loginButton)
{
FileInputStream fIn=new FileInputStream("uses.dat");
ObjectInputStream objIn=new ObjectInputStream(fIn);
a=(Account)objIn.readObject();
while(a!=null&&!check)
{
if(idText.getText().equals(a.id)&&passText.getText().equals(a.pass))
{
mainFrame.setVisible(true);
check=true;
}
a=(Account)objIn.readObject();
}
if(!check) JOptionPane.showMessageDialog(this,"Try again");
fIn.close();
objIn.close();
}
}
catch(IOException ex)
{
}
catch(ClassNotFoundException ex)
{
}
}
public static void main(String [] ts)
{
new SampleFile1();
}
}
Chương trình này kô đọc được cái file user.dat hay sao ấy, kô hiểu mình làm sai cái gì nữa
Title: Re: Giúp mình code ve java
Post by: saos@ngmo on 19/08/07, 23:16
Post by: saos@ngmo on 19/08/07, 23:16
lập trình viên java adx đâu roài, vào giúp em nó cái
Title: Re: Giúp mình code ve java
Post by: phamhongtu on 19/08/07, 23:47
Post by: phamhongtu on 19/08/07, 23:47
Cái này thì mình chịu roài ! :(