【问题标题】:How can i change an image to another image by clicking in it in java [closed]如何通过在java中单击将图像更改为另一个图像[关闭]
【发布时间】:2014-11-07 22:37:36
【问题描述】:

我想通过单击该 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

}

【问题讨论】:

  • 尝试将鼠标侦听器添加到您要捕获鼠标事件的标签,而不是框架。
  • 这个问题似乎是题外话,因为它是关于完成代码。

标签: java image swing awt imageicon


【解决方案1】:

就个人而言,我更喜欢使用JButton 来处理这种事情,尽管使用JLabel 也是可行的。 JButton 相对于JLabel 的主要优势在于它是“被点击”的,因此您可以直接利用addActionListener 的方法。

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

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

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

}

【讨论】:

  • 你能不能把那个圆圈弄得更近一点……我不知道让它们更近一些。因为我只是java中的菜鸟。请帮帮我
  • @JackRourke 在每个按钮上将边框设置为自定义值。类似:button.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));。您可以根据需要调整值(如果需要,它甚至可以为零)。
  • 非常感谢@Guillaume Polet
  • 如果有空白区域且没有色环,我还想让红色和黄色的圆圈落入底部。我对此一无所知。你能给我一些想法吗?并且如果4个相同颜色的圆圈在水平,垂直或对角线上依次排列,将显示赢得该游戏的消息。
  • @JackRourke 这是您应该实施的常见逻辑,但开始偏离主题。我不是一个会为你写代码的答录机。如果您有特定问题,请在 SO 上发布一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 1970-01-01
  • 2017-10-09
  • 1970-01-01
相关资源
最近更新 更多