简体   繁体   English

Java:无法转换为自定义类

[英]Java: Cannot Cast to Custom Class

I am creating a Tic-Tac-Toe game in which a square in the grid is represented by a class called Square which I created. 我正在创建一个井字游戏,其中网格由我创建的名为Square的类表示。 It extends JButton, but I added a boolean field called pressable which states whether or not the button can be pressed in-game. 它扩展了JButton,但是我添加了一个名为pressable的布尔字段,该字段说明是否可以在游戏中按下按钮。 If the button already has a letter, then it cannot be pressed. 如果按钮已经有一个字母,则无法按下它。 I want to implement code in my actionPerformed() method that will check the pressable variable, and if pressable is false, it will not change the letter of the square. 我想在我的actionPerformed()方法中实现代码,该代码将检查pressable变量,如果pressable为false,它将不会更改正方形的字母。 I am using the getSource() method on my ActionEvent to give me a reference to the square pressed. 我在ActionEvent上使用getSource()方法为我提供了对所按下的正方形的引用。 Therefore, I need to cast the Object to type Square so that I can access its pressable field. 因此,我需要将Object 强制转换为Square类型,以便可以访问其可按下字段。 But when I do this, I get the following error: java.lang.ClassCastException: javax.swing.JButton cannot be cast to tic.tac.toe.Square This makes no sense to me. 但是,当我这样做时,出现以下错误: java.lang.ClassCastException: javax.swing.JButton cannot be cast to tic.tac.toe.Square这对我来说毫无意义。 Before I needed each square to have a pressable field, I used a JButton, which worked just fine. 在我需要每个正方形都有一个可按下字段之前,我使用了JButton,它工作得很好。 But since Square inherits JButton, I would think that it would work as well. 但是由于Square继承了JButton,所以我认为它也会起作用。 How can I fix this? 我怎样才能解决这个问题?

The JVM is telling you the truth: the object returned by getSource() is in fact a JButton and is not a Square object, despite your statements above. JVM是告诉你真相:被返回的对象getSource()实际上一个JButton,并没有一个方形物体,尽管你上述声明。 So the fact is, you are adding your ActionListener to a JButton and not to a Square object. 事实是,您要将ActionListener添加到JButton而不是 Square对象。 You will need to inspect your code to see why this is so. 您将需要检查您的代码,看看为什么会这样。 If you need more of our help, you'll need to show this code. 如果您需要我们的更多帮助,则需要显示此代码。


Edit 编辑
You state: 您声明:

I looked into it and realized that my "Start" button was going through the same process, which is where the error came from. 我调查了一下,发现我的“开始”按钮正在经历相同的过程,这就是错误的出处。

Thank you for the update. 谢谢你的更新。

This suggests that you're using one ActionListener for all buttons, which you'll not want to do. 这表明您对所有按钮都使用了一个ActionListener,而您不想这样做。 Certainly all the tic-tac-toe buttons can use the same listener, but any others should use there own separate listener object, perhaps via an anonymous inner class. 当然,所有的井字游戏按钮都可以使用相同的侦听器,但是其他任何按钮都应该使用自己的单独的侦听器对象,也许可以通过匿名内部类使用。

You could disable the JButton the first time it is clicked with .setEnabled(false) which would get around this issue. 您可以在第一次使用.setEnabled(false)单击JButton时禁用它,从而可以解决此问题。 But I agree with HovercraftFullOfEels. 但是我同意HovercraftFullOfEels。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM