【问题标题】:Java swing: scale image read from urlJava swing:从url读取的缩放图像
【发布时间】:2015-03-14 00:12:57
【问题描述】:

我有一个 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();
}

但是图片没有缩放

我该怎么办?

【问题讨论】:

标签: java swing


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多