Java程序日志四
package aadd;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Color;
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.FilenameFilter;
import java.util.Random;
public class Guess01<lblImg1> extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
String strPath = ""; //文件夹路径
String strFileName = ""; //文件名称
File[] fileArray; // 文件夹下所有文件
int NUM_IMG = 0; // 文件总数目
int index = 0; // 当前文件的序号
int index1 =0;
int index2 =0;
private JPanel contentPane;
private JTextField tfDir;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Guess01 frame = new Guess01();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//********************************************************以下是图形界面部分***************************************************
public Guess01() {
setTitle(" 猜猜看游戏国滔版");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 645, 409);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
// 选择目录 按钮的处理程序
final JButton btnDir = new JButton("\u9009\u62E9\u76EE\u5F55");
btnDir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser jfc=new JFileChooser("D:/");
//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.showDialog(new JLabel(), "选择");
File file=jfc.getSelectedFile();
tfDir.setText(file.getAbsolutePath());
strPath = file.getAbsolutePath();
strFileName = jfc.getSelectedFile().getName();
if(file!=null && file.isDirectory()){
// 获取文件夹下所有的文件
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);
// 标签,显示带猜测学生姓名
final JLabel lbGuessName = new JLabel("这是学生姓名显示的地方");
lbGuessName.setBounds(240, 91, 200, 23);
contentPane.add(lbGuessName);
// 标签,显示第一个学生相片
final JLabel lblImg1 = new JLabel("这是显示学生美照的地方1");
lblImg1.setBackground(Color.RED);
this.add(lblImg1);
lblImg1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
if((lblImg1.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);
}
}
}
);
lblImg1.setBounds(26, 151, 183, 178);
contentPane.add(lblImg1);
// 标签,显示第二个学生相片
final JLabel lblImg2 = new JLabel("这是显示学生美照的地方2");
lblImg2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg1) {
if((lblImg2.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);
}
}
}
);
lblImg2.setForeground(Color.BLACK);
lblImg2.setBackground(SystemColor.inactiveCaption);
lblImg2.setBounds(241, 155, 183, 172);
contentPane.add(lblImg2);
// 标签,显示第三个学生相片
final JLabel lblImg3 = new JLabel("这是显示学生美照的地方3");
lblImg3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg2) {
if((lblImg3.getText().equals(lbGuessName.getText()))){
JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);
}
}
}
);
lblImg3.setBounds(434, 155, 185, 172);
contentPane.add(lblImg3);
// 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名
JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");
btnGuessAgain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ImageIcon icon = null;
File f = new File(strPath);
String[] names = f.list(new FilenameFilter(){
@Override
public boolean accept(File f, String name) {
// TODO Auto-generated method stub
return name.endsWith(".jpg");
}
});
Random r = new Random();
int []nArrey ={r.nextInt(NUM_IMG),r.nextInt(NUM_IMG),r.nextInt(NUM_IMG)};
String imageSrc = strPath+"/"+names[nArrey[0]];
String imageSrc1 = strPath+"/"+names[nArrey[1]];
String imageSrc2 =strPath+"/"+names[nArrey[2]];
//随机显示三张图片
lblImg1.setIcon(new ImageIcon(imageSrc));
lblImg2.setIcon(new ImageIcon(imageSrc1));
lblImg3.setIcon(new ImageIcon(imageSrc2));
//把文件名输出到标签
lblImg1.setText(names[nArrey[0]]);
lblImg2.setText(names[nArrey[1]]);
lblImg3.setText(names[nArrey[2]]);
//取值随机数3个 减一可选择0、1、2数组数
Random l=new Random();
int s= l.nextInt(3)-1;
//把三张图中的文件名随机显示一个到显示学生姓名标签中
lbGuessName.setText(names[nArrey[s]]);
}});
btnGuessAgain.setBounds(223, 337, 93, 23);
contentPane.add(btnGuessAgain);
}
}