JAVA实验第四天

。。。先放图,睡醒再上代码。。



经过了四天(实际上是两天)的艰苦奋斗,不懈努力,废寝忘食。。。。。。说多都是泪啊,总之,经过了多天的敲打代码以及各种百度,谷歌,ctrl+c和ctrl+v按了不下几百次了吧,还有Back键也是总之,编程就是一门锻炼你意志力的课程=-=也看得出程序猿是有多么的辛苦多么的累啦。当然,当这么多天的努力终于实现了项目的时候你会发现-------------->>>>哇~好想睡觉!!!!(说笑)  你会发现,幸福总是来得太突然,一切的努力觉得都值了=-=

这次的小游戏弄得比较简洁,本来还想添加多几个控件的,比如统计正确错误的次数等等,添加背景图片等等,不过没什么时间去设计,毕竟上午上Java下午还有其他实验课,然后一天下来要完成两个实验课的实验内容也是挺不简单的,所以,先把代码放在这里,等我有空了再来完善完善。也希望各位能带我装逼带我飞,多帮我改善下代码。编程就是要大家互帮互助,多多提问。谢谢大家啦,也谢谢老师的教导。


那么,先说说这几天做的这个猜猜看游戏吧,首先,要有一个面板,然后,要有标签按钮,文本框;然后,要给按钮弄个监听,要实现选择文件夹并获得文件夹的路径以及文件;然后,要能随机生成三张图片以及某张图片对应的姓名~然后,显示图片的三个标签也要弄个鼠标监听,用来实现判断猜的对不对。

基本方法就是---->>将图片以名字命名------>>>获取文件并放进数组------>>>产生三个不重复的随机数组成数组,让放文件的数组调用并产生三张图片----->>>将产生的图片再放进数组,随机显示到标签上------->>>将其中一个的姓名显示到标签-------->>>好,完毕


--------------------------------------------------------------我是分割线别打我-------------------------------------------------------------------

运行游戏时,点击选择目录打开装有相片的文件夹,然后点再猜一次开始游戏

点击下载相片文件(提取码iepi)另外,我这里没有弄Icon来实现缩放图片的功能,因此需要将文件夹里的图片修改成200*200大小

下面上代码--------------->>>>>

package lj;

import java.awt.EventQueue;  
import javax.swing.JFrame;  
import javax.swing.JPanel;  
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.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 extends JFrame {  
	/** 
	 *  
	 */  
	private static final long serialVersionUID = 1L;  

	private JPanel contentPane;  
	private JTextField tfDir;  
	File[] fileArray;   // 文件夹下所有文件  
	int NUM_IMG = 0;    // 文件总数目  
	String strPath = "";    //文件夹路径  
	String strFileName = "";    //文件名称  
	JLabel jlbImg = null;  //显示图片的标签

	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("                                                猜猜看游戏V0.1");  
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
		setBounds(100, 100, 645, 409);  
		contentPane = new JPanel(); 
		setContentPane(contentPane);  
		contentPane.setLayout(null); 

		/******************************************
		 ***********添加显示Image的JLabel控件*******  
		 *******************************************/

		jlbImg = new JLabel();  
		jlbImg.setBackground(Color.RED);  
		jlbImg.setBounds(100, 100, 200, 200);  
		this.add(jlbImg);  

		//*************** 选择目录 按钮的处理程序 *******************************
		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.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());  

					// 获取文件路径 与文件名  
					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(279, 108, 122, 23);  
		lbGuessName.setForeground(Color.red);
		contentPane.add(lbGuessName);  
		//*******************************************************************

		//********************************* 标签,显示第一个学生相片********************************************* 
		final JLabel lblImg1 = new JLabel("学生の一",JLabel.CENTER);  
		lblImg1.addMouseListener(new MouseAdapter() {  
			public void mouseClicked(MouseEvent arg0) {
				if(arg0.getSource()==lblImg1){
					if(( lblImg1.getText().equals(strPath+"/"+lbGuessName.getText()+".jpg"))){
						JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);

					}
					else {
						JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);

					}  
				}
			}  
		});  
		lblImg1.setBounds(26, 155, 183, 178);  
		contentPane.add(lblImg1);  
		//****************************************************************************************************
