码迷,mamicode.com
首页 > 编程语言 > 详细

《Java课程实习》日志(周三)

时间:2015-07-02 10:05:01      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

在之前的代码上添加
<span style="font-size:14px;">import java.util.Random; 
import javax.swing.JOptionPane; </span>


<span style="font-size:14px;">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,"no,猜错了","错误",JOptionPane.ERROR_MESSAGE);

					}  

				}
			} 
		});</span>

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,"no,猜错了","错误",JOptionPane.ERROR_MESSAGE);

					}  
				}
			}
		});


	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,"no,猜错了","错误",JOptionPane.ERROR_MESSAGE);
					}  
				}
			}
		});

把这代码
String strTmp = fileArray[index].toString();
					index++;
					if(index==NUM_IMG)
						index = 0;
					jlbImg1.setIcon(new ImageIcon(strTmp));

					index++;
					jlbImg2.setIcon(new ImageIcon(strTmp));
					index++;
					if(e.getSource()==btnGuessAgain ){ //如果是next按钮
						String strTmp2 = fileArray[index].toString();
						index++;

						jlbImg2.setIcon(new ImageIcon(strTmp2));

						index++;               
					}

				}   String strTmp1 = fileArray[index].toString();
				index++;
				jlbImg3.setIcon(new ImageIcon(strTmp1));   
			}
		});

改成

Random random = new Random(System.currentTimeMillis());//+
					ImageIcon icon;
					//int k[] = null;
					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(fileArray[index].getName());
								lblImg1.setIcon(icon);
								lblImg1.setText(filename1);
								break;
							case 1:
								System.out.println(fileArray[index].getName());  
								lblImg2.setIcon(icon);
								lblImg2.setText(filename1);
								break;
							case 2:
								System.out.println(fileArray[index].getName());  
								lblImg3.setIcon(icon);
								lblImg3.setText(filename1);
								break;
							}
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
				}
				@SuppressWarnings("unused")
				Random random1 = new Random(index);
				//i = index;
				System.out.println(fileArray[index].getName());
				String filename=fileArray[index].getName();
				lbGuessName.setText(filename);
			}
		});

就能实现以下功能技术分享技术分享



不足之处就是第二个文本框不选择txt文件也能实现该功能,且能猜对的总是最后一个,不是随机的,还有就是三张图片有时候会出现两张一样的。

版权声明:本文为博主原创文章,未经博主允许不得转载。

《Java课程实习》日志(周三)

标签:

原文地址:http://blog.csdn.net/u013910357/article/details/46722075

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!