您的位置:首页 > 编程语言 > Java开发

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

2015-07-01 09:57 639 查看
第一部分:实验项目
项目一:随机相片显示。

目的:了解随机数的生成,及图片的显示。

目标:

(1)在已有代码的基础上,完成随机(或 顺序)显示三张图片的功能。

(2)当猜测准确,或错误时,弹出对话框进行回应。

首先我运行了老师给的代码,发现并不能选择路径上的图片位置,所以以我打算从这里做起
从网上找了些资料,补全了一些地方
import java.io.IOException;  

import java.util.EventObject;

import java.awt.Image;

import javax.swing.AbstractButton;  

import javax.swing.ImageIcon;

import javax.imageio.ImageIO;

import javax.swing.JFileChooser;     

import java.io.File;

import java.awt.EventQueue;     

import javax.swing.JFrame;  

import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;

import javax.swing.JButton; 

import javax.swing.JTextField;  

import javax.swing.JLabel;  

import java.awt.Color;

import java.awt.SystemColor;  

import java.awt.event.ActionListener;  

import java.awt.event.ActionEvent;  

import java.awt.event.MouseAdapter;  

import java.awt.event.MouseEvent;

         

public class bbq extends JFrame {  

    /** 

     *  

     */  

    private static final long serialVersionUID = 1L;  

    String strPath = "";    //文件夹路径  

    String strFileName = "";    //文件名称  

      

    File[] fileArray;   // 文件夹下所有文件  

    int NUM_IMG = 0;    // 文件总数目  

    int index   = 0;       

      

    private JPanel contentPane;  

    private JTextField tfDir;  

    private JTextField tfClass;  

    /** 

     * Launch the application. 

     */  

    public static void main(String[] args) {  

        EventQueue.invokeLater(new Runnable() {  

            public void run() {  

                try {  

                    bbq frame = new bbq();  

                    frame.setVisible(true);  

                } catch (Exception e) {  

                    e.printStackTrace();  

                }  

            }  

        });  

    }  

    /** 

     * Create the frame. 

     */  

    public bbq() {  

        setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");  

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        setBounds(100, 100, 645, 409);  

        contentPane = new JPanel();  

        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));  

        setContentPane(contentPane);  

        contentPane.setLayout(null);  

          

        // 选择目录 按钮的处理程序  

        JButton btnDir = new JButton("\u9009\u62E9\u76EE\u5F55");  

        btnDir.addActionListener(new ActionListener() {  

            public void actionPerformed(ActionEvent arg0) {  

                    

                      JFileChooser jfc=new JFileChooser();  

                      //jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );  

                      jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);  

                      jfc.showDialog(new JLabel(), "选择");  

                      File file=jfc.getSelectedFile();                      

                      tfDir.setText(file.getAbsolutePath());                                          

                      strPath = file.getAbsolutePath();  

                      strFileName = jfc.getSelectedFile().getName();  

            }  

        });  

        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);  

    

          

        // 选择班级 按钮的处理程序  

        JButton btnClass = new JButton("\u9009\u62E9\u73ED\u7EA7");  

        btnClass.addActionListener(new ActionListener() {  

            public void actionPerformed(ActionEvent e) {  

                    JFileChooser jfc=new JFileChooser();  

                    //jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );  

                    jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);  

                    jfc.showDialog(new JLabel(), "选择");  

                    File file=jfc.getSelectedFile();    

                    tfClass.setText(file.getAbsolutePath());   

            }  

        });  

        btnClass.setBounds(26, 59, 93, 23);  

        contentPane.add(btnClass);  

          

        // 文本框,显示班级文件  

        tfClass = new JTextField();  

        tfClass.setEditable(false);  

        tfClass.setBounds(125, 60, 363, 21);  

        contentPane.add(tfClass);  

        tfClass.setColumns(10);  

          

        // 标签,显示带猜测学生姓名  

        JLabel lbGuessName = new JLabel("\u59D3\u540D");  

        lbGuessName.setBounds(259, 91, 102, 23);  

        contentPane.add(lbGuessName);  

          

        // 标签,显示第一个学生相片  

        JLabel lblImg1 = new JLabel("\u56FE\u72471");  

        lblImg1.setBackground(Color.RED);  

        this.add(lblImg1);  

          

        lblImg1.addMouseListener(new MouseAdapter() {  

            @Override  

            public void mouseClicked(MouseEvent arg0) {  

                   

             }  

         });  

        lblImg1.setBounds(26, 151, 183, 178);  

        contentPane.add(lblImg1);  

          

        // 标签,显示第二个学生相片  

        JLabel lblImg2 = new JLabel("\u56FE\u72472");  

        lblImg2.addMouseListener(new MouseAdapter() {  

            @Override  

            public void mouseClicked(MouseEvent e) {  

                  

            }  

        });  

        lblImg2.setForeground(Color.BLACK);  

        lblImg2.setBackground(SystemColor.inactiveCaption);  

        lblImg2.setBounds(241, 155, 183, 172);  

        contentPane.add(lblImg2);  

          

        // 标签,显示
4000
第三个学生相片  

        JLabel lblImg3 = new JLabel("\u56FE\u72473");  

        lblImg3.addMouseListener(new MouseAdapter() {  

            @Override  

            public void mouseClicked(MouseEvent e) {  

                  

            }  

        });  

        lblImg3.setBounds(434, 155, 185, 172);  

        contentPane.add(lblImg3);  

          

        // 再猜一次 按钮,点击则更新相应的三张图片 与 带猜测学生姓名  

        JButton btnGuessAgain = new JButton("\u518D\u731C\u4E00\u6B21");  

        btnGuessAgain.addActionListener(new ActionListener() {  

            public void actionPerformed(ActionEvent e) {  

                  

            }  

        });  

        btnGuessAgain.setBounds(223, 337, 93, 23);  

        contentPane.add(btnGuessAgain);  

    }  
}  





选择路径完成,后面的还需要慢慢修改与完善。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: