如何通过在java中单击将图像更改为另一个图像

时间:2014-09-14 05:51:40

标签: java image swing awt imageicon

我想通过单击该imageicon将此代码中imageicon使用的图标更改为另一个图标。有人能帮帮我吗?我能在这段代码中做些什么?如果底部充满白色,我希望它也会掉下来。如果一个用户点击白点,颜色变为黄色,然后,如果再次点击另一个白色,颜色变为红色。

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


class Connect4Games extends JFrame implements ActionListener, MouseListener 
{
JFrame frame;
JPanel pane;
JLabel insertaxis[][];
ImageIcon EmptySpace, circleYellow, circleRed ;
BufferedImage bufferedImage;
public Connect4Games() {
    LookAndFeel.setLookAndFeel();
    pane = new JPanel();
    frame = new JFrame();
    insertaxis = new JLabel[6][7];
    EmptySpace = new ImageIcon("image/Circle.png");
    circleYellow = new ImageIcon("image/Circle2.png");
    circleRed = new ImageIcon("image/Circle3.png");
    pane.setLayout(new GridLayout(6, 7));
    pane.setBackground(Color.blue);
    add(pane);
    addMouseListener(this);
    setTitle("Connect 4");
    setVisible(true);
    setSize(670, 590);
    frame.pack();
    setLocationRelativeTo(null);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    for (int i = 0; i < 6; i++) {
        for (int j = 0; j < 7; j++) {
            insertaxis[i][j] = new JLabel();
            pane.add(insertaxis[i][j]);
            insertaxis[i][j].setIcon(EmptySpace);
        }
    }
}

@Override
public void actionPerformed(ActionEvent e) {
}

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

@Override
public void mouseClicked(MouseEvent e) {
    int x = e.getX();
    int y = e.getY();

    if(e.getPoint() == null){
        insertaxis[x][y].setIcon(circleRed);
    }
}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

1 个答案:

答案 0 :(得分:1)

在个人情况下,我更倾向于使用JButton来做这种事情,尽管JLabel也是可行的。 JButton优于JLabel的主要优点是“被点击”,因此您可以直接利用addActionListener方法。

现在你需要做的不仅仅是在按钮上设置图标颜色,但这是一个开始。

我还稍微重构了你的代码,因为它有一些错误:

  • 您创建了一个实际上从未使用过的JFrame。使用JFrame而不是扩展JFrame实际上是一个好主意(如果不向其添加任何特定行为,则无需扩展JFrame)
  • 拨打pack()setVisible(true)应该是您最后拨打的电话之一
  • 应该在EDT(事件调度线程)上完成对Swing UI的所有更改,并且应该在invokeLater块中启动UI,确保它在EDT上运行。

这是一个小型实现,展示了如何使用JButton轻松实现此目的。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Connect4Games {
    JFrame frame;
    JPanel pane;
    JButton insertaxis[][];
    ImageIcon emptySpace, circleYellow, circleRed;
    BufferedImage bufferedImage;

    private boolean red = true;

    public Connect4Games() {
        frame = new JFrame();
        pane = new JPanel();
        insertaxis = new JButton[6][7];
        emptySpace = new ImageIcon(getCircle(Color.GRAY));
        circleYellow = new ImageIcon(getCircle(Color.YELLOW));
        circleRed = new ImageIcon(getCircle(Color.RED));
        pane.setLayout(new GridLayout(6, 7));
        pane.setBackground(Color.blue);
        frame.add(pane);
        frame.setTitle("Connect 4");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 7; j++) {
                final JButton button = new JButton();
                button.setBorderPainted(false);
                button.setContentAreaFilled(false);
                button.setFocusPainted(false);
                button.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (button.getIcon() == emptySpace) {
                            if (red) {
                                button.setIcon(circleRed);
                            } else {
                                button.setIcon(circleYellow);
                            }
                            red = !red;
                        } else {
                            JOptionPane.showMessageDialog(button, "Sorry, you cannot change the color of this place");
                        }
                    }
                });
                insertaxis[i][j] = button;
                pane.add(insertaxis[i][j]);
                insertaxis[i][j].setIcon(emptySpace);
            }
        }
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);

        frame.setVisible(true);
    }

    private static BufferedImage getCircle(Color color) {
        BufferedImage image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics g = image.getGraphics();
        g.setColor(color);
        g.fillArc(0, 0, 32, 32, 0, 360);
        return image;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Connect4Games();
            }
        });
    }

}