質問

I'm learning the different ways to position objects in GUI and I don't really understand why its necessary to cast to LayoutManager in order to use the setLayout method. would it be necessary to implement the LayoutManager interface for this to work correctly ?

public class FlowL extends JApplet{

   public void init(){

         // why is the cast necessary ?  
        // an error occurs when  its   setLayout(new FlowLayout());    
        setLayout((LayoutManager) new FlowLayout());


 }

}
役に立ちましたか?

解決

"and even though the error disapears i get an error.. java.lang.ClassCastException: flowLayout.FlowLayout cannot be cast to java.awt.LayoutManager"

There is no standard Java class flowLayout.FlowLayout. That's your class (or possibly some third party class that doesn't extends LayoutManager, which I highly doubt).

You need to import

import java.awt.FlowLayout;

And get rid of your

import flowLayout.FlowLayout; // if you have that.

  • Lesson to be learned: Don't name your classes the same as existing Java classes.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top