最近我在写一个Java项目,其中需要给按钮添加背景图片,这个倒是很简单,困难的是如何调整背景图片的大小,纠结了很久,终于在网上找到了一个解决方法,拿出来和大家分享一下

[java] view plaincopy
  1. public void setIcon(String file, JButton iconButton) {  
  2.         ImageIcon icon = new ImageIcon(file);  
  3.         Image temp = icon.getImage().getScaledInstance(iconButton.getWidth(),  
  4.                 iconButton.getHeight(), icon.getImage().SCALE_DEFAULT);  
  5.         icon = new ImageIcon(temp);  
  6.         iconButton.setIcon(icon);  
  7.     }  

 

其中,file是图片的路径,iconButton是按钮的变量名。封装成一个函数就是这样简单。

 

相关文章:

  • 2021-12-15
  • 2022-12-23
  • 2021-10-17
  • 2021-11-09
  • 2022-12-23
  • 2021-12-30
  • 2021-10-20
  • 2021-09-19
猜你喜欢
  • 2021-11-23
  • 2021-08-30
  • 2022-12-23
  • 2021-10-16
  • 2021-04-11
  • 2022-12-23
相关资源
相似解决方案