手头上的项目是基于Swing的,有个功能是从数据库中取出id='XXX'的图片数据(二进制),返回多行(一个ID对应多个图片,类似于淘宝图片展示),然后显示到JLabel上,我写的代码如下,问题是无法显示,而且不知道如何去通过鼠标事件,触发图片交换显示。void initImage() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con;
            con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\NetBeansProjects\\data\\data.mdb", "", null);
            Statement stmt = mycon.con.createStatement();
            String querycommand = "SELECT image FROM imageTable WHERE imageID='" + id + "'";
            ResultSet rs = stmt.executeQuery(querycommand);
            while (rs.next()) {
                System.out.println(querycommand);
                byte b[] = new byte[1024 * 1024];
                int c = 0;
                try {
                    File f = new File("Icon//demo.jpg");//先指定一临时图片,存放从数据库中读出的图片
                    FileOutputStream fos = new FileOutputStream(f);
                    InputStream is = rs.getBinaryStream("image");
                    while ((c = is.read(b)) != -1) {
                        fos.write(b, 0, c);
                    }
                    fos.flush();
                    fos.close();
                    is.close();
                } catch (FileNotFoundException enfe) {
                } catch (IOException ioe) {
                }
                jLabelImage.setIcon(new ImageIcon(b));//将读出的图片设置为按钮的图标
            }
            stmt.close();
            mycon.con.close();
        } catch (SQLException sqle) {
        }
    }
问题一,我应该如何取图片并让图片显示在JLabel上?
问题二,如何写一个鼠标事件处理函数,让它实现多个图片交换显示在JLabel上?

解决方案 »

  1.   

    问题1
    因为你是放在循环做的,也就是说你的JLabel可能还没来得及载入图片,你的数据库又读出下一张图片把文件覆盖了,直接用流的方式不挺好吗?不需要临时文件
                    try {
                        InputStream is = rs.getBinaryStream("image");
                        ByteArrayOutputStream bao = new ByteArrayOutputStream();
                        while ((c = is.read(b)) != -1) {
                            bao.write(b, 0, c);
                        }
                        is.close();
                   ...
                     jLabelImage.setIcon(new ImageIcon(bao.toByteArray()));
    问题2
    你要在哪个控件上写事件,可以用MouseAdapter,或者自己决定用什么Mouse的监听器,然后在控件上注册监听器就可以了
      

  2.   

    楼上说得很对,将起放在循环外面,然后可以将JLabel放到JPanel里面更好一点,这样不便于出错,
    //给控件增加增听事件
    jb_Sumbit.addActionListener(new ActionListener(){ @Override
    public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    int answer = JOptionPane.showConfirmDialog(null,"提交");
    if(answer==JOptionPane.OK_OPTION){
    System.out.println("你选择确定");
    }else if(answer==JOptionPane.CANCEL_OPTION){
    System.out.println("你选择了取消");
    }else{
    System.out.println("你选择了否");
    }
    }

    });
      

  3.   


    for example
    ResultSet rs = null; //用个成员变量保存数据库检索结果    void initImage() { //你原来的方法
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection con;
                con = DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=D:\\NetBeansProjects\\data\\data.mdb", "", null);
                Statement stmt = mycon.con.createStatement();
                String querycommand = "SELECT image FROM imageTable WHERE imageID='" + id + "'";
                //ResultSet rs = stmt.executeQuery(querycommand);
                rs = stmt.executeQuery(querycommand);
    /* 以下显示照片处理不要,放到按钮事件
                while (rs.next()) {
                    System.out.println(querycommand);
                    byte b[] = new byte[1024 * 1024];
                    int c = 0;
                    try {
                        File f = new File("Icon//demo.jpg");//先指定一临时图片,存放从数据库中读出的图片
                        FileOutputStream fos = new FileOutputStream(f);
                        InputStream is = rs.getBinaryStream("image");
                        while ((c = is.read(b)) != -1) {
                            fos.write(b, 0, c);
                        }
                        fos.flush();
                        fos.close();
                        is.close();
                    } catch (FileNotFoundException enfe) {
                    } catch (IOException ioe) {
                    }
                    jLabelImage.setIcon(new ImageIcon(b));//将读出的图片设置为按钮的图标
                }
                stmt.close();
                mycon.con.close();
    */
            } catch (SQLException sqle) {
            }
        }JButton jbtn = new JButton("下一张");
    jbtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) { //按钮事件
            try {
                if (rs.next()) { //从结果集取出一张照片显示
                    InputStream is = rs.getBinaryStream("image");
                    ByteArrayOutputStream bao = new ByteArrayOutputStream();
                    while ((c = is.read(b)) != -1) {
                        bao.write(b, 0, c);
                    }
                    is.close();
                    jLabelImage.setIcon(new ImageIcon(bao.toByteArray()));
                }
            } catch (Exception e){e.printStackTrace();}
        }
    });
    //把按钮放到你的面板或者窗体上
    your_frame.add(jbtn);
      

  4.   

    这些对我简直太有用了,我看我找到结贴的理由了。呵呵,再弱弱的问一个问题,我感觉上面写的可能对数据库操作有问题,如果我用List存放这些照片,然后从list中取,并显示,这样你感觉咋样?
    代码怎么写?