Aha, returning to school is efficient ~ But I want to review it again, but I sat down in front of the computer and started to read the program again. No way, I can't ...... I am working on a chat software recently. Do I have to log on to it. I want to create a cardlayout for the login interface, but the problem arises. Now we can take it out and paste several images to record it.
Basic jframe display program:
Import java. AWT .*;
Import javax. Swing .*;
Class demo ...{
Public static void main (string [] ARGs )...{
Cardlayout card = new cardlayout ();
Jframe frame = new jframe ("cardlayout layout test ");
Frame. setdefaclocloseoperation (jframe. exit_on_close );
Jlabel label1 = new jlabel ("lable1 ");
Jlabel label2 = new jlabel ("label2 ");
Frame. setlayout (card );
Frame. Add (label1, "L1 ");
Frame. Add (label2, "L2 ");
Frame. setsize (100,100 );
Container con = frame. getcontentpane ();
Card. Show (con, "L2 ");
Frame. setvisible (true );
}
}
I don't need to paste this interface, but the skin is not very comfortable ...... Let's take a look at the container above the show of the card in the program, or the first parameter con of the show. I did not write this statement in my program, but it does not exist in creating the iner. However, if the first parameter is put in the frame, compilation will pass, but exception will be thrown:
First, yes.Wrong parent for cardlayoutIt seems that there is a problem with the cardlayout. Show (parent, string) parameter. After reading JDK, this show is such a show (container, string). That is to say, the previous parameter is container !! Let's look at the inheritance tree of a class:
See it. Father of jframe's father, that is, Grandpa's father is Java. AWT. container! That is to say, jframe is a container. Why is it impossible to place the jframe that belongs to the iner here? People can eat. Men and women belong to people and can eat. Eunuch is a child of a man's inheritance class and can eat, but eunuch without a mouth is a child of eunuch's inheritance class. Although they are all human beings, he/she/he cannot eat because he/she has no mouth. In another thought, isn't jframe having a getcontentpane () method? Why is it a container. Now that this method is used, it is impossible to return this! It seems that there is nothing to prove through an example that is not rigorous, but most of the operations on jframe in many programs call a getcontentpane () and do some operations on the container, why do I need to call this one more? I was waiting for me here. Also, swing and AWT are originally the relationships that are constantly being broken, so it's hard to get SWT back ......