在Java中显示来自按钮的各种图像

在Java中显示来自按钮的各种图像,java,image,swing,button,Java,Image,Swing,Button,我正在编写一个程序,用户可以从中选择3个不同的按钮,根据他们选择的按钮,将显示不同的图像(本例中为gif)。我将程序分为3个类:GUI窗口类、GUI类和颜色面板类。问题是我无法在单击按钮时加载图像 /******************************GUIWindow*******************************/ import javax.swing.*; public class GUIWindow { public static void main(St

我正在编写一个程序,用户可以从中选择3个不同的按钮,根据他们选择的按钮,将显示不同的图像(本例中为gif)。我将程序分为3个类:GUI窗口类、GUI类和颜色面板类。问题是我无法在单击按钮时加载图像

/******************************GUIWindow*******************************/
import javax.swing.*;

public class GUIWindow
{
    public static void main(String[] args)
    {
        GUI theGUI = new GUI();
        theGUI.setTitle("Merry Christmas!");
        theGUI.setSize(500, 600);
        theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        theGUI.setVisible(true);
    }
}

/******************************GUI*******************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI extends JFrame
{
    private ImageIcon cat1 = new ImageIcon("cat1.gif");
    private ImageIcon cat2 = new ImageIcon("cat2.gif");
    private JButton cat1Button = new JButton("1");
    private JButton cat2Button = new JButton("2");
    private JButton cat3Button = new JButton("3");

    public GUI()
    {
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(cat1Button);
        buttonPanel.add(cat2Button);
        buttonPanel.add(cat3Button);

        Container container = getContentPane();
        container.add(buttonPanel, BorderLayout.NORTH);

        cat1Button.addActionListener(new cat1Listener());
        cat2Button.addActionListener(new cat2Listener());
    }

    private class cat1Listener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            ColorPanel panel = new ColorPanel(Color.white, cat1);
            Container pane = getContentPane();
            pane.add(panel);
        }
    }

    private class cat2Listener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            ColorPanel panel = new ColorPanel(Color.white, cat2);
            Container pane = getContentPane();
            pane.add(panel);
        }
    }
}

/******************************ColorPanel*******************************/
import java.awt.*;
import javax.swing.*;

public class ColorPanel extends JPanel
{
    private ImageIcon image;

       public ColorPanel(Color backColor, ImageIcon i)
       {
          setBackground(backColor);
          image = i;
        }

       public void paintComponent(Graphics g)
       {
          super.paintComponent(g);

          int x = (getWidth() - image.getIconWidth()) / 2;
          int y = (getHeight() - image.getIconHeight()) / 2;

          image.paintIcon(this, g, x, y);
       }
}

最简单的方法是在JLabel中显示图像图标,并使用JLabel的
setIcon(…)
方法简单地交换图标。添加了Swing标记,删除了eclipse标记,因为问题与Swing有关,与eclipse无关。感谢您的帮助!程序现在可以运行了!:D还感谢您帮助我使用标签,很抱歉这是我第一次使用此网站。欢迎您将您的解决方案总结为自我回答。最简单的方法是在JLabel中显示图像图标,并使用JLabel的
设置图标(…)
方法简单地交换图标。添加了Swing标签,eclipse标签被删除,因为这个问题与Swing有关,与eclipse无关。谢谢您的帮助!程序现在可以运行了!:D还感谢您帮助我处理标签,很抱歉这是我第一次使用此网站。欢迎您将您的解决方案总结为自我回答。