我正在创建一个gui,顶部有一个图像,下面有一个按钮。 每次单击按钮时,图像都会发生变化 能够改变图像3次,然后从第一次开始 我遇到了一个问题,希望有人能回答 这是我的代码
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import java.io.*;
public class GUIExample extends Frame implements WindowListener,ActionListener {
Button b;
JLabel Picture;
ImageIcon pic,dup;
private int numClicks = 0;
int k = 0;
Image[] img = new Image[3];
public static void main(String[] args) throws IOException {
GUIExample myWindow = new GUIExample("GUIExample");
myWindow.setSize(500,500);
myWindow.setVisible(true);
}
public GUIExample(String title) throws IOException {
super(title);
img[0] = ImageIO.read(new File("Earth1.jpg"));
img[1] = ImageIO.read(new File("Earth2.jpg"));
img[2] = ImageIO.read(new File("Earth3.jpg"));
setLayout(new FlowLayout());
addWindowListener(this);
b = new Button("Change Image");
Image i = img[0].getScaledInstance(300, 300, java.awt.Image.SCALE_SMOOTH);
pic = new ImageIcon(i);
Picture = new JLabel(pic);
add(Picture);
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
numClicks++;
System.out.println("a");
System.out.println(numClicks);
paint1();
}
public void paint1()
{
System.out.println("b");
switch(numClicks){
case 1 : k = 1;
break;
case 2 : k = 2;
break;
case 3 : k = 0;numClicks = 0;
break;
}
System.out.println(k);
Image t = img[k].getScaledInstance(300, 300, java.awt.Image.SCALE_SMOOTH);
pic.setImage(t);
Picture.setIcon(pic);
}
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}
这是错误即将到来
Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at gui.GUIExample.<init>(GUIExample.java:28)
at gui.GUIExample.main(GUIExample.java:21)
有没有一种更简单的方法可以在不使用IO的情况下将照片加载到此中,这对我来说是一个新概念。 关于如何正确运行的任何其他建议将不胜感激。
由于
答案 0 :(得分:1)
有两个GUI框架,
最好选择较新的挥杆。
开始:
public class SlideShow extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new SlideShow().setVisible(true);
}});
}
<强> 字段: 强>
private Image[] imgs;
private int currentImgIndex;
private JLabel imageComponent = new JLabel();
private Action nextAction = new AbstractAction() {
{
putValue(NAME, "Next");
}
@Override
public void actionPerformed(ActionEvent arg0) {
currentImgIndex = (currentImgIndex + 1) % imgs.length;
imageComponent.setIcon(new ImageIcon(imgs[currentImgIndex]));
imageComponent.repaint(50L);
}
};
nextAction
是一种解耦的行动&#34;您可以重复使用,放在一个按钮上,同时放在菜单栏的菜单中。
构造函数构建所有:
public SlideShow() {
String dir = "C:/.../media";
try {
imgs = new Image[3];
imgs[0] = ImageIO.read(new File(dir + "/image2.png"));
imgs[1] = ImageIO.read(new File(dir + "/image5.png"));
imgs[2] = ImageIO.read(new File(dir + "/image12.png"));
} catch (IOException e) {
e.printStackTrace();
}
setTitle("Slide Show");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(0, 0, 800, 600);
add(imageComponent, BorderLayout.CENTER);
JButton nextButton = new JButton(nextAction);
nextButton.setPreferredSize(new Dimension(100, 30));
add(nextButton, BorderLayout.SOUTH);
imageComponent.setIcon(new ImageIcon(imgs[currentImgIndex]));
}
默认情况下,add(...)
引用的内容窗格位于边框布局中,与您一样。
那就是它。
AbstractAction中的{ ... }
构造是一个初始化块,就像构造函数一样。