Java

时间:2017-05-05 17:59:16

标签: java image-resizing

为什么此代码不会根据jLabel大小调整图像大小?为什么图像不适合jLabel?我做错了什么?在研究了大量关于图像大小调整的代码片段之后,这是我到目前为止所做的代码,

private void btn_browseActionPerformed(java.awt.event.ActionEvent evt) {

    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    filename = f.getAbsolutePath();

    try {

        BufferedImage img = ImageIO.read(new File(filename));
        int width = lbl_img.getWidth();
        int height = lbl_img.getHeight();
        Image image = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage buffered = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        buffered.getGraphics().drawImage(image, 0, 0, null);

        ImageIcon imageIcon = new ImageIcon(buffered);
        lbl_img.setIcon(imageIcon);
        txt_imgpath.setText(filename);

        File file = new File(filename);
        FileInputStream fis = new FileInputStream(file);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        for (int readNum; (readNum = fis.read(buf)) != -1;) {
            bos.write(buf, 0, readNum);
        } {
            person_image = bos.toByteArray();
        }
    } catch(IOException ex) {
        Logger.getLogger(FoodsJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}
private ImageIcon format = null;
String filename = null;
byte[] person_image = null;

此代码不显示任何异常,但不能按我的意愿工作。它显示图像的实际大小,无论大小。不像jLabel的大小。对不起,这种新手类型的问题。我只是Java的初学者。我尝试了很多,但无法找出解决方案。有人请帮帮我。

编辑: 在通过单击jTable中的数据或通过搜索jTextField来检索数据时,图像显示为实际大小,无论大小。不像jLabel的大小。我也需要在该代码中做些什么吗?这是代码,

private void tbl_foodsMouseClicked(java.awt.event.MouseEvent evt) {                                       
    int raw = tbl_foods.getSelectedRow();
    String Table_Click = (tbl_foods.getModel().getValueAt(raw, 0).toString());
    try
    {
        String sql = "Select Image from Product_food where ID='"+Table_Click+"'";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        if(rs.next())
        {
            byte[] imagedata = rs.getBytes("Image");
            format = new ImageIcon(imagedata);
            lbl_img.setIcon(format);

        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

    try
    {

        String sql = "Select * from Product_food where ID='"+Table_Click+"' ";
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        if(rs.next())
        {
            String add1 = rs.getString("ID");
            txt_proid.setText(add1);
            String add2 = rs.getString("Name");
            txt_name.setText(add2);
            Double add3 = rs.getDouble("Price");
            txt_price.setText(Double.toString(add3));
            Date add4 = rs.getDate("Date");
            txt_adddate.setDate(add4);
            byte[] add5 = rs.getBytes("Image");
            format = new ImageIcon(add5);
            lbl_img.setIcon(format);

        }
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, e);
    }
}    

0 个答案:

没有答案