gpt4 book ai didi

java - 如何从 Access 附件字段中提取图像并将其放入 JLabel 中

转载 作者:行者123 更新时间:2023-12-02 09:51:56 25 4
gpt4 key购买 nike

我对 Java 还很陌生,所以我的知识有限。我有这样的作业,我必须从 Access 数据库获取数据并填充一个充满字段的对话框。我对典型字段没有任何问题,但我在尝试使附件字段工作时遇到了死胡同。

我尝试过使用我在网上看到的 .getByte() 方法,但我还不太掌握 Attachment uncanaccess 类方法。有人可以帮助我或引导我走向正确的方向吗?以下是一些有关我如何填写其他字段的引用代码:

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/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;
}

关于java - 如何从 Access 附件字段中提取图像并将其放入 JLabel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56261673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com