Java 在PIC之间切换(JFrame、JButton)

Java 在PIC之间切换(JFrame、JButton),java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,我正在尝试创建一个窗口,当按下“更改”按钮时,可以在图片之间切换。当我试图运行程序时,Java徽标会弹出,就像程序即将启动一样,但随后它就消失了。我现在有点不知所措,我希望有人能给我一个提示,告诉我可能出了什么问题 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.URL; import javax.imageio.*; import javax.swing.*; p

我正在尝试创建一个窗口,当按下“更改”按钮时,可以在图片之间切换。当我试图运行程序时,Java徽标会弹出,就像程序即将启动一样,但随后它就消失了。我现在有点不知所措,我希望有人能给我一个提示,告诉我可能出了什么问题

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.imageio.*;
import javax.swing.*;

public class ImageViewer extends JFrame{

private JPanel panel;
private JLabel imageLabel;
private JButton button;
private Icon[] icons = {};
private static final long serialVersionUID = 1L;

public ImageViewer() {

    try {
        panel = new JPanel();
        URL url1 = new URL("http://www.sm.luth.se/csee/courses/d0010e/l/prob/10tj5Ei9o/LTU-Teatern.jpg");
        URL url2 = new URL("http://www.sm.luth.se/csee/courses/d0010e/l/prob/10tj5Ei9o/LTU-Vetenskapens-hus.jpg");
        Icon image = new ImageIcon(ImageIO.read(url1));
        Icon image2 = new ImageIcon(ImageIO.read(url2));
        icons[0] = image;
        icons[1] = image2;
        imageLabel = new JLabel();
        panel.add(button);
        panel.add(imageLabel);
        button = new JButton("Change");
        button.addActionListener(new ActionListener() {     
            private boolean value = false;
            {
            }

            public void actionPerformed(ActionEvent arg0) {
                value = value == true ? false : true;
                if (value == false) {
                    imageLabel.setIcon(icons[0]);
                }else { 
                    imageLabel.setIcon(icons[1]);
                }
            }
        });
        this.setContentPane(panel);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
    }catch (Exception e) {
    }
}

public static void main(String args[]) {
    new ImageViewer();
}
}

首先,这里是我创建的内容,为您提供一些提示

这是代码。提示将在代码之后出现

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组件。只生成整个类所需的类变量。我的习惯是使所有的类变量以及类方法都私有。只公开需要公开的方法

  • 一旦我做了所有这些事情,写《行动聆听者》就变得微不足道了。

    “我现在有点困了,我希望有人能给我一个提示,告诉我可能有什么问题。”1)你忘了问问题。2) 您错过了两个重要的标记(Java和Swing),因此很少有人看到过这一点。3)
    }捕获(异常e){}
    不要忽略异常!他们准确地告诉我们出了什么问题。除非实现日志记录,否则至少调用
    Throwable.printStackTrace()
    4)BTW-
    value=!价值观
    value=value==true更短更清晰?假:真