1 Bài 4 - Buổi 2 Sun Nov 04, 2012 4:07 pm
Admin
Admin
- Code:
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Bai4 {
JFrame frame;
JLabel lb1,lb2,img1,img2;
JTextField t1,t2;
JPanel p1,p2,p3,p4;
JButton Submit,Cancel;
public Bai4(){
frame = new JFrame();
frame.setLayout(new FlowLayout());
frame.setTitle("Dang nhap");
frame.setLocation(500, 300);
frame.setSize(400, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add p1
p1 = new JPanel(new GridLayout(1, 2));
lb1 = new JLabel("Username");
img1= new JLabel();
img1.setIcon(new ImageIcon("username.png"));
t1 = new JTextField(15);
p1.add(img1);
p1.add(lb1);
//Add p2
p2 = new JPanel(new GridLayout(1, 2));
lb2 = new JLabel("Password");
img2= new JLabel();
img2.setIcon(new ImageIcon("pass.png"));
t2 = new JTextField(15);
p2.add(img2);
p2.add(lb2);
// Create button
Submit = new JButton("Đăng nhập");
Submit.addActionListener(new SubmitAction(t1,t2));
Cancel = new JButton("Cancel");
Cancel.addActionListener(new CancelAction());
frame.add(p1);
frame.add(t1);
frame.add(p2);
frame.add(t2);
frame.add(Submit);
frame.add(Cancel);
frame.setVisible(true);
}
public static void main(String[] args){
new Bai4();
}
}
class CancelAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
class SubmitAction implements ActionListener{
JTextField t1,t2;
SubmitAction(JTextField t1, JTextField t2) {
this.t1 = t1;
this.t2 = t2;
}
@Override
public void actionPerformed(ActionEvent e) {
String a,b;
a=t1.getText();
b=t2.getText();
if (a.equals("") || b.equals(""))
JOptionPane.showMessageDialog(null, "Username or Password is not blank");
else
if (a.equals(b))
JOptionPane.showMessageDialog(null, "Dang nhap thanh cong");
else
if (!a.equals(b))
JOptionPane.showMessageDialog(null, "Dang nhap THAT BAI");
}
}
Được sửa bởi Admin ngày Thu Nov 08, 2012 9:40 pm; sửa lần 1.