gpt4 book ai didi

Java Swing : scale image read from url

转载 作者:行者123 更新时间:2023-12-02 05:04:00 26 4
gpt4 key购买 nike

我有一个应该显示图像(从网络检索)的 JFrame 和两个用于显示下一个和上一个图像的横向按钮。原始图像尺寸太大,我会在显示之前对其进行缩放。这是我的代码:

public class ShowPicture extends JFrame implements ActionListener {

private String link;
private JButton next;
private JButton prev;
private Image image;

public ShowPicture(String link) {
this.setSize(300, 200);
setLocationRelativeTo(null); //center
setVisible(true);

this.link = link;
this.setLayout(new FlowLayout());

prev = new JButton("prev");
next = new JButton("next");

URL url;
try {
url = new URL(link + "/front.jpg");
image = ImageIO.read(url);
} catch (MalformedURLException ex) {
Logger.getLogger(ShowPicture.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ShowPicture.class.getName()).log(Level.SEVERE, null, ex);
}

JLabel image_label = new JLabel(new ImageIcon(image));
image_label.setSize(100, 100);

this.add(prev);
this.add(image_label);
this.add(next);

pack();
}

但图像未缩放

我该怎么办?

最佳答案

我会缩放图像本身而不是JLabel。使用:

image = ImageIO.read(url);
image = image.getScaledInstance(100,100,Image.SCALE_DEFAULT);
JLabel image_label = new JLabel(new ImageIcon(image));

关于Java Swing : scale image read from url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27966190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com