加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何在JLabel中调整Image / IconImage的大小?

发布时间:2020-12-15 08:43:12 所属栏目:Java 来源:网络整理
导读:这是我的代码: String s = "/Applications/Asphalt6.app";JFileChooser chooser = new JFileChooser();File file = new File(s);Icon icon = chooser.getIcon(file);// show the iconJLabel ficon = new JLabel(s,icon,SwingConstants.LEFT); 现在,从图标中
这是我的代码:

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);
        g.dispose();

        JOptionPane.showMessageDialog(
            null,new JLabel(new ImageIcon(bi)));
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读