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
lập trình viên java adx đâu roài, vào giúp em nó cái
Cái này thì mình chịu roài ! :(