java程序中如何设置图片大小?
展开全部
用Image中的getScaledInstance方法得到一个按照指定宽度和高度缩放以后的Image实例,然后再用setImage方法设置ImageIcon所显示的图像
一下示例:
import javax.swing.*;
import java.awt.*;
public class test extends JFrame
{
private ImageIcon img;
private JLabel showImg;
private final static int WIDTH=147;
private final static int HEIGHT=136;
public test()
{
img=new ImageIcon("1.png");
img.setImage(img.getImage().getScaledInstance(test.WIDTH,test.HEIGHT,Image.SCALE_DEFAULT));
showImg=new JLabel();
showImg.setIcon(img);
this.add(showImg,BorderLayout.CENTER);
this.setBounds(300,200,400,300);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String args[])
{
new test();
}
}
一下示例:
import javax.swing.*;
import java.awt.*;
public class test extends JFrame
{
private ImageIcon img;
private JLabel showImg;
private final static int WIDTH=147;
private final static int HEIGHT=136;
public test()
{
img=new ImageIcon("1.png");
img.setImage(img.getImage().getScaledInstance(test.WIDTH,test.HEIGHT,Image.SCALE_DEFAULT));
showImg=new JLabel();
showImg.setIcon(img);
this.add(showImg,BorderLayout.CENTER);
this.setBounds(300,200,400,300);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String args[])
{
new test();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询