Simple Swing widget
JLabel components
the most basic component in the Swing library is JLabel. It did exactly what you expected: stay there, look beautiful, describe the other components. The JLabel that are shown are actually applied:
JLabel
Not very appealing, but still useful. In fact, throughout the application, not only the JLabel is described in the composition, but it is also used as a picture description. Whenever you see a picture in a Swing application, it can be JLabel. JLabel does not have many unexpected methods for Swing beginners. The basic methods include setting up text, pictures, alignment, and other components of the label description:
· Get/settext (): Gets / sets the text of the label.
· Get/seticon (): Gets / sets the picture of the label.
· Get/sethorizontalalignment (): Gets / sets the horizontal position of the text.
· Get/setverticalalignment (): Gets / sets the vertical position of the text.
· Get/setdisplayedmnemonic (): Gets / sets the access key (underline text) of the label.
· Get/setlablefor (): Gets / sets the component attached to this label, so when the user presses ALT + access key, the focus shifts to the specified component.
JButton components
The basic action component in Swing , JButton, is the same button as the OK and Cancel that you can see in each window, and These buttons do exactly what you want them to do - After you click them, something happens. What the hell is going to happen? You must define what happens (see the event for more information).
The method used to change the JButton property is similar to the JLabel Method (you may find that these properties are similar in most Swing components). They control text, pictures, and orientations:
· Get/settext (): Gets / sets the text of the label.
· Get/seticon (): Gets / sets the picture of the label.
· Get/sethorizontalalignment (): Gets / sets the horizontal position of the text.
· Get/setverticalalignment (): Gets / sets the vertical position of the text.
· Get/setdisplayedmnemonic (): Gets / sets the access key (underscore character), which causes the button to click When combined with the Alt button.
In addition to these methods, I would also like to introduceJButtonanother set of methods that are included. These methods take advantage of all the different states of the buttons. A state is a property that describes a component, usually with a true / false setting. In JButton , you can include the following possible statuses: Active /Inactive , checked /unchecked , mouse over / mouse off, pressed / not pressed, and so on. In addition, these states can be combined, such as buttons that can be selected while the mouse is over. Now you may ask yourself what you are going to do with these states. As an example, take a look at the back button on your browser. Notice how the picture changes when the mouse passes through it, and how the picture changes when the button is pressed. This button takes advantage of a different state. Each state takes a different picture, which is a common and effective way to prompt user interaction. The state method on the JButton is:
· Get/setdisabledicon ()
· Get/setdisableselectedicon ()
· Get/seticon ()
· Get/setpressedicon ()
· Get/setrollovericon ()
· Get/setrolloverselectedicon ()
· Get/setselectedicon ()
JTextField components
The basic text component in Swing is JTextField, which allows the user to enter text in the UI. I'm sure you're familiar with text fields: to master this tutorial, you must enter a user name and password using a text field. You enter text, delete text, select text, and move the text around--swing you are responsible for all of this work. As a UI developer, when you take advantage of Jjtextfield, you don't actually need to do anything.
In any case, this is what JTextField looks like when it's actually used:
JTextField
When dealing with JTextField, you only need to focus on one method-it should be obvious, this method is to set the text method: Get/settext (), used to get/set the text in the JTextField.
JFrame components
JFrame
So far, I've introduced the three basic building blocks of Swing: Tags, buttons, and text fields, but now you need a place to put them, and you want users to know how to handle them. The JFrame class is doing this- It's a container that allows you to add other components to it, organize them, and present them to the user.
jframe Actually, it's not just that you put the component in it and present it to the user. Compared to its superficial simplicity, it is actually swing swing jframe jframe in the native operating system is registered as a window, after doing so, You can get many familiar operating system Windows features: Minimize / Maximize, change size, move. But for the purpose of this tutorial, put jframe jframe Some of the methods of modifying properties that are called are:
· Get/settitle (): Gets / sets the caption of the frame.
· Get/setstate (): Gets / sets the minimized, maximized, etc. state of the frame.
· Is/setvisible (): Gets / sets the visual state of the frame, in other words, whether it is displayed on the screen.
· Get/setlocation (): Gets / sets the position that the frame should appear on the screen.
· Get/setsize (): Gets / sets the size of the frame.
· Add (): Adds the component to the frame.
2016/05/02 Swing