【问题标题】:Scaling an image on a button which is resized在调整大小的按钮上缩放图像
【发布时间】:2021-11-22 14:28:41
【问题描述】:

我正在开发一个允许用于放置控件、移动、调整大小等的应用程序。但我正在尝试将图标图像添加到按钮控件。放置和实例化时,它会根据以下代码调整图标图像的大小。

但是,当我使用用户功能调整控件大小并再次调用此路由时,它无法调整图像大小并且它仍然是原始图标大小。我试过使用“this.”,将控件传递给它自己,我已经完成打印以确保它看到新的大小和宽度......我错过了什么?

此外,当我创建第二个控件(或第三个等)时,它使用第一张图像的初始大小。 谢谢!

protected void sizeIcon () {
    try {
           File f2 = new File("media\\button.gif");
           BufferedImage inputImage = ImageIO.read(f2);
           BufferedImage img = new BufferedImage(this.getWidth(), this.getHeight(), inputImage.getType());
           Graphics2D g = img.createGraphics();
           g.drawImage(inputImage, 7, 0, this.getWidth(), this.getHeight(), null);
           ImageIO.write(img, "gif", new File("test.gif"));
           this.setIcon(new ImageIcon("test.gif"));
           g.dispose();

        } catch(Exception e) {System.out.println(e);}
   

【问题讨论】:

    标签: java swing icons jbutton image-resizing


    【解决方案1】:

    抱歉,知道了,看来旧文件没有被替换。 -MH

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2023-01-26
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2012-08-29
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    相关资源
    最近更新 更多