gpt4 book ai didi

java - 类转换异常 - 鼠标事件

转载 作者:行者123 更新时间:2023-12-02 07:03:26 25 4
gpt4 key购买 nike

一些上下文 - 我正在创建 Scrabble 的基本实现,并且 GUI 依赖于 Java Swing 和 AWT。下面的代码摘录包含 Cell 类(拼字游戏板上的独立空间)的构造函数。我正处于概念验证阶段,正在测试向单个单元格添加和删除硬编码字母图标。每个单元格都是一个带有 JLabel(其中包含字母的 ImageIcon)的单独 JPanel。该代码看起来工作正常,没有错误,但每 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();
}
}
});
}

最佳答案

当鼠标坐标已经转换为坐标空间时,问题是由在 Cell 上调用 getComponentAt(e.getPoint()); 引起的letterIcon

当单击某个组件时,MouseEvent 的点会自动转换为监听器注册到的组件的坐标空间。

在你的例子中,这就是letterIcon。这意味着 0x0 处的点是 letterIcon 的左上角/左上角(尽管它可能位于物理位置)。

因此,调用 getComponentAt(e.getPoint()) 是要求 Cell 返回与实际上仅相对于 Cell 的位置相对应的组件>letterIcon,它将(在大多数情况下)返回 Cell 本身。

相反,您应该简单地使用 MouseEvent#getComponent 返回触发事件的组件,即 letterIcon

用一个简单的例子更新

这是一个将 JLabel 设置为鼠标目标的简单示例。单击鼠标时,标签及其父容器都会根据鼠标单击的坐标绘制一个小点。

还有一个额外的好处,即父容器还将单击点转换到其坐标空间并绘制第二个点,该点应该与标签在同一次单击中。

enter image description here

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);
}
}
}
}

关于java - 类转换异常 - 鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16371789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com