java中图片自适应

自定义JLabel中的图片大小 
image.setImage(image.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT)); 

import java.awt.Image; 

import javax.swing.ImageIcon; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class Test extends JFrame { 

 public Test() { 

 int width = 100; 
 int height = 100; 
  
 ImageIcon image = new ImageIcon("no.gif"); 
 image.setImage(image.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT)); 
 JLabel label = new JLabel(image); 
  
 getContentPane().add(label); 
  
 setBounds(100, 100, 200, 200); 
 setDefaultCloseOperation(EXIT_ON_CLOSE); 
 setVisible(true); 
 } 

 public static void main(String[] args) { 
 new Test(); 
 } 
}

你可能感兴趣的:(java)