一些上下文 - 我正在创建Scrabble的基本实现,GUI依赖于Java Swing和AWT。下面的代码摘录包含Cell类的构造函数(Scrabble板上的单个空间)。我处于概念验证阶段,正在测试添加和删除单个单元格的硬编码字母图标。每个单元格都是一个带有JLabel的单个JPanel(其中包含一个字母的ImageIcon)。代码看起来好像没有错误,但每5-6次添加/删除(通过鼠标单击)会导致类强制转换异常。具体例外是:
线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:无法将单元格强制转换为javax.swing.JLabel
我无法看到导致此异常的位置,更具体地说,为什么它只会在多次成功添加和删除后才会出现。任何见解都非常感激;我是Java GUI的初学者。
public class Cell extends JPanel {
/*Tile Colors*/
public static Color twColor = new Color(255, 0, 0);
public static Color dwColor = new Color(255, 153, 255);
public static Color tlColor = new Color(0, 51, 255);
public static Color dlColor = new Color(102, 204, 255);
public static Color defaultColor = new Color(255, 255, 255);
private JLabel selected = null;
private JLabel clicked = null;
private JLabel letterIcon;
private ImageIcon defaultIcon;
private ImageIcon testImg;
public Cell(int xPos, int yPos, int premiumStatus) {
defaultIcon = new ImageIcon ("img/transparent.png");
testImg = new ImageIcon ("img/test.jpg"); // Letter image hard-coded for testing
letterIcon = new JLabel("", defaultIcon, JLabel.CENTER);
add(letterIcon);
letterIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JLabel clicked = (JLabel) getComponentAt(e.getPoint());
System.out.println(clicked);
if (clicked == null) {
return;
}
if (selected != null) {
selected.removeAll();
selected.revalidate();
selected.setIcon(defaultIcon);
selected.repaint();
System.out.println("Test");
selected = null;
return;
}
if (selected == null) {
selected = clicked;
selected.setIcon(testImg);
selected.revalidate();
selected.repaint();
}
}
});
}
答案 0 :(得分:2)
当鼠标坐标已经转换为getComponentAt(e.getPoint());
的坐标空间时,通过调用Cell
上的letterIcon
来解决问题。
单击某个组件时,MouseEvent
的点将自动转换为侦听器注册到的组件的坐标空间。
在您的情况下,那是letterIcon
。这意味着0x0处的一个点是letterIcon
的左上角(尽管它可能位于物理位置)。
因此,调用getComponentAt(e.getPoint())
会要求Cell
返回与实际上仅与letterIcon
相关的位置对应的组件,这将(在大多数情况下)返回Cell
本身。
相反,您应该只是使用MouseEvent#getComponent
来返回触发事件的组件,即letterIcon
使用简单示例进行更新
这是一个将JLabel
设置为鼠标目标的简单示例。单击鼠标时,标签及其父容器都将根据鼠标单击的坐标绘制一个小点。
还有一个额外的好处,即父容器还会将点击点转换为它的坐标空间并绘制第二个点,该点应与标签位于同一点。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestMouseClicked {
public static void main(String[] args) {
new TestMouseClicked();
}
public TestMouseClicked() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel clickMe;
private Point clickPoint;
public TestPane() {
setLayout(new GridBagLayout());
clickMe = new JLabel("Click me") {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.MAGENTA);
// paintPoint(g, clickPoint);
}
};
add(clickMe);
clickMe.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
clickPoint = e.getPoint();
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
paintPoint(g, clickPoint);
if (clickPoint != null) {
g.setColor(Color.BLUE);
// Convert the point from clickMe coordinate space to local coordinate space
paintPoint(g, SwingUtilities.convertPoint(clickMe, clickPoint, this));
}
}
protected void paintPoint(Graphics g, Point clickPoint) {
if (clickPoint != null) {
int size = 4;
g.fillOval(clickPoint.x - size, clickPoint.y - size, size * 2, size * 2);
}
}
}
}