对于个人而言,在做这个程序的过程中我学到了很多东西。不仅仅是熟悉了Java语言,熟悉了网络编程,掌握了时事件监听,更多的是学会了如何去调试一个程序,并且遇到问题该如何利用可利用的资源解决问题。
今天没有继续做猜猜看的游戏,因为我觉得我得先理顺一下我的思路,所以今天我重新的读了一下代码,在还不太明白的地方问同学,以便解决~并且在代码上有所标注。
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.Image;
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.util.Random;
@SuppressWarnings("serial")
public class cdf extends JFrame {
/**
*
*/
class myFileFilter implements FileFilter{
//过滤文件类型~
@Override
public boolean accept(File pathname) {
String filename = pathname.getName().toLowerCase();
if(filename.contains(".jpg")){
return false;
}else{
return true;
}
}
}
private JPanel contentPane;
private JTextField tfDir;
private JTextField tfClass;
File[] fileArray; // 文件夹下所有文件
int NUM_IMG = 0; // 文件总数目
int index = 0; // 当前文件的序号
JLabel jlbImg = null;
JButton btnNext = null;
/**
* Launch the application.
*/
//主函数~
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
cdf frame = new cdf();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public cdf() {
setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗体~
setBounds(100, 100, 600, 500);//窗体的大小
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));//边界布局~分成5行5列~
setContentPane(contentPane);
contentPane.setLayout(null);
// 选择目录 按钮的处理程序
JButton btnDir = new JButton("目录");
btnDir.addActionListener(new ActionListener() {//添加监听器~
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser j=new JFileChooser();
j.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置Filechooser
j.showDialog(new JLabel(), "选择目录");
File file=j.getSelectedFile();
tfDir.setText(file.getAbsolutePath());//将路径显示在文本框~
// TODO Auto-generated method stub
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, 363, 21);
contentPane.add(tfDir);
tfDir.setColumns(10);
// 选择班级 按钮的处理程序
JButton btnClass = new JButton("班级");
btnClass.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser i=new JFileChooser();
i.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置Filechooser
i.showDialog(new JLabel(), "选择班级");
File file=i.getSelectedFile();
tfClass.setText(file.getAbsolutePath());//将路径显示在文本框~
// 获取文件夹下所有的文件
fileArray = file.listFiles();
NUM_IMG = fileArray.length;
}
});
btnClass.setBounds(26, 59, 93, 23);
contentPane.add(btnClass);
// 文本框,显示班级文件
tfClass = new JTextField();
tfClass.setEditable(false);//文本框不可编辑~
tfClass.setBounds(125, 60, 363, 21);
contentPane.add(tfClass);
tfClass.setColumns(10);
// 标签,显示带猜测学生姓名
final JLabel lbGuessName = new JLabel("\u59D3\u540D");
lbGuessName.setBounds(259, 91, 102, 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);
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","警告",JOptionPane.WARNING_MESSAGE);
}
}
}
});
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);
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","警告",JOptionPane.WARNING_MESSAGE);
}
}
}
});
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);
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","警告",JOptionPane.WARNING_MESSAGE);
}
}
}
});
lblImg3.setBounds(434,155, 150, 200);
contentPane.add(lblImg3);
// 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名
final JButton btnNext = new JButton("再猜一次");
btnNext.addActionListener(new ActionListener() {//添加监听~
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btnNext){ //如果是next按钮
//随机数
Random random = new Random();
//循环三次
ImageIcon icon;//定义icon变量
int p1 = 0,p2 = 0, p3 = 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);// 缩放图像
icon = new ImageIcon(smallImage);//把Image文件转化为ImageIcon
if(index==NUM_IMG)
index = 0;
switch(i)
{
case 0:
lblImg1.setIcon(icon);
lblImg1.setText(filename1);
p1 = index;
break;
case 1:
lblImg2.setIcon(icon);
lblImg2.setText(filename1);
p2 = index;
break;
case 2:
lblImg3.setIcon(icon);
lblImg3.setText(filename1);
p3 = index;
break;
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
int []cdf = {p1,p2,p3};//数组~
Random r = new Random();
StringBuffer s1=new StringBuffer();
s1.append(cdf[r.nextInt(2)]);
String filename = s1.toString();//将s1转换为String类型~
int a = Integer.parseInt(filename);
String filename1 = fileArray[a].getName();
lbGuessName.setText(filename1);
}
}
});
btnNext.setBounds(223, 400, 93, 23);//再猜一次按钮的位置
contentPane.add(btnNext);
}
}