我想通过单击该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
}
答案 0 :(得分:1)
在个人情况下,我更倾向于使用JButton
来做这种事情,尽管JLabel
也是可行的。 JButton
优于JLabel
的主要优点是“被点击”,因此您可以直接利用addActionListener
方法。
现在你需要做的不仅仅是在按钮上设置图标颜色,但这是一个开始。
我还稍微重构了你的代码,因为它有一些错误:
JFrame
。使用JFrame
而不是扩展JFrame
实际上是一个好主意(如果不向其添加任何特定行为,则无需扩展JFrame)pack()
和setVisible(true)
应该是您最后拨打的电话之一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();
}
});
}
}