《JAVA程序设计》实训第二天——《猜猜看》游戏

课程实训第二天,我在第一天的基础上去导入文件夹,第一天那时候一直修改都是修改不到,上网找了相关的知识,问了同学该怎么去导入显示图片。

public class weiwei extends JFrame {  
    /**  
*  
*/  
    private static final long serialVersionUID = 1L;  
  
    private JPanel contentPane;  
    private JTextField tfDir;  
    private JTextField tfClass;  
    File[] fileArray; // 文件夹下所有文件  
    int NUM_IMG = 0; // 文件总数目  
    int index = 0; // 当前文件的序号  
    int i = 0;  
  
    JLabel jlbImg1 = null;  
    JLabel jlbImg2 = null;  
    JLabel jlbImg3 = null;  
  
    private Graphics g;  
  
    /** 
     * 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;  
            }  
        }  
    }  
  
    public static void main(String[] args) {  
        EventQueue.invokeLater(new Runnable() {  
            public void run() {  
                try {  
                    weiwei frame = new weiwei();  
                    frame.setVisible(true);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
    }  
  
    /** 
     * Create the frame. 
     */  
    public weiwei() {  
        setTitle("\u731C\u731C\u770B\u6E38\u620FV0.1");  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        setBounds(100, 100, 645, 500);  
        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.showDialog(new JLabel(), "选择");  
                File file = jfc.getSelectedFile();  
                tfDir.setText(file.getAbsolutePath());  
                if (file != null && file.isDirectory()) {  
                    // 参考: java中File.listFiles(FileFilter) FileFilter的使用  
                    // http://zhouzaibao.iteye.com/blog/347557 ;  
  
                    // 获取文件夹下所有的文件  
                    fileArray = file.listFiles();  
                    NUM_IMG = fileArray.length;  
  
                }  
            }  
        });  
        btnDir.setBounds(26, 26, 93, 23);  
        contentPane.add(btnDir);  

在借鉴了同学的导入方法后,在自己修改的代码上可以点击进去选择文件了。


可以点击那个选择目录有反应了真的很开心,可以进一步去修改代码了。导入了下载的图片,可以实现猜猜看的显示图片的功能了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值