gpt4 book ai didi

java - 带有图像的 GUI 窗口

转载 作者:行者123 更新时间:2023-12-01 12:44:31 24 4
gpt4 key购买 nike

我正在创建一个 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 的情况下将我的照片加载到此中,这对我来说是一个新概念。任何有关如何使其正确运行的其他建议将不胜感激。

谢谢

最佳答案

混合了两种 GUI 框架,

  • AWT(框架、按钮)
  • Swing(JLabel)

最好选择较新的 Swing 。

开始:

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 是一个解耦的“操作”,您可以重复使用它,将其放在按钮上,同时放置在菜单栏中的菜单中。

构造函数构建所有:

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(...) 引用的内容 Pane 位于边框布局中,就像您所做的那样。

就是这样。

AbstractAction 中的 { ... } 构造是一个初始化 block ,就像构造函数一样。

关于java - 带有图像的 GUI 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24827272/

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