Java AWT Learning Notes

Source: Internet
Author: User

Recently these two days, spent some time to review the study of java.awt, so spend some time today to write down their summary.

1. Common components: Button, TextArea, Label, Checkbox, TextField

Container---Window (frame,dialog), Panel

Layout Manager:

FlowLayout (Streaming layout manager)
Ordered from left to right.

Panel default layout manager.

BorderLayout (Boundary layout manager)

East, south, west, north, middle

The default layout manager for frame.

GridLayout (Grid layout manager)

Matrix of rules

CardLayout (Card layout manager)

Options tab

GridBagLayout (Grid package layout manager)

Non-rule matrices

2. Create a graphical interface:

<1> Create a Frame form.
<2> basic settings for the form. such as size, location, layout.
<3> define components.
<4> add a component to the form by using the form's Add method.
<5> let the form show through setvisible (true)

3. Event Monitoring Mechanism

Features of the event monitoring mechanism:
<1> Event source.
<2> events.
<3> Listener.
<4> event handling.

Event Source: These are the graphical interface components in the AWT package or swing package.

Event: Each event source has its own unique corresponding event and common events.

Listener: The action that can trigger an event (more than one action) is already encapsulated in the listener.

All three of them are already defined in Java.
Get the object directly to use it.

What we have to do is to deal with the resulting action.

4. Form Events

F.addwindowlistener (new  windowadapter ()        {            publicvoid  Windowclosing (windowevent e)            {                system.exit (0);                }        });

5.Action Events

Closeitem.addactionlistener (new  ActionListener ()        {            publicvoid  actionperformed (ActionEvent e)            {                system.exit (0);            }        });

6. Mouse events

But.addmouselistener (NewMouseadapter () {Private intCount = 1; Private intClickcount = 1;  Public voidmouseentered (MouseEvent e) {System.out.println ("Mouse into this component" +count++); }             Public voidmouseclicked (MouseEvent e) {if(E.getclickcount () ==2) System.out.println ("Double-click action" +clickcount++); }        });

7. Keyboard events

But.addkeylistener (new  keyadapter ()        {            publicvoid  keypressed ( KeyEvent e)            {                    if(E.iscontroldown () &&e.getkeycode () = =Keyevent.vk_enter)                     //system.exit (0);                System.out.println ("Ctrl+enter is run");                 // System.out.println (Keyevent.getkeytext (E.getkeycode ()) + "...." +e.getkeycode ());             }        });

Use of 8.Dialog

9. Application of the menu

Menubar,menu,menuitem
Create a menu bar, create a menu, and set up menu items in each menu.
The menu can also be added to the menu as a submenu.
Add a menu to the frame by using the Setmenubar () method.

A simple Notepad program code is as follows:

Package MyMenu ;Importjava.awt.*;Importjava.awt.event.*;ImportJava.io.*; Public classmenu1 {Frame F;    MenuBar Bar;    Menu M1; TextArea Ta=NewTextArea ();    MenuItem Openitem,saveitem,exititem;    FileDialog Opendialog,savedialog; PrivateFile FL;  Publicmenu1 () {inite (); }    Private voidInite () {f=NewFrame ("FileList"); F.setsize (300, 400); F.setlocation (100, 200); Bar=NewMenuBar (); M1=NewMenu ("File"); OpenItem=NewMenuItem ("Open"); SaveItem=NewMenuItem ("Save"); Exititem=NewMenuItem ("Exit");        Bar.add (M1); Ta=NewTextArea ();        M1.add (OpenItem);        M1.add (SaveItem);        M1.add (Exititem); OpenDialog=NewFileDialog (F, "OpenFile", Filedialog.load); Savedialog=NewFileDialog (F, "SaveFile", Filedialog.save);        F.setmenubar (bar);        GetEvent ();        F.add (TA); F.setvisible (true); }    Private voidgetEvent () {Saveitem.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {if(fl==NULL) {savedialog.setvisible (true); String Dirpath=savedialog.getdirectory (); String FileName=Savedialog.getfile (); if(dirpath==NULL|| filename==NULL)                        return; FL=NewFile (dirpath,filename); }                Try{bufferedwriter BFW=NewBufferedWriter (NewFileWriter (fl));                    Bfw.write (Ta.gettext ());                Bfw.flush (); } Catch(IOException E1) {e1.printstacktrace ();        }            }            }); Openitem.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {opendialog.setvisible (true); String Dirpath=opendialog.getdirectory (); String FileName=Opendialog.getfile (); if(dirpath==NULL|| filename==NULL)                    return; Ta.settext (""); FL=NewFile (dirpath,filename); Try{BufferedReader BfR=NewBufferedReader (NewFileReader (fl)); String Str=NULL;  while((Str=bfr.readline ())! =NULL) {ta.append (str+ "\ r \ n"); }                } Catch(FileNotFoundException E1) {e1.printstacktrace (); } Catch(IOException E1) {e1.printstacktrace ();        }            }            }); F.addwindowlistener (NewWindowadapter () { Public voidwindowclosing (windowevent e) {system.exit (0);        }        }); Exititem.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {system.exit (0);            }            }); }     Public Static voidMain (string[] args) {Newmenu1 (); }}

10.jar Package Double-click Execute

Build the MyMenu package with java-d compile, and then create a text file in the current directory, it might be 1.txt, the content is:

Main-class:mymenu.menu1

Note the space and the end of the line break

This is the input Jar package generation directive:

JAR-CVFM My.jar 1.txt MyMenu

The My.jar will be generated

WIN7 system open jar file times wrong, prompting "Could not find the main class" warning.

Resolve the issue by modifying the registry.

Step One: Open the registry, start and run (or with the shortcut key win+r), enter regedit, OK;

Step Two: find a Jar file, select Open mode, enter D:/program files/java/jre/bin/javaw.exe, and then select Open on the line;

Step three: enter Hkey_classes_root/applications/javaw.exe/shell/open/command, modify the default key value to "D:/program files/java/jre/bin/ Javaw.exe "-jar"%1 ".

The Java environment is similar to the installation elsewhere, just change the file address.

Java AWT Learning Notes

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.