【问题标题】:Image desapears in JPanel when read from database从数据库读取时图像在 JPanel 中消失
【发布时间】:2014-04-12 13:52:43
【问题描述】:

晚上好,

我有这部分代码:

我的问题是当我尝试显示 BLOB 类型的图像时。我正在使用Netbeans IDE 和apach derby 数据库。

像这样一切都很好,但是如果我删除该行:JOptionPane.showMessageDialog(null, "hello"); 图像会快速消失和消失,

String req = "select * from viewpiece where idpiece = "+idpiece;
        myConnection = MyConnection.getMyConnection() ;
        ResultSet rs = myConnection.executeSQL(req);  

        try {   
            if (rs.next()) {

                TextFieldValeur.setText(rs.getString(2));
                TextFieldMonnaie.setText(rs.getString(5));
                TextFieldPays.setText(rs.getString(20));

                Blob Rectoblob = rs.getBlob(21);
                byte[] RectoByte = Rectoblob.getBytes(1, (int)(Rectoblob.length()));
                 try {
                    rectoBufferedImage = ImageIO.read(new ByteArrayInputStream(RectoByte));
                    g = panelImageRecto.getGraphics();

                   JOptionPane.showMessageDialog(null,  "hello");
                   g.drawImage(rectoBufferedImage, 10, 20,175,175, null);

                } catch (IOException ex) {
                    Logger.getLogger(PieceDetaillee.class.getName()).log(Level.SEVERE, null, ex);
                }

            }

        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(rootPane, "Erreur recherche de details\n"+req);
        } 

问题出在哪里?以及为什么显示消息 Hello 的那一行是一个解决方案?

有什么帮助吗?非常感谢。

【问题讨论】:

    标签: java swing jpanel blob


    【解决方案1】:

    您的问题是您使用 getGraphics() 获取 Graphics 上下文,然后使用它进行绘图。此 Graphics 对象是短暂的,并且不是在 Swing GUI 中绘制图像的安全方法。更好的是使用 JVM 为您提供的 Graphics 对象在 JPanel 的 paintComponent 方法中绘制图像。或者将其显示为 JLabel 中的 ImageIcon。

    【讨论】:

    • 我想我明白了……谢谢你,气垫船先生
    猜你喜欢
    • 2012-07-16
    • 2010-11-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多