• 《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("u731Cu731Cu770Bu6E38u620FV0.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);  
      
            // 选择目录 button的处理程序  
            JButton btnDir = new JButton("u9009u62E9u76EEu5F55");  
            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);  

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


    能够点击那个选择文件夹有反应了真的非常开心。能够进一步去改动代码了。导入了下载的图片,能够实现猜猜看的显示图片的功能了。



  • 相关阅读:
    java内存模型
    如何保证消费者接收消息的顺序
    mysql事务隔离级别
    mysql加锁读
    mysql一致性读
    InnoDB锁
    JDK1.8中的线程池
    JDK1.8中HashMap实现
    物品推荐(基于物品的协同过滤算法)
    CRM 2013 生成自动编号
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7068460.html
Copyright © 2020-2023  润新知