《JAVA程序设计》第三次——《猜猜看》游戏

 

     这次仔细地把一些注释打了上去。希望自己能更好地理解与修改。这个小游戏还有很多需要改进的地方,不知道我还能不能做到我想要的效果。加油

import javax.imageio.ImageIO;  
import javax.swing.JFrame;  
import javax.swing.JPanel;  
import javax.swing.border.EmptyBorder;  
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.EventQueue;
import java.awt.Image;  
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.FileFilter;  
import java.io.IOException;  
import java.util.Random;  
  
public class SmallGame extends JFrame {  
    /**  
*  
*/  
    private static final long serialVersionUID = 1L;  
  
    private JPanel contentPane;  //添加面板容器
    private JTextField tfDir;  //添加文本输入框。用于显示目录文件的
    File[] fileArray; // 文件夹下所有文件  
    int NUM_IMG = 0; // 文件总数目  
    int index = 0; // 当前文件的序号  
    int i = 0;  
  
    JLabel jlbImg1 = null;  
    JLabel jlbImg2 = null;  
    JLabel jlbImg3 = null;


  
    
  
    /** 
     * Launch the application. 
     */  
    class myFileFilter implements FileFilter {  
  
        @Override  
        public boolean accept(File pathname) {  
            String filename = pathname.getName().toLowerCase();  
            if (filename.contains(".jpg")) {  
                return false;  
            } else {  
                return true;  
            }  
        }  
    }  
  //把这个事件添加到AWT的事件处理线程当中去
    public static void main(String[] args) {  
        EventQueue.invokeLater(new Runnable() {  
            public void run() {  
                try {  
                    SmallGame frame = new SmallGame();  
                    frame.setVisible(true);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
    }
    /** 
     * Create the frame. 
     */  
    public SmallGame() {  
        setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");  
      //setBounds(x,y,width,height); x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        
        setBounds(100, 100, 645, 500); 
        contentPane = new JPanel();   //内容面板
      //设置面板的边界,Border描述了面板四周的边界(属于面板内部),EmptyBorder是一个空白的边界;  
      //语句的意思是让contentPane内部边框为空,并且有5个像素的厚度,  
      //如果直接在contentPane上面添加一个按钮(设置为充满),那么按钮将铺满除了边框之外的内部矩形  

        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));  
        setContentPane(contentPane);  //初始化容器,用来在容器上添加一些控件
        contentPane.setLayout(null);  
  
        // 选择目录 按钮的处理程序  
        JButton btnDir = new JButton("选择文件");  
        btnDir.addActionListener(new ActionListener() {  //添加事件监听接口
            public void actionPerformed(ActionEvent e) {  
                JFileChooser jfc = new JFileChooser("E:/");  //打开默认的目录E盘
                jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);   //设置模式,这里是指显示文件和目录
                jfc.showDialog(new JLabel(), "选择");  
                File file = jfc.getSelectedFile();  
                tfDir.setText(file.getAbsolutePath()); //吧文件路径显示在文本框中 
                if (file != null && file.isDirectory()) {  //如果FILE不为空而且file是目录
                    
                    // 获取文件夹下所有的文件  
                    fileArray = file.listFiles();  //将列表中的文件添加进 fileArray数组  
                    NUM_IMG = fileArray.length;  //数组的长度等于文件的总数   
  
                }  
            }  
        });  
        //将按钮添加进容器
        btnDir.setBounds(26, 26, 93, 23);  
        contentPane.add(btnDir);  
  
        // 文本框,显示目录  
        tfDir = new JTextField();  
        tfDir.setEditable(false);  //使用这个函数的控件不能被编辑
        tfDir.setBounds(125, 27, 450, 21);  
        contentPane.add(tfDir);  
        tfDir.setColumns(10);  
   
        
          
        //设置提醒文字  
        JLabel tis = new JLabel("猜猜我是谁,请点我相片");  
        tis.setBounds(5, 100, 200, 33);  
        this.add(tis);  
  
        // 标签,显示带猜测学生姓名  
        final JLabel lbGuessName = new JLabel("称呼");  
        lbGuessName.setBounds(259, 91, 140, 23);  
        contentPane.add(lbGuessName);  
  
        // 标签,显示第一个学生相片  
        final JLabel lblImg1 = new JLabel("NO.1",JLabel.CENTER);  //JLabel.CENTER这个是为了居中显示
  
        lblImg1.addMouseListener(new MouseAdapter() {   
            public void mouseClicked(MouseEvent arg0) {  //监听,鼠标点击
                if (arg0.getSource() == lblImg1) {  //如果鼠标点击的是lblImg1  
                    if ((lblImg1.getText().equals(lbGuessName.getText()))) {  //且lblImg1的路径和正确答案相同 
                        JOptionPane.showMessageDialog(null, "YES!你猜对了!", "提示",  
                                JOptionPane.PLAIN_MESSAGE);  //弹出提示框 
  
                    } else {  
                        JOptionPane.showMessageDialog(null, "NO!你猜错了!", "错误",  
                                JOptionPane.ERROR_MESSAGE);  //否则就输出错误框    
                           

  
                    }  
  
                }  
            }  
        });  
        lblImg1.setBounds(26, 155, 150, 200);  
        contentPane.add(lblImg1);  
  
        // 标签,显示第二个学生相片  
        final JLabel lblImg2 = new JLabel("NO.2");  
        lblImg2.addMouseListener(new MouseAdapter() {  
            @Override  
            public void mouseClicked(MouseEvent arg1) {  
                if (arg1.getSource() == lblImg2) {  
                    if ((lblImg2.getText().equals(lbGuessName.getText()))) {  
                        JOptionPane.showMessageDialog(null, "YES!你猜对了!", "提示",  
                                JOptionPane.PLAIN_MESSAGE);  
  
                    } else {  
                        JOptionPane.showMessageDialog(null, "NO!你猜错了!", "错误",  
                                JOptionPane.ERROR_MESSAGE);  
  
                    }  
                }  
            }  
        });  
        lblImg2.setForeground(Color.BLACK);  
        lblImg2.setBackground(SystemColor.inactiveCaption);  
        lblImg2.setBounds(241, 155, 150, 200);  
        contentPane.add(lblImg2);  
  
        // 标签,显示第三个学生相片  
        final JLabel lblImg3 = new JLabel("NO.3");  
        lblImg3.addMouseListener(new MouseAdapter() {  
  
            @Override  
            public void mouseClicked(MouseEvent arg2) {  
                if (arg2.getSource() == lblImg3) {  
                    if ((lblImg3.getText().equals(lbGuessName.getText()))) {  
                        JOptionPane.showMessageDialog(null, "YES!你猜对了!", "提示",  
                                JOptionPane.PLAIN_MESSAGE);  
  
                    } else {  
                        JOptionPane.showMessageDialog(null, "NO!你猜错了!", "错误",  
                                JOptionPane.ERROR_MESSAGE);  
  
                    }  
                }  
  
            }  
        });  
        lblImg3.setBounds(434, 155, 150, 200);  
        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) { //如果是“再猜一次"按钮
                	Random random = new Random(System.currentTimeMillis());//随机数 
                	
                	ImageIcon icon;                	
					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 语句是一个控制语句,它通过将控制传递给其体内的一个 case 语句来处理多个选择和枚举
							switch(i){
							case 0:
								System.out.println(index);
								lblImg1.setIcon(icon);
								lblImg1.setText(filename1);
							
								break;
							case 1:
								System.out.println(index);  
								lblImg2.setIcon(icon);
								lblImg2.setText(filename1);
							

								break;
							case 2:
								System.out.println(index);  
								lblImg3.setIcon(icon);
								lblImg3.setText(filename1);
							

								break;

							}
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();

						}

					}
				     System.out.println(fileArray[index].getName());  
					 String filename = fileArray[index].getName();    
					 lbGuessName.setText(filename);    

				}

			}
		});
        
        btnGuessAgain.setBounds(223, 400, 93, 23);  
        contentPane.add(btnGuessAgain);  
    }  
}

 

 

希望今晚能让它更完善些。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值