How do I change an image using ActionListener?

84 Views Asked by At

I'm making a game and I want to change an image already being shown. I think the problem is the wording in the actionPerformed class.

Here's my code:

TwoButtons()
{

    setLayout (new FlowLayout());

    image = new ImageIcon ("as.jpg");
    b = new JButton (image);
    b.setActionCommand("a");
    b.addActionListener(this);
    add(b);
}

public void actionPerformed (ActionEvent evt)
{
    if (evt.getActionCommand().equals("a"))
    {
        image = (new ImageIcon("qd.jpg"));
    }
    repaint();
}
0

There are 0 best solutions below