实习日志4----猜猜看最终实现成果

实现的结果其实并不算完整,还缺少一个内容---数据添加到数据库

但是基本上猜猜看的部分已经完成了

这次完成这个程序,在自己敲代码以及和咨询老师、同学下完成的

在这里谢谢曾经帮过我的小伙伴们~

接下来是我写代码时的一些笔记,大家不懂的时候可以参考一下

一、java swing 的jframe窗体代码中为什么总有

public static void main(String[] args) {    
        EventQueue.invokeLater(new Runnable() {    
            public void run() {    
                try {    
                    Daomu frame = new Daomu();    
                    frame.setVisible(true);    
                } catch (Exception e) {    
                    e.printStackTrace();    
                }    
            }    
        });    
    }    </span>
其实是为了 把这个事件添加到awt的事件处理线程当中去
awt的事件处理线程会按照队列的顺序依次调用每个待处理的事件来运行 
使用该方式的原因是:awt是单线程模式的,所有awt的组件只能在(推荐方式)事件处理线程中访问,从而保证组件状态的可确定性。


二、
java里ContentPane是干什么的?

与AWT组件不同,Swing组件不能直接添加到顶层容器中,

它必须添加到一个与Swing顶层容器相关联的内容面板(content pane)上。

内容面板是顶层容器包含的一个普通容器,它是一个轻量级组件。基本规则如下:
  (1)把Swing组件放入一个顶层Swing容器的内容面板上
  (2)避免使用非Swing的重量级组件。
 


其他与代码有关的解释,我都一一对应在相应的代码上了~

希望对大家有些微帮助

