如何从访问附件字段中提取图像,然后将其放入jlabel中



我对Java来说是新手,因此我的知识是有限的。我有这个分配,必须从访问数据库中获取数据,并填充一个装满字段的对话框。我对典型字段没有任何问题,但是我碰到了试图使附件领域起作用的死胡同。

我已经尝试使用我在网络上看到的.getByte((方法,但我还没有完全掌握附件未访问类方法。谁能帮助我或指导我朝正确的方向指导?这是一些有关我如何填写其他字段的代码:

JTextField_cod_distrib.setText(result.getLong("Cod_distribuitor")+"");  
JCheckBox_in_stoc.setSelected(result.getBoolean("In_stoc"));
JTextField_pret.setText(result.getFloat("Pret")+"");     JTextField_denumire_produs.setText(result.getString("Denumire_produs")+"");
JTextField_cod_produs.setText(result.getInt("Cod_produs")+"");
JTextField_ambalaj.setText(result.getString("Ambalaj")+"");  

如果您知道数组中总是有一个附件,则可以做

jlabel.setIcon(new ImageIcon(getScaled(ImageIO.read(new ByteArrayInputStream(((Attachment[])result.getObject("attachment"))[0].getData())),120,120)));

否则,您必须为每个附件添加一个Jlabel:

JPanel attachmentPanel=new JPanel(new FlowLayout(FlowLayout.LEFT));
Attachment[] attachments=(Attachment[])result.getObject("attachment");
for(Attachment attachment:attachments) {
    Image original=ImageIO.read(new ByteArrayInputStream(attachment.getData()));
    attachmentPanel.add(new JLabel(new ImageIcon(getScaled(original,120,120))));
}
//add the attachmentPanel to your component

来自https://docs.oracle.com/javase/tutorial/uiswing/examples/components/icondemoproject/src/src/components/icondemoapp.java

/**
     * 
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    private BufferedImage getScaledImage(BufferedImage srcImg, int w, int h){
        double sw=srcImg.getWidth();
        double sh=srcImg.getHeight();
        double fw=w/sw;
        double fh=h/sh;
        if(fw<fh) w=(int)(sw*fh);
        else if(fh<fw) h=(int)(sh*fw);
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }

相关内容

  • 没有找到相关文章

最新更新