【发布时间】: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();
}
但是图片没有缩放
我该怎么办?
【问题讨论】:
-
已经提出了一个类似的问题(包含您问题的答案):stackoverflow.com/questions/6714045/…