Many beginners always think that I still know nothing about Java. Now I have learned the GUI and I have no idea at all!
So why did I learn GUI from the beginning ???
1. Easy to use GUI.
2. He can actually make things out.
3. He can give you some knowledge of some basic Java content, such as calling methods of classes and Inheritance of classes.
4. Be interested in Java. The most important thing is to know what Java can do.
I personally follow this principle. No matter what I want to learn in the future, the learning method is as follows:
I. Which classes do I need to know.
2. Master several basic methods in the class.
Well, I don't need to talk much about it. Now I want to talk about how to learn GUI quickly.
I. Gui categories are divided into three categories
A. Container class.
B. component class.
C. helper class.
As we only need to have a basic understanding of the GUI, so many things do not need to be studied in depth, so the above three classes correspond to jframe, flowlayout, and jbutton respectively. As long as you learn these three types of guis, you can end.
Public static void main (string [] ARGs)
{
Jframe frame = new jframe (); // instantiate a frame.
Frame. setvisible (true); // make the window visible
Frame. settitle ("first Java program"); // Add a title for the window
Frame. setbounds (300,100,400,400); // you can specify the position and size of the window.
Frame. setdefaclocloseoperation (jframe. exit_on_close); // close the window and end the program
Jbutton btn1 = new jbutton ();
BTN. settext ("1 ");
Frame. Add (btn1); // I don't know why the button cannot be displayed in eclipse, but it can be displayed in full screen mode.
Jbutton btn2 = new jbutton ();
BTN. settext ("2 ");
Frame. Add (btn2 );
// Frame. getcontentpane (). Add (BTN); Do not know why getcontentpane is required. If you are interested, you can discuss it.
}
The knowledge we can learn from the above program is:
1. instantiate a class.
2. Call the methods in the class.
3. The stage is as big as what we think. As long as you dare to think, other people provide methods for me to call.
4. Feel the good classes written by others, so that you can write good classes by yourself in the future.
The class written by sum is actually a good description of the window. For us, we only need to perform some operations on the window. However, the method of the class must be known when performing operations on the window, we can search by checking JDK and Google.
If you run this program, you will find the second button always appears, because frame has its own default Layout mode. As long as you add components, I will overwrite the entire framework, in this case, the second button overwrites the first button. However, we don't have to worry about jframe's mature class. We firmly believe that there must be a way for us to implement our own layout.
So add the following in the above program:
Flowlayout flow = new flowlayout ();
Frame. setlayout (flow );
With this example, we can know that we can achieve it as long as we can think about it.
Finally, it also explains why the button must be added to the container for display:
Frame. Add (btn2); it is like placing the dish directly on the table.
Frame. getcontentpane (). add (BTN); according to this understanding, the component must be placed in the container class for display. The table is a container, but it can store other containers, while other containers can store components. Therefore, the component is usually placed in the content panel container, and then the content panel is pasted into the frame window.