import java.awt.EventQueue;    
import java.awt.Image;  
  
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.event.ActionListener;    
import java.awt.event.ActionEvent;    
import java.awt.event.MouseAdapter;    
import java.awt.event.MouseEvent;    
import java.awt.image.BufferedImage;  
import java.io.File;    
import java.io.FileOutputStream;  
import java.io.FilenameFilter;  
import java.io.IOException;  
import java.util.Random;  
public class Daomu 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;    
    private JFileChooser fileChooser;  //添加文件选择对话框
  
    //把这个事件添加到awt的事件处理线程当中去
    public static void main(String[] args) {    
        EventQueue.invokeLater(new Runnable() {    
            public void run() {    
                try {    
                    Daomu frame = new Daomu();    
                    frame.setVisible(true);    
                } catch (Exception e) {    
                    e.printStackTrace();    
                }    
            }    
        });    
    }    
  
    /**  
     * Create the frame.  
     */    
    public Daomu() {  
    	//setBounds(x,y,width,height); x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的 
        setBounds(100, 100, 720, 500); 
        contentPane = new JPanel(); //内容面板  
        //设置面板的边界,Border描述了面板四周的边界(属于面板内部),EmptyBorder是一个空白的边界;
        //语句的意思是让contentPane内部边框为空,并且有5个像素的厚度,
        //如果直接在contentPane上面添加一个按钮(设置为充满),那么按钮将铺满除了边框之外的内部矩形
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));   
        setContentPane(contentPane);  //初始化容器,用来在容器上添加一些控件  
        contentPane.setLayout(null);    
  
        // 添加显示Image的JLabel控件    
        jlbImg = new JLabel();       
        jlbImg.setBounds(100, 100, 200, 200);    
        this.add(jlbImg);    
  
        JButton btnDir = new JButton("选择文件");  
        //设置按钮效果  
        btnDir.addActionListener(new ActionListener() {  //添加事件监听接口
            @Override  
            public void actionPerformed(ActionEvent e) {  
        fileChooser = new JFileChooser("E:/");//默认打开目录E盘  
        //设置模式,这里是指示显示文件和目录  
        fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );  
                fileChooser.showOpenDialog(null); //打开对话框 
                File file=fileChooser.getSelectedFile();  //得到选中的文件
                  
                if(file.isDirectory()){  //如果文件是目录
                    System.out.println("文件夹:"+file.getAbsolutePath());  
                      
                }else if(file.isFile()){  //如果是文件
                    System.out.println("文件:"+file.getAbsolutePath());  
                }  
                System.out.println(fileChooser.getSelectedFile().getName()); //输出路径 与文件名
                  
                // 把文件路径显示在文本框中  
                tfDir.setText(file.getAbsolutePath());  
             // 获取文件路径 与文件名    
                strPath = file.getAbsolutePath();    
                strFileName = fileChooser.getSelectedFile().getName();    
  
                if(file!=null && file.isDirectory()){   //如果file不为空且file是目录
             // 获取文件夹下所有的文件  
                fileArray = file.listFiles(); //将列表中的文件添加进 fileArray数组
                NUM_IMG = fileArray.length; //数组的长度等于文件的总数 
            }}  
        });  
        //将按钮添加进容器
        btnDir.setBounds(80, 30, 93, 23);  
        contentPane.add(btnDir);    
  
        // 文本框,显示目录    
        tfDir = new JTextField();    
        tfDir.setEditable(false);   // 使调用这个函数的控件不能被编辑 
        tfDir.setBounds(180, 30, 363, 23);    
        contentPane.add(tfDir);    
        tfDir.setColumns(10);    
  
        // 标签,显示猜测姓名    
        final JLabel lbGuessName = new JLabel("名字");  
        lbGuessName.setBounds(340, 80, 122, 23);    
        contentPane.add(lbGuessName);    
  
        // 标签,显示第一个相片    
        final JLabel lblImg1 = new JLabel("图片一",JLabel.CENTER);//JLabel.CENTER这个是为了居中显示    
        lblImg1.addMouseListener(new MouseAdapter() {    
            public void mouseClicked(MouseEvent arg0) { //鼠标点击 
                if(arg0.getSource()==lblImg1){  //如果鼠标点击的是lblImg1
                    if(( lblImg1.getText().equals(strPath+"/"+lbGuessName.getText()+".jpg"))){  //且lblImg1的路径和正确答案相同
                        JOptionPane.showMessageDialog(null,"恭喜你猜对了","提示",JOptionPane.PLAIN_MESSAGE);//弹出提示框  
  
                    }  
                    else {  
                        JOptionPane.showMessageDialog(null,"你猜错了!","错误",JOptionPane.ERROR_MESSAGE);//否则就输出错误框  
  
                    }    
                }  
            }    
        });    
        lblImg1.setBounds(30, 130, 200, 270);    
        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(250, 130, 200, 270);    
        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(470, 130, 200, 270);    
        contentPane.add(lblImg3);    
          
        // 下一题 按钮,点击则更新相应的三张图片 与 带猜测学生姓名    
        final JButton btnGuessAgain = new JButton("下一题");    
        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; //定义3个随机数 
                    for (;;) {  
                        i1 = r.nextInt(NUM_IMG);  //从0-最大文件数中随机 
                        i2 = r.nextInt(NUM_IMG);  //从0-最大文件数中随机
                        i3 = r.nextInt(NUM_IMG);  //从0-最大文件数中随机
                        if (i1 != i2 & i2 != i3 & i1 != i3) //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);//在3张图片中随机一个  
//                  try {  
//                      t=new ImageIcon(ImageIO.read(new File(strArry)));  
//                         Image img = ImageIO.read(file);  
//                         // 构造Image对象   
//                         BufferedImage tag = new BufferedImage(190, 180,BufferedImage.TYPE_INT_RGB);  
//                         tag.getGraphics().drawImage(img, 0, 0, 190, 180, null);   
//                     } catch (IOException e1) {  
//                         e1.printStackTrace();  
//                     }  
  
                    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, 420, 93, 23);    
        contentPane.add(btnGuessAgain);    
    }    
}

实现的结果:





在实现的时候,发现按钮中的文字以及图片的名字字体有点小

所以想修改下按钮中文字的字体大小以及名字标签的字体大小

这里只举例“选择文件”按钮文字的修改,其他都是相同的方法添加

 Font f1 = new Font("楷体",Font.BOLD,14);//设置字体大小
        btnDir.setFont(f1);

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值