*
		// ******************************标签,显示第二个学生相片  *********************************************
		final JLabel lblImg2 = new JLabel("学生の二",JLabel.CENTER);  
		lblImg2.addMouseListener(new MouseAdapter() {  
			@Override  
			public void mouseClicked(MouseEvent arg1) {  
				if(arg1.getSource()==lblImg2){
					if(( lblImg2.getText().equals(strPath+"/"+lbGuessName.getText()+".jpg"))){
						JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);

					}
					else {
						JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);

					}  
				}
			}  
		});  
		lblImg2.setBounds(228, 155, 183, 172);  
		contentPane.add(lblImg2);  
		//****************************************************************************************************

		// *************************************标签,显示第三个学生相片 ***************************************
		final JLabel lblImg3 = new JLabel("学生の三",JLabel.CENTER);  
		lblImg3.addMouseListener(new MouseAdapter() {  
			@Override  
			public void mouseClicked(MouseEvent arg2) {  
				if(arg2.getSource()==lblImg3){
					if(( lblImg3.getText().equals(strPath+"/"+lbGuessName.getText()+".jpg"))){
						JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);

					}
					else {
						JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);

					}  
				}

			}  
		});  
		lblImg3.setBounds(430, 155, 183, 172);  
		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){ 

					//将路径下的所有.jpg文件放进数组names
					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[] a = new int[3];
					int i1, i2, i3;
					for (;;) {
						i1 = r.nextInt(NUM_IMG);
						i2 = r.nextInt(NUM_IMG);
						i3 = r.nextInt(NUM_IMG);
						if (i1 != i2 & i2 != i3 & i1 != i3)
							break;
					}//保证三个随机数不重复
					a[0] = i1;
					a[1] = i2;
					a[2] = i3;

					String imageSrc = strPath+"/"+names[a[0]];  
					String imageSrc1 = strPath+"/" +names[a[1]];  
					String imageSrc2 =strPath+ "/"+names[a[2]]; 

					//将产生的三个随机图片放进数组strArry,以便后面调用
					String[] strArry = {imageSrc,imageSrc1,imageSrc2};
					int j = r.nextInt(3);

					lblImg1.setIcon(new ImageIcon(imageSrc));  
					lblImg2.setIcon(new ImageIcon(imageSrc1));  
					lblImg3.setIcon(new ImageIcon(imageSrc2));

					//将图片路径显示在标签上
					lblImg1.setText(imageSrc);
					lblImg2.setText(imageSrc1);
					lblImg3.setText(imageSrc2);


					File fb = new File(strArry[j]);//File对象 fb 调用strArry里的随机产生的三个图片
					String fileName = fb.getName();//获得随机产生的图片的名字
					String stname = fileName.substring(0,fileName.lastIndexOf("."));//去掉前缀以及扩展名
					lbGuessName.setText(stname);//将得到的含有名字的String对象stname显示到界面中间的姓名标签上

				}

			}  

		});  
		btnGuessAgain.setBounds(283, 337, 93, 23);  
		contentPane.add(btnGuessAgain);  
		//********************************************************************************************************		
	}  

}  


我居然猜错了陈镜宇是哪个!大哭


高端一点,弄个图片按钮吧


下面是修改过的代码

import java.awt.EventQueue;  
import java.awt.Font;

