《Java课程实习》日志(周四)&&课程设计:《猜猜看》游戏

来源:互联网 发布:poco与完成端口 编辑:程序博客网 时间:2024/04/28 08:54

游戏没有按预期成功完成,后面自己再去尝试,希望能够全部做出来。时间匆忙,暂时写这么多。

import java.awt.EventQueue;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.border.EmptyBorder;import javax.swing.AbstractButton;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.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.io.IOException;import java.util.Random;public class Guess01 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; String strPath = "";    //文件夹路径String strFileName = "";    //文件名称JLabel lblImg1 = null;JLabel lblImg2 = null;JLabel lblImg3 = null;JLabel lbGuessName =null;/** * 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();}}});}/** * Create the frame. */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("目录");btnDir.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(e.getSource()==btnDir ){    JFileChooser jfc=new JFileChooser();//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);jfc.showDialog(new JLabel(), "选择");File file=jfc.getSelectedFile();if(file.isDirectory()){System.out.println("文件夹:"+file.getAbsolutePath());}else if(file.isFile()){System.out.println("文件:"+file.getAbsolutePath());}System.out.println(jfc.getSelectedFile().getName());// 把文件路径显示在文本框中tfDir.setText(file.getAbsolutePath());//jlbImg.setIcon(new ImageIcon(file.getAbsolutePath()));// 获取文件路径 与文件名strPath = file.getAbsolutePath();strFileName = jfc.getSelectedFile().getName();if(file!=null && file.isDirectory()){// 获取文件夹下所有的文件fileArray = file.listFiles();NUM_IMG = fileArray.length;if(e.getSource()==btnDir){File f = new File(strPath);  String[] names = f.list(new FilenameFilter(){public boolean accept(File f , String name){return name.endsWith(".jpg");  }    });Random r = new Random();  String strTmp = strPath+"/"+names[r.nextInt(108)];    String strTmp1 =strPath+"/"+names[r.nextInt(108)];    String strTmp2 = strPath+"/"+ names [r.nextInt(108)];  lblImg1.setIcon(new ImageIcon(strTmp));lblImg2.setIcon(new ImageIcon(strTmp1));lblImg3.setIcon(new ImageIcon(strTmp2));}}}}});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 JButton btnClass = new JButton("班级");btnClass.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {JFileChooser jfc=new JFileChooser();//jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );//jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);jfc.setFileSelectionMode(JFileChooser.FILES_ONLY); File file=jfc.getSelectedFile();jfc.showDialog(new JLabel(), "选择");if(file.isDirectory()){  System.out.println("文件夹:"+file.getAbsolutePath());  }else if(file.isFile()){  System.out.println("文件:"+file.getAbsolutePath());  }  System.out.println(jfc.getSelectedFile().getName());// 把文件路径显示在文本框中tfClass.setText(file.getAbsolutePath());// 获取文件路径 与文件名strPath = file.getAbsolutePath();strFileName = jfc.getSelectedFile().getName();//获取文件夹下所有的文件  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("学生姓名");lbGuessName.setFocusable(true);lbGuessName.setBounds(259, 91, 102, 23);contentPane.add(lbGuessName);// 标签,显示第一个学生相片lblImg1 = new JLabel("学生一");lblImg1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {}});lblImg1.setForeground(Color.BLACK);lblImg1.setBounds(26, 151, 183, 178);contentPane.add(lblImg1);// 标签,显示第二个学生相片lblImg2 = new JLabel("学生二");lblImg2.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {}});lblImg2.setForeground(Color.BLACK);lblImg2.setBounds(241, 155, 183, 172);contentPane.add(lblImg2);// 标签,显示第三个学生相片lblImg3 = new JLabel("学生三");lblImg3.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent arg2) {}});lblImg3.setForeground(Color.BLACK);lblImg3.setBounds(430, 155, 185, 172);contentPane.add(lblImg3);// 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名final JButton btnGuessAgain = new JButton("再猜");btnGuessAgain.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {if(e.getSource()==btnGuessAgain){ //当前时间作为种子,避免两个Random对象产生同样的数字序列//Random random = new Random(System.currentTimeMillis()); Random random = new Random();  ImageIcon icon;//定义icon变量  for(int i=0;i<3;i++)  {  index = random.nextInt(NUM_IMG);  String strTmp = fileArray[index].toString();  try {  icon = new ImageIcon(ImageIO.read(new File(strTmp)));  Image image = icon.getImage(); // 从图表中获取到图片  Image smallImage = image.getScaledInstance(100,100,Image.SCALE_FAST);// 缩放图像  icon = new ImageIcon(smallImage);//把Image文件转化为ImageIcon  //借鉴一下同学的if(index==NUM_IMG)  index = 0;  switch(i)  {  case 0:  lblImg1.setIcon(icon);  break;  case 1:  lblImg2.setIcon(icon);  break;  case 2:  lblImg3.setIcon(icon);  break;  }  } catch (IOException e1) {  e1.printStackTrace();  }     }}   }  });   btnGuessAgain.setBounds(223, 337, 93, 23);contentPane.add(btnGuessAgain);}}//随机数方式显示图片。。。还有文字随机显示,最后再考虑消息框


0 0