import java.awt.EventQueue;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
public class DE extends JFrame {
/**
最终代码
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField tfDir;
private JTextField tfClass;
File[] fileArray; // 文件夹下所有文件
int NUM_IMG = 0; // 文件总数目
int index = 0; // 当前文件的序号
int i = 0;
JLabel jlbImg1 = null;
JLabel jlbImg2 = null;
JLabel jlbImg3 = null;
/**
* Launch the application.
*/
class myFileFilter implements FileFilter{
@Override
public boolean accept(File pathname) {
String filename = pathname.getName().toLowerCase();
if(filename.contains(".jpg")){
return false;
}else{
return true;
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DE frame = new DE();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DE() {
setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 645, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// 选择目录 按钮的处理程序
JButton btnDir = new JButton("\u9009\u62E9\u76EE\u5F55");
btnDir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
tfDir.setText(file.getAbsolutePath());
if(file!=null && file.isDirectory()){
// 参考: java中File.listFiles(FileFilter) FileFilter的使用
// http://zhouzaibao.iteye.com/blog/347557 ;
// 获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
}
});
btnDir.setBounds(26, 26, 93, 23);
contentPane.add(btnDir);
// 文本框,显示目录
tfDir = new JTextField();
tfDir.setEditable(false);
tfDir.setBounds(125, 27, 450, 21);
contentPane.add(tfDir);
tfDir.setColumns(10);
// 选择班级 按钮的处理程序
/* JButton btnClass = new JButton("\u9009\u62E9\u73ED\u7EA7");
btnClass.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser jfc=new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
tfClass.setText(file.getAbsolutePath());
}
});
btnClass.setBounds(26, 59, 93, 23);
contentPane.add(btnClass);
// 文本框,显示班级文件
tfClass = new JTextField();
tfClass.setEditable(false);
tfClass.setBounds(125, 60, 450, 21);
contentPane.add(tfClass);
tfClass.setColumns(10);*/
// 标签,显示带猜测学生姓名
final JLabel lbGuessName = new JLabel("学生姓名");
lbGuessName.setBounds(259, 91,140,23);
contentPane.add(lbGuessName);
// 标签,显示第一个学生相片
final JLabel lblImg1 = new JLabel("学生一");
lblImg1.addMouseListener(new MouseAdapter () {
@Override
public void mouseClicked(MouseEvent arg0) {
if(arg0.getSource()==lblImg1){
if(( lblImg1.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);
try {
//与数据库建立连接
Connection con=DriverManager.getConnection("jdbc:odbc:myDB");
Statement s1 = con.createStatement();
s1.executeUpdate("update STUDENT set R=R+1 where ID=1");
s1.close();
con.close();
System.out.println("execute ok");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);
try {
//与数据库建立连接
Connection con=DriverManager.getConnection("jdbc:odbc:myDB");
Statement s = con.createStatement();
s.executeUpdate("update STUDENT set WRONG=WRONG+1 where ID=1");
s.close();
con.close();
System.out.println("execute ok");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
lblImg1.setBounds(26,155, 150, 200);
contentPane.add(lblImg1);
// 标签,显示第二个学生相片
final JLabel lblImg2 = new JLabel("学生二");
lblImg2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg1) {
if(arg1.getSource()==lblImg2){
if(( lblImg2.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);
try {
//与数据库建立连接
Connection con=DriverManager.getConnection("jdbc:odbc:myDB");
Statement s1 = con.createStatement();
s1.executeUpdate("update STUDENT set R=R+1 where ID=1");
s1.close();
con.close();
System.out.println("execute ok");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);
try {
//与数据库建立连接
Connection con=DriverManager.getConnection("jdbc:odbc:myDB");
Statement s = con.createStatement();
s.executeUpdate("update STUDENT set WRONG=WRONG+1 where ID=1");
s.close();
con.close();
System.out.println("execute ok");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
lblImg2.setForeground(Color.BLACK);
lblImg2.setBackground(SystemColor.inactiveCaption);
lblImg2.setBounds(241,155, 150, 200);
contentPane.add(lblImg2);
// 标签,显示第三个学生相片
final JLabel lblImg3 = new JLabel("学生三");
lblImg3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg2) {
if(arg2.getSource()==lblImg3){
if(( lblImg3.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);
try {
//与数据库建立连接
Connection con=DriverManager.getConnection("jdbc:odbc:myDB");
Statement s1 = con.createStatement();
s1.executeUpdate("update STUDENT set R=R+1 where ID=1");
s1.close();
con.close();
System.out.println("execute ok");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);
try {
//与数据库建立连接
Connection con=DriverManager.getConnection("jdbc:odbc:myDB");
Statement s = con.createStatement();
s.executeUpdate("update STUDENT set WRONG=WRONG+1 where ID=1");
s.close();
con.close();
System.out.println("execute ok");
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
});
lblImg3.setBounds(434,155, 150,200);
contentPane.add(lblImg3);
// 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名
final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");
btnGuessAgain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnGuessAgain ){ //如果是next按钮
Random random = new Random(System.currentTimeMillis());
//循环三次
ImageIcon icon;
int j = 0,h = 0,l = 0,j1 = 0,h1 = 0,l1 = 0;
for(int i=0;i<3;i++)
{
index = random.nextInt(NUM_IMG);
String strTmp = fileArray[index].toString();
String filename1=fileArray[index].getName();
try {
icon = new ImageIcon(ImageIO.read(new File(strTmp)));
// 从图表中获取到图片
Image image = icon.getImage();
// 缩放图像
Image smallImage = image.getScaledInstance(150,200,Image.SCALE_FAST);
//把Image文件转化为ImageIcon
icon = new ImageIcon(smallImage);
if(index==NUM_IMG)
index = 0;
switch(i)
{
case 0:
System.out.println(index);
lblImg1.setIcon(icon);
lblImg1.setText(filename1);
h=index;
h1=index;
break;
case 1:
System.out.println(index);
lblImg2.setIcon(icon);
lblImg2.setText(filename1);
j=index;
j1=index;
break;
case 2:
System.out.println(index);
lblImg3.setIcon(icon);
lblImg3.setText(filename1);
l=index;
l1=index;
break;
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
int []data1 = {j,h,l,j1,l1,h1};
Random r=new Random();
StringBuffer str1=new StringBuffer();
str1.append(data1[r.nextInt(4)]);
String filename = str1.toString();
System.out.println(str1);
int a = Integer.parseInt(filename);
String filename1 = fileArray[a].getName();
lbGuessName.setText(filename1);
}
}
});
btnGuessAgain.setBounds(223, 400, 93, 23);
contentPane.add(btnGuessAgain);
}
}
#猜猜看#连接数据库计算猜对次数猜错次数
最新推荐文章于 2021-03-27 14:43:30 发布