如何在 JLabel 中调整图像/图标图像的大小?

2024-01-25

这是我的代码:

String s = "/Applications/Asphalt6.app";
JFileChooser chooser = new JFileChooser();

File file = new File(s);
Icon icon = chooser.getIcon(file);

// show the icon
JLabel ficon = new JLabel(s, icon, SwingConstants.LEFT);

现在,从图标中提取的图像非常小。我怎样才能调整它的大小?


import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;

class BigIcon {

    public static void main(String[] args) {
        JFileChooser chooser = new JFileChooser();
        File f = new File("BigIcon.java");
        Icon icon = chooser.getIcon(f);

        int scale = 4;

        BufferedImage bi = new BufferedImage(
            scale*icon.getIconWidth(),
            scale*icon.getIconHeight(),
            BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bi.createGraphics();
        g.scale(scale,scale);
        icon.paintIcon(null,g,0,0);
        g.dispose();

        JOptionPane.showMessageDialog(
            null,
            new JLabel(new ImageIcon(bi)));
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 JLabel 中调整图像/图标图像的大小? 的相关文章

随机推荐