Java Basic--gui Programming (III)

Source: Internet
Author: User

Then the first two study notes, this article mainly introduces the Layout Manager and the dialog box two parts content.

First, layout manager

Take a small example to elicit the topic, according to our random add two buttons, will produce what effect, see the results of the implementation.

ImportJava.awt.Button;ImportJava.awt.Frame; Public classTest25 { Public Static voidMain (string[] args) {Frame F=NewFrame (Layout Manager); F.setsize (300, 400); F.add (NewButton ("Buttons a")); F.add (Newbutton ("B")); F.setvisible (true); }}

Execution Result:

From the Run program, you see only the second button, B.

The reason is that each component has a specific location and size in the container, it is difficult to determine its position when arranging components in a container, and to simplify user control, Java takes the layout manager to manage it to specify a different layout manager for the container.

1.BorderLayout (Border layout, default mode) divides a container into five regions

  Frame f=New frame ("layout manager");      F.add (thenew button ("button a"), Borderlayout.north);      F.add (thenew button ("button B"));      F.add ( "button C"), "East");      F.setbounds (20,20,300,300);      F.setvisible (true);

2. FlowLayout from left to right, top to bottom, if we add "North", "East" parameters in the FlowLayout layout, it will be ignored.

Frame f=New frame ("layout manager");    F.setlayout (new  FlowLayout ());    F.add (thenew button ("button a"), Borderlayout.north);    F.add (thenew button ("button B"));    F.add (thenew button ("button B"));

3. GridLayout grid, from left to right, top to bottom, the layout always ignores the optimal size of the component, and the width of all cells is the same

4.CardLayout Tab Layout

5.GridBagLayout layout of the king, powerful, use complex,

A new layout manager in 6.BoxLayOut Swing allows multiple components to be placed all vertically or horizontally, nesting multiple panels of that Layout manager, enabling similar

7.GridBagLayout functionality, but is much simpler than using GridBagLayout

A code example of a more complex point (with the intention of the left column three row grid layout, the Right tab layout, and the left button pressed will trigger the corresponding change on the right)

Importjava.awt.BorderLayout;ImportJava.awt.Button;Importjava.awt.CardLayout;ImportJava.awt.Color;ImportJava.awt.Frame;Importjava.awt.GridLayout;ImportJava.awt.Panel;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;//Example of layoutclassLayouttest { Publiclayouttest () {init (); } cardlayout CL=NewCardLayout ();//Card LayoutPanel Plcenter =NewPanel ();//Panel, Container    Private voidinit () {Frame F=NewFrame (); Panel plwest=NewPanel ();        Plwest.setbackground (Color.pink);        Plcenter.setbackground (Color.dark_gray); F.add (Plwest, borderlayout.west); //let's go left .F.add (Plcenter); Plwest.setlayout (NewGridLayout (3, 1));//Grid Layout, three rows, one columnButton Btnprev =NewButton ("Previous"); Button Btnnext=Newbutton ("latter"); Button Btnthree=NewButton (a "third");        Plwest.add (Btnprev);        Plwest.add (Btnnext);        Plwest.add (Btnthree); Plcenter.setlayout (CL); //Middle main panel, set as card layoutPlcenter.add (NewButton ("A"), "1AAA"); Plcenter.add (NewButton ("B"), "2AAA"); Plcenter.add (NewButton ("C"), "3AAA"); Plcenter.add (NewButton ("D"), "4AAA"); Plcenter.add (NewButton ("E"), "5AAA"); Plcenter.add (NewButton ("F"), "6AAA"); //Internal classes for button event handling        classMyactionlistenerImplementsActionListener { Public voidactionperformed (ActionEvent e) {if(E.getactioncommand (). Equals ("Previous") {cl.previous (plcenter); } Else if(E.getactioncommand (). Equals ("latter") {cl.next (plcenter); } Else if(E.getactioncommand (). Equals ("Third") {cl.show (Plcenter,"5AAA");//specifies which one to display}}} Myactionlistener listener=NewMyactionlistener ();        Btnprev.addactionlistener (listener);        Btnnext.addactionlistener (listener);        Btnthree.addactionlistener (listener); F.setsize (300, 300); F.setvisible (true); F.addwindowlistener (NewWindowadapter () { Public voidwindowclosing (windowevent e) {system.exit (0);    }        }); }}classTest26 { Public Static voidMain (string[] args) {Newlayouttest (); }}

Note: Cancel the layout manager (code example below)

You can use absolute coordinates to specify the location and size of the component, at which point we call container.setlayout (NULL) and then call the Container.setbounds () method.

ImportJava.awt.Button;ImportJava.awt.Frame; Public classTest27 {//example, the location of the custom button     Public Static voidMain (string[] args) {Frame F=NewFrame (); F.setsize (500, 400); F.setlayout (NULL);//canceling the layout managerButton BTN =NewButton ("This is the buttons"); Btn.setbounds (50, 50, 80, 40);        F.add (BTN); F.setvisible (true); }}

Ii. dialog box (Dialog)

The dialog box is often seen in our daily use of computers, not to repeat them. Directly on the code example, proficiency in the application.

ImportJava.awt.Button;ImportJava.awt.Dialog;Importjava.awt.FlowLayout;ImportJava.awt.Frame;ImportJava.awt.Label;ImportJava.awt.TextArea;ImportJava.awt.TextField;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent;ImportJava.io.File; Public classTest28 { Public Static voidMain (string[] args) {NewMywindow (); }}classMywindow {Mywindow () {init (); }    PrivateDialog Dlg;//dialog Box    PrivateLabel lblmsg;//label    PrivateFrame F; PrivateButton btn; PrivateTextField Txtdir;//used to enter a directory name    PrivateTextArea txtfilelist;//used to display a list of files    Private voidinit () {f=NewFrame ("Directory Viewer"); F.setbounds (30, 30, 400, 400); F.setlayout (NewFlowLayout ()); Txtdir=NewTextField (30);//30 Character LengthBTN =NewButton ("Show file"); txtFileList=NewTextArea (20, 35);//the area used to display the list of filesF.add (Txtdir);        F.add (BTN);        F.add (txtfilelist); Dlg=NewDialog (F, "hint message");//This true indicates whether it is modal or not.
If it is dlg=new Dialog (F, "hint message", true), the representative will not be able to manipulate the subsequent dialog box after the pop-up dialog .
Lblmsg =NewLabel ("The entered directory is incorrect"); Dlg.add (LBLMSG); Dlg.setbounds (200, 200, 250, 100); Dlg.setlayout (NewFlowLayout ()); Initevent (); F.setvisible (true); } Private voidinitevent () {Dlg.addwindowlistener (NewWindowadapter () { Public voidwindowclosing (WindowEvent e) {((Dialog) E.getsource ()). setvisible (false); //dlg.setvisible (false); }; }); F.addwindowlistener (NewWindowadapter () { Public voidwindowclosing (windowevent e) {system.exit (0); } }); Btn.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {Txtfilelist.settext (""); String Dirstr= Txtdir.gettext ();//Take out the path entered by the userFile File =NewFile (DIRSTR); if(File.isdirectory () &&file.exists ()) {string[] filenamelist=file.list (); for(String s:filenamelist) {txtfilelist.append (s+ "\ r \ n");//don't forget to add a line break } } Else { //txtfilelist.append ("What are you typing?");Dlg.setvisible (true); } } }); }}

Java Basic--gui Programming (III)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.