Take you into graphical user interface (2)

zhaozj2021-02-17  53

Thinking about the previous section Let's take a look at the code:

Import javax.swing. *;

Class myframe extends jframe {public myframe () {settitle window "); setbounds (200, 100, 300, 100); setsizable (false); setFaultCloseOperation (jframe.exit_on_close);}}

Class mywin {public static void main (string [] args) {myframe mywin = new myframe (); Mywin.setVisible (TRUE);}}

First, we have born out of the JFrame class, and only one new method in this window class is that this class is constructed, in which we call some inheritance methods. Let me introduce the functions and methods of use of each method (in fact, these methods can be found in the JDK's API Spectation document. This document can be downloaded on the official website of Sun. Here I only start here. Throwing bricks, you have to write better and more beautiful windows you can learn more about Sun's document): Settitle () The role is to set the window title (that is, the text in the window title), it receives a string, then Display it on the title bar of the window.

SetBounds () The role is to set the location and size of the window. Taken 4 int variables as parameters, there are two parameters of the method: the first specified for setbounds (int X, int y, int width, int Height ). In order to understand the use of these parameters, first, I will introduce you to the use of the coordinate system. The coordinate system in Java is the original point in the upper left corner of the screen, and then the direction of the X, Y axis is just the coordinate system that is usually seen. Conversely (as shown below). As for Width and Height as the name, the width and height are.

The second is setbounds (Rectangle R). Rectangle is a rectangular class in the AWT library, so you must use Rectangle must import java.awt. *. As for the specific use of the Rectangle class, it will not be said here. If you are not enough to understand the Rectangle class, please look at Java DOCS. In addition to using the setBounds () method, you can also use SetLocation (int, int, int), or setlocation (POINT) to set the relative position in the upper left corner of the window, and then set the window size using setsize (int, int, int), or setsize (dimension). Also here is not discussions to Point and Dimension, if you feel that it is necessary to discuss with me).

The role of setResizable () is whether the window can change the size of the size. It requires a Boolean variable as a parameter. TRUE represents a window to change the size of the form; false does not change the size, and the maximum button on the title bar does not work.

SetDefaultCloseOperation () method determines what happens when the window is turned off. It can use 4 parameters, which are constants of Static Final defined by the JFrame class, which are exit_on_close, dispose_on_close, do_nothing_close, hide_on_close. The Dispose_on_close parameter makes it included in the frames and any components, but does not close the application. Donothing_on_clse parameters invalidate the shutdown of the frame window. Hide_on_close parameters just hide the window using setvisible (false), it is the default value. The remaining part is similar to the content of the previous section, but everyone also noted that the way the window visible will use two methods here. We use setvisible (), this method is different from show () different it needs a boolean variable. When using True, you can see the window. These two ways about which one is better, I have seen a few books, love to use show (), some love to use setvisible (), suggesting setvisible (), SHOW can be used for the window when using other components Make the window visible and bring the window to the forefront. As for the concept of components, we will specifically speak in the next section, and will explain that Java's basic principles and their partial components in the window showing other components and corresponding inheritance relationships in the library. Thank you for your support, I hope you continue to support me, suggestions and discussions, please write to kwaif@sina.com

转载请注明原文地址:https://www.9cbs.com/read-29116.html

New Post(0)