简体   繁体   中英

Java class type mismatch in scala?

I am trying to import the Color class from javafx , and use it in a scala class. But when I use my class somewhere else, I get this error:

Error:(9, 50) type mismatch;
 found   : javafx.scene.paint.Color
 required: drawingengine.Color

Here is the code:

package drawingengine
import javafx.scene.paint.Color
sealed class Pixel(x: Int, y: Int, color: Color);

I can fix it myself by changing line 3 to:

sealed class Pixel(x: Int, y: Int, color: javafx.scene.paint.Color);

But I think this is kinda ugly, so is't there a better way to use an imported class?


Also, i get this warning

imported `Color' is permanently hidden by definition of object Color in package drawingengine
import javafx.scene.paint.Color

and IntelliJ greys out the importing line as if it is not used.

I can see that others have had the same problem, but I don't see how the shown example applies to what i am doing. So what can I do about this warning?

This means you have defined your own Color class, inside the same package drawingengine , and that it is colliding with the JavaFX one.
This

javafx.scene.paint.Color

fixes it because you explicitly use a qualified name.
If you want to maintain both of them, you might alias the JavaFX one

import javafx.scene.paint.{Color => FxColor}

And use it like

sealed class Pixel(x: Int, y: Int, color: FxColor);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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