I. GUI Overview:
Graphical user interface (graphics Interface, GUI). A class library in the Java Operations graphical interface is called the Abstract tool collection (Abstraction Window Toolkit, AWT). It provides basic graphics components for Java applications.
All classes related to AWT programming are placed in the java.awt package and its child packages. There are two base classes in AWT programming: Component and MenuComponent.
Two. Operation of the graphical interface:
General steps to create a graphical interface:
1. Create a frame form.
2, basic settings for the form. such as size, location, layout and so on.
3, define the components.
4. Add components to the form.
5, add the event monitoring mechanism.
6. Let the form display.
The most important step in the above is 5, add event monitoring mechanism.
Event Monitoring:
Features of the event monitoring mechanism:
1, Event Source: The AWT package or swing package of those graphical interface components.
2, Events: Each event source has its own unique corresponding events and shared events.
3. Listener: The action that can trigger an event (not unique) is already encapsulated in the listener.
4, event processing.
1 Import java.awt.*; 2 Import Java.awt.event.WindowAdapter; 3 Import java.awt.event.WindowEvent; 4 Import Java.awt.event.WindowListener; 5 public class Framedemo {6 public static void main (string[] args) {7//create a non-visible form 8 frame frame = n EW Frame (); 9//Set the form size. Ten Frame.setsize (500, 400); 11//Set the form pop-up position: frame.setlocation (200, 300); 13//Set layout. Frame.setlayout (New FlowLayout ()); 15//Create menu bar MenuBar MB = new MenuBar (); 17//Create menu 18 Menu m = new Menu ("file"); 19//Create submenu MenuItem Closeitem = new MenuItem ("Exit"); M.add (Closeitem); Mb.add (m); 23//Create button ("OK") with buttons button = New button 25//Add menu Frame.setmenubar (MB), Frame.add (button), 28//Add Listener Frame.addwind Owlistener (New Windowadapter () {public void windowclosing (WindowEvent e) {system.exit (0) ; 32 }33}); 34//Call the Component class's setvisible (Boolean) to display. Frame.setvisible (True); 36}37}
Code Framedemo, a simple demonstration of the steps to manipulate the graphical interface:
Three. AWT Common components:
AWT components need to invoke the graphical interface of the running platform to create and platform-consistent peers, so AWT can only use common components that are supported by all platforms.
The basic components provided by AWT are:
> button: Buttons.
> Canvas: Canvases for painting.
> CheckBox: CheckBox Component (can also become a radio box component).
> CheckboxGroup: For multiple checkbox components to be grouped together, a set of CheckBox components will only have one can be selected, that is, all become a radio box component.
> Choice: Drop-down selection box group.
> Frame: Window.
> Label: Label class, you can place suggestive text.
> List: ListBox component, you can add multiple entries.
> Panel: A basic container that cannot exist alone must be placed in another container.
> Scrollbar: Slide bar assembly.
> ScrollPane: A container component with horizontal and vertical scrollbars.
> TextArea: Multiline Text field.
> TextField: Single-line text box.
Four. Graphic operation exercises:
Exercise 1: Enter a table of contents in the text box and click the Go button to list the files and folder names in the directory below the text area
Ideas:
1. Draw a GUI window.
2. Add listener events.
3, through file operation files.
1 public class Readfilegui {2//define required components according to the topic: 3 private frame frame; 4 private Button Tofilebutton; 5 Private TextField Dirtext; 6 private TextArea Fileanddirarea; 7 8//Error prompt Dialog 9 private Dialog errdialog; private Label Inforlabel; one private Button okbutton; 12 Readfilegui () {14}15//initialization method: public void init () {+ frame = new Frame ("read "Frame.setbounds (300,200,500,400); 19//Set to cannot change form size. Frame.setresizable (false); Frame.setlayout (new FlowLayout ()); Tofilebutton = n ew button ("Go to"); Dirtext = new TextField (40); Fileanddirarea = new TextArea (null,, textarea.scrollbars_both); 26//Set to not editable: Fileand Dirarea.seteditable (FALSE); 28 29//Initialize the error prompt box. Errdialog = new Dialog (frame, "hint", true); Errdialog.setbounds (300,200,350,100); ErrDialog.setresizable (FALSE); Errdialog.setlayout (New FlowLayout ()), Inforlabel = new Label (), OKButton = New button (" "); Errdialog.add (Inforlabel); Panax Notoginseng Errdialog.add (OKButton); Frame.add (Dirtext); Frame.add (Tofilebutton), Frame.add (Fileanddirarea); Addlistenerevent (); frame.setvisible (true); 44}45//Add listener event the public void Addl Istenerevent () {Frame.addwindowlistener (new Windowadapter () {$ public void windowclosing (Windoweven T e) {system.exit (0); 50}51}) 52//Add Tofilebuttion Listener event Tofileb Utton.addactionlistener (new ActionListener () {actionperformed (ActionEvent e) {55 Press the Go button to read the data in the Dirtext and print it in Fileadndirarea. Readdirandfile () 57}58}) 59//Add Dirtext Listener event, press ENTER to print out directory file 60 Dirtext.addkeylistener (New Keyadapter () {e.get public void keypressed (KeyEvent e) {The KeyCode () = = E.vk_enter) readdirandfile (); 64}65}); 66//Add OKButton Monitor Event. Okbutton.addactionlistener (new ActionListener () {69 public void actionperformed (ActionEvent e) Errdialog.setvisible (FALSE);}71});}73 public void Readdirandfile () {7 4 String str = dirtext.gettext (); file dir = new file (str); if (dir.exists () && dir.isdirectory ()) {Fileanddirarea.settext (""); tring[] FileNames = Dir.list (); Name:filenames for (String) {fileanddirarea.append (name+ "\t\n"); 81}82 }83 else {inforlabel.settext ("path: '" +str+ "is incorrect, please re-enter.) "); Errdialog.setvisible (true);//FileAnddirarea.settext ("Path format Error! ");}89}88 public static void Main (string[] args) {new Readfilegui (); 92}93}
Run the result diagram:
Dark Horse programmer-java Basic-gui