import javax.swing.JFrame;  
import javax.swing.JPanel;  
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.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 extends JFrame {  
	/** 
	 *  
	 */  
	private static final long serialVersionUID = 1L;  

	private JPanel contentPane;  
	private JTextField tfDir;  
	File[] fileArray;   // 文件夹下所有文件  
	int NUM_IMG = 0;    // 文件总数目  
	String strPath = "";    //文件夹路径  
	String strFileName = "";    //文件名称  
	JLabel jlbImg = null;  //显示图片的标签

	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("                                                猜猜看游戏V0.1");  
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
		setBounds(100, 100, 645, 409);  
		contentPane = new JPanel(); 
		setContentPane(contentPane);  
		contentPane.setLayout(null); 

		/******************************************
		 ***********添加显示Image的JLabel控件*******  
		 *******************************************/

		jlbImg = new JLabel();    
		jlbImg.setBounds(100, 100, 200, 200);  
		this.add(jlbImg);  

		//*************** 选择目录 按钮的处理程序 *******************************
		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.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());  

					// 获取文件路径 与文件名  
					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);  
		//****************************************************

		// *****************标签,显示带猜测学生姓名 ************************** 
		JLabel onename = new JLabel("某科学の学生姓名--------->>>>");
		onename.setBounds(80,108,190,23);
		onename.setForeground(Color.blue);
		contentPane.add(onename);
		
		final JLabel lbGuessName = new JLabel(  "学生の姓名",JLabel.CENTER);  
		lbGuessName.setBounds(279, 88, 128, 53);  
		lbGuessName.setForeground(Color.red);
		lbGuessName.setFont(new Font("宋体", Font.BOLD, 24));
		lbGuessName.setOpaque(true);
		lbGuessName.setBackground(Color.white);
		contentPane.add(lbGuessName);  
		//*******************************************************************

		//********************************* 标签,显示第一个学生相片********************************************* 
		final JLabel lblImg1 = new JLabel("学生の一",JLabel.CENTER);  
		lblImg1.addMouseListener(new MouseAdapter() {  
			public void mouseClicked(MouseEvent arg0) {
				if(arg0.getSource()==lblImg1){
					if(( lblImg1.getText().equals(strPath+"/"+lbGuessName.getText()+".jpg"))){
						JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);

					}
					else {
						JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);

					}  
				}
			}  
		});  
		lblImg1.setBounds(26, 155, 183, 178);  
		contentPane.add(lblImg1);  
		//****************************************************************************************************

		// ******************************标签,显示第二个学生相片  *********************************************
		final JLabel lblImg2 = new JLabel("学生の二",JLabel.CENTER);  
		lblImg2.addMouseListener(new MouseAdapter() {  
			@Override  
			public void mouseClicked(MouseEvent arg1) {  
				if(arg1.getSource()==lblImg2){
					if(( lblImg2.getText().equals(strPath+"/"+lbGuessName.getText()+".jpg"))){
						JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);

					}
					else {
						JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);

					}  
				}
			}  
		});  
		lblImg2.setBounds(228, 155, 183, 172);  
		contentPane.add(lblImg2);  
		//****************************************************************************************************

		// *************************************标签,显示第三个学生相片 ***************************************
		final JLabel lblImg3 = new JLabel("学生の三",JLabel.CENTER);  
		lblImg3.addMouseListener(new MouseAdapter() {  
			@Override  
			public void mouseClicked(MouseEvent arg2) {  
				if(arg2.getSource()==lblImg3){
					if(( lblImg3.getText().equals(strPath+"/"+lbGuessName.getText()+".jpg"))){
						JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);

					}
					else {
						JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);

					}  
				}

			}  
		});  
		lblImg3.setBounds(430, 155, 183, 172);  
		contentPane.add(lblImg3);  
		//***************************************************************************************************
		final JButton btnGuessExit = new JButton("退         出");  
		btnGuessExit.addActionListener(new ActionListener() {  
			public void actionPerformed(ActionEvent e1) {  
				if(e1.getSource()==btnGuessExit){ 
					System.exit(0);
				}
					
				}
		});
		ImageIcon icon = new ImageIcon("D:\\1.jpg");
		btnGuessExit.setIcon(icon);
		btnGuessExit.setBounds(123, 340, 100, 30);  
		contentPane.add(btnGuessExit);  
		// ************************再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名 *********************** 
		final JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");  
		btnGuessAgain.addActionListener(new ActionListener() {  
			public void actionPerformed(ActionEvent e) {  
				if(e.getSource()==btnGuessAgain){ 

					//将路径下的所有.jpg文件放进数组names
					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[] a = new int[3];
					int i1, i2, i3;
					for (;;) {
						i1 = r.nextInt(NUM_IMG);
						i2 = r.nextInt(NUM_IMG);
						i3 = r.nextInt(NUM_IMG);
						if (i1 != i2 & i2 != i3 & i1 != i3)
							break;
					}//保证三个随机数不重复
					a[0] = i1;
					a[1] = i2;
					a[2] = i3;

					String imageSrc = strPath+"/"+names[a[0]];  
					String imageSrc1 = strPath+"/" +names[a[1]];  
					String imageSrc2 =strPath+ "/"+names[a[2]]; 

					//将产生的三个随机图片放进数组strArry,以便后面调用
					String[] strArry = {imageSrc,imageSrc1,imageSrc2};
					int j = r.nextInt(3);

					lblImg1.setIcon(new ImageIcon(imageSrc));  
					lblImg2.setIcon(new ImageIcon(imageSrc1));  
					lblImg3.setIcon(new ImageIcon(imageSrc2));

					//将图片路径显示在标签上
					lblImg1.setText(imageSrc);
					lblImg2.setText(imageSrc1);
					lblImg3.setText(imageSrc2);


					File fb = new File(strArry[j]);
					String fileName = fb.getName();
					String stname = fileName.substring(0,fileName.lastIndexOf("."));//去掉前缀以及扩展名
					lbGuessName.setText(stname);

				
				}

			}  
			
		});  
		ImageIcon icon1 = new ImageIcon("D:\\2.jpg");
		btnGuessAgain.setIcon(icon1);
		btnGuessAgain.setBounds(353, 340, 100, 30);  
		contentPane.add(btnGuessAgain);  
		//********************************************************************************************************		
	}  

}  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值