1 GIẢI ĐỀ THI Fri Nov 23, 2012 10:28 pm
m1_huy
Vịt mới lớn
giải đề thi cuối kì cho ae rút kinh nghiệm admin ơi!
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Carro {
JFrame frame = new JFrame();
paint board = new paint();
int x,y,method;
Carro(){
frame.setTitle("Carro");
frame.setSize(258, 280);
frame.setLocation(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
x=e.getX();
y=e.getY();
x=x/40;
y=y/40;
x=x*40;
y=y*40-40;
if (e.getButton()==MouseEvent.BUTTON1)
board.setL(x, y, 0);
else
if (e.getButton()==MouseEvent.BUTTON3)
board.setL(x, y, 1);
board.repaint();
}
});
frame.add(board);
frame.setVisible(true);
}
public static void main(String[] args) {
Carro n = new Carro();
}
}
class paint extends JPanel{
int lx[] =new int[40];
int ly[] =new int[40];
int rx[] =new int[40];
int ry[] =new int[40];
int sizel=0,sizer=0;
int x,y;
public boolean exits(int x,int y,int method){
for (int i=0;i<sizel;i++)
if (lx[i]==x && ly[i]==y)
return true;
for (int i=0;i<sizer;i++)
if (rx[i]==x && ry[i]==y)
return true;
return false;
}
public void setL(int x,int y,int method){
this.x=x;
this.y=y;
System.out.println(sizel);
if (!exits(x,y,method)){
if (method==0){
lx[sizel]=x;
ly[sizel]=y;
sizel++;
}
else if (method==1){
rx[sizer]=x;
ry[sizer]=y;
sizer++;
}
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Draw rows
g.drawLine(0, 0, 240, 0);
g.drawLine(0, 40, 240, 40);
g.drawLine(0, 80, 240, 80);
g.drawLine(0, 120, 240, 120);
g.drawLine(0, 160, 240, 160);
g.drawLine(0, 200, 240, 200);
g.drawLine(0, 240, 240, 240);
//Draw cols
g.drawLine(0, 0, 0, 240);
g.drawLine(40, 0, 40, 240);
g.drawLine(80, 0, 80, 240);
g.drawLine(120, 0, 120, 240);
g.drawLine(160, 0, 160, 240);
g.drawLine(200, 0, 200, 240);
g.drawLine(240, 0, 240, 240);
for (int i=0;i<sizel;i++){
g.setColor(Color.red);
g.drawOval(lx[i]+5, ly[i]+5, 30, 30);
}
for (int i=0;i<sizer;i++){
g.setColor(Color.green);
g.drawLine(rx[i]+5, ry[i]+5, rx[i]+35, ry[i]+35);
g.drawLine(rx[i]+35, ry[i]+5, rx[i]+5, ry[i]+35);
}
}
}
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
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.JPasswordField;
import javax.swing.JTextField;
public class DangNhap {
JFrame frame;
JTextField t_user;
JPasswordField t_pass;
JLabel icon_user,icon_pass,l_user,l_pass;
JButton login,Cancel;
JPanel Butt,Show;
DangNhap(){
frame = new JFrame();
frame.setLayout(new BorderLayout());
frame.setSize(350, 200);
frame.setTitle("Bai 2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Text field
t_user= new JTextField(15);
t_pass = new JPasswordField(15);
//Button
login = new JButton("Login");
login.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String n,n1;
String p,p2="";
int flag=0;
char[] p1;
n1 = t_user.getText().toString();
p1 = t_pass.getPassword();
for (int i=0;i< p1.length;i++)
p2+=p1[i]+"";
try {
Scanner fin = new Scanner(new FileReader("thongtin.txt"));
while (fin.hasNext()){
n = fin.next();
p = fin.next();
System.out.println(n+" "+n1);
System.out.println(p+" "+p2);
if (n.equals(n1) && p.equals(p2)){
JOptionPane.showMessageDialog(frame, "Dang nhap thanh cong");
flag=1;
break;
}
}
if (flag==0)
JOptionPane.showMessageDialog(frame, "Dang nhap THAT BAI");
} catch (FileNotFoundException ex) {
Logger.getLogger(DangNhap.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Cancel = new JButton("Cancel");
Cancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
//Label
icon_user = new JLabel(new ImageIcon("username.png"));
icon_pass = new JLabel(new ImageIcon("pass.png"));
l_user = new JLabel("Username :");
l_pass = new JLabel("Password :");
Butt = new JPanel(new FlowLayout());
Butt.add(login);
Butt.add(Cancel);
Show = new JPanel(new FlowLayout());
Show.add(icon_user);
Show.add(l_user);
Show.add(t_user);
Show.add(icon_pass);
Show.add(l_pass);
Show.add(t_pass);
frame.add(BorderLayout.CENTER,Show);
frame.add(BorderLayout.SOUTH,Butt);
frame.setVisible(true);
}
public static void main(String[] args) {
DangNhap obj = new DangNhap();
}
}
Similar topics
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|
Admin (56) | ||||
1101817 (16) | ||||
m1_huy (16) | ||||
Võ Nguyên Khoa (11) | ||||
user1101 (6) | ||||
java_study (2) | ||||
toanhl (2) | ||||
uhthithoi (2) | ||||
lymai (1) | ||||
aidzayta (1) |