在JPanel中缩小图像大小

时间:2016-06-02 15:41:48

标签: java image swing imageicon

我尝试在

下创建GUI作为图像

enter image description here

但是,当我运行文件时,图像的大小仍保持原始大小

enter image description here

如何减小图像的大小,以便在旁边添加文字?

public class HomePage extends JFrame {
  public static void main(String[] args) throws IOException {
    new HomePage();
  }

  public HomePage() throws IOException {
    this.setTitle("Picture Application");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.yellow);
    BufferedImage myPicture = ImageIO.read(new File("C:\\Users\\seng\\Desktop\\logo.png"));
    myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
    JLabel picLabel = new JLabel(new ImageIcon(myPicture));
    add(picLabel);
    this.pack();
    this.setVisible(true);
  }
}

1 个答案:

答案 0 :(得分:2)

myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);

上面的语句返回一个你从不引用的新BufferedImage。

代码应为:

//myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);    
Image scaled = myPicture.getScaledInstance(10,30,Image.SCALE_SMOOTH);
JLabel picLabel = new JLabel(new ImageIcon(scaled));