Finally came to the visual window made part of, from learning Java, until the beginning of learning, until now, are looking forward to one day can write an interface and is very beautiful application, today is a real start of the time node, memorable.
There are a lot of things, first of all, swing and abstract window Toolkit: Swing provides everything you need to write a program that uses a graphical user interface and is now the most widely used GUI.
Component graphical user interface, you need to use two types of objects: components and containers. A component is a separate element in the user interface, such as a button or slider, and a container is a component that holds other components.
The containers and components we need are basically all in javax.swing.* and java.awt.* packages.
1. Set the frame title.
In a class that inherits JFrame, you can call JFrame's constructor to set the caption:
Super ("Title");
You can also use the framework's methods:
Settitle ("Title");
2. Specify the size of the frame
SetSize (350,125); The number represents the height width of the frame, in pixels
3. How to close the frame
Setdefaultcloseoperation (Jframe.exit_on_close); Exit program when button is clicked
Setdefaultcloseoperation (Jframe.dispose_on_close); Close the frame and destroy the object, but the application continues to run
Setdefaultcloseoperation (Jframe.do_nothing_on_close); Let the framework open and continue running
Setdefaultcloseoperation (jframe.hide_on_close);//close frame and continue running
4. Appearance customization
You can set the appearance by calling the Setlookandfeel () method of the Uimanager class in the main swing package. The method receives a parameter: The full name of the skin class. (The calling method is to use Try-catch to handle possible errors.)
Uimanager.setlookandfeel ("Com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); Indicates that the appearance of Nimbus is called
5. Display Frame
SetVisible (TRUE);
6. Button (JButton)
JButton okbutton=new JButton ("OK"); Create a JButton object and set the label
Add (OKButton); To add a JButton component to a container
7. Layout Manager
The layout manager can be used to set the location of the component in the container, and there are currently several layout managers
FlowLayout: The simplest layout manager
FlowLayout flo=new FlowLayout ();
GridLayout: The number of rows and columns that can be specified by the component organization member
GridLayout grid=new GridLayout (2,3); Organization is 2 rows and 3 columns
BorderLayout: Placing components in a east five lines of defense
BorderLayout border=new BorderLayout ();
Add (Abutton,borderlayout.north);
Add (Bbutton,borderlayout.south);
Add (Cbutton,borderlayout.east);
Add (dbutton,borderlayout.west);
Add (Ebutton,borderlayout.center);
BoxLayout: Row or column A component
Boxlayout.x_axis Specify horizontal arrangement, Boxlayout.y_axis specify vertical arrangement
JPanel swingdemo=new JPanel ();
BoxLayout box=new boxlayout (Swingdemo,boxlayout.x_axis); Arrange the panel Swingdemo components horizontally
8. Labels and text boxes (jlable, JTextField)
Jlable is used to display information that the user cannot modify, so that the text can also be a graphic.
Jlable page1=new jlable ("Email Adress:", jlable.right); Right to justify
JTextField is used to provide the area where the user enters a single line of text, and when you create a text box, you can set its width
JTextField pageaddress=new JTextField ("US", 20); The default content is "US", with a width of 20 characters
object contains text that can be retrieved using method GetText ().
String Email=email.gettext ();
Set text
Email.settext ("21312151252[email protected]");
9. check box (Jcheckbox)
Consists of files and boxes, which can be selected.
Jcheckbox box=new jcheckbox ("Hello World"); Set the label "Hello World" for the check box
Jcheckbox can be grouped, but cannot be selected at the same time.
Test with the above code
Because jumbo size is not within buttongroup, it is not affected by the other three options.
10. Combo Box (JComboBox)
A pop-up selection list can also be set to accept text input.
11. Text Area (JTextArea)
Allows the user to enter multiple lines of text, specifying the width and height of the component.
The actual effect of the text area component will be different: when it reaches the bottom, it will automatically grow, with no scroll bars, relatively humble. You need to put in the container of the scroll pane that you learned later
12. Panel (JPanel)
JPanel is the simplest container that can be used in swing
13. Use Insets to separate components
Using insets to keep components away from the edge of the container, Insets is an object representing the edge area of the container.
Insets around=new Insets (10,6,10,3); Top edge 10 pixels, left 6, right 3, bottom 10
Integrated with today's study, designed a complete interface for later learning to use:
Java Self-study diary--partⅲ