java图片间切换,java - 在图片之间切换(JFrame,JButton) - SO中文参考 - www.soinside.com...

首先,这是我为了给您一些提示而创建的。

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9WMnFLNC5wbmcifQ==

eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9CYnFMSy5wbmcifQ==

这是代码。提示将在代码之后。import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import javax.imageio.ImageIO;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.SwingUtilities;

public class ImageViewer implements Runnable {

private boolean isImage1;

private BufferedImage image;

private ImageData imageData;

private JLabel label;

public static void main(String args[]) {

SwingUtilities.invokeLater(new ImageViewer());

}

public ImageViewer() {

this.imageData = new ImageData();

this.image = imageData.getImage1();

this.isImage1 = true;

}

@Override

public void run() {

JFrame frame = new JFrame("Image Viewer");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

label = new JLabel(new ImageIcon(image));

panel.add(label);

JButton button = new JButton("Change");

button.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

if (isImage1) {

image = imageData.getImage2();

} else {

image = imageData.getImage1();

}

label.setIcon(new ImageIcon(image));

isImage1 = !isImage1;

}

});

panel.add(button);

frame.add(panel);

frame.pack();

frame.setVisible(true);

}

public class ImageData {

private BufferedImage image1;

private BufferedImage image2;

public ImageData() {

URL url1;

URL url2;

try {

url1 = new URL("http://www.sm.luth.se/csee/courses/d0010e/l/prob/10tj5Ei9o/LTU-Teatern.jpg");

url2 = new URL("http://www.sm.luth.se/csee/courses/d0010e/l/prob/10tj5Ei9o/LTU-Vetenskapens-hus.jpg");

this.image1 = readImage(url1);

this.image2 = readImage(url2);

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

private BufferedImage readImage(URL url) {

try {

return ImageIO.read(url);

} catch (IOException e) {

e.printStackTrace();

}

return null;

}

public BufferedImage getImage1() {

return image1;

}

public BufferedImage getImage2() {

return image2;

}

}

}

所以这里是提示。除非您打算覆盖一个或多个类方法,否则请不要扩展JFrame或任何Swing组件。

始终在事件调度线程(EDT)上启动Swing项目。为了方便起见,我在主ImageViewer类中实现了Runnable。您的main方法应始终包含对SwingUtilities invokeLater的调用。

我将图像的读取移至其自己的数据类。始终将数据与视图分开。我通常使用模型/视图/控制器架构来创建Swing项目。

我检查了URL中的错误或实际图像读取。如果发生错误,它将打印出堆栈跟踪,这将有助于我发现错误。切勿将整个方法放在try-catch块中。

唯一需要作为类变量的Swing组件是JLabel组件。仅使整个类都需要的类变量。我的习惯是将所有类变量以及类方法设为私有。仅公开需要公开的方法。

一旦我做了所有这些事情,编写动作监听器就变得微不足道了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值