在之前的代码上添加
<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文件也能实现该功能,且能猜对的总是最后一个,不是随机的,还有就是三张图片有时候会出现两张一样的。
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-24 03:51:03