Dark Horse Programmer -- [Java Basics] -- GUI (graphical user interface), javagui
I. Overview
1. GUI (GraphicalUser Interface): Also known as graphical user Interface, is a way for computer users to interact with computers.
2. There are two ways for users to interact with computers: GUI and CLI.
(1) GUI (Graphical User Interface): Graphical User Interface that displays the computer operation Interface in Graphical mode, which is convenient and intuitive.
(2) CLI (Command LineUser Interface): Command line user Interface, which is a common doscommand line operation. Remember some commands, which are not intuitive.
3. the GUI objects provided by Java exist in the java. Awt and javax. Swing packages:
(1) java. Awt (Abstract Window Toolkit): The Abstract Window Toolkit, which calls the local system method implementation function, is a heavyweight control.
(2) javax. Swing package: a GUI system established on the basis of AWT. It has more components and is fully implemented by java and is a lightweight control.
Ii. inheritance relationship diagram
NOTE: The Container is a Container component. You can add other components in this component by using the add method. Common subclasses of Container: Window and Panel (which cannot exist independently ).
3. layout manager
1. Layout: the arrangement of components in the container.
2. Common layout manager:
● FlowLayout (Stream layout manager)
| The arrangement in the Southeast and Northwest is the default layout management of the Frame.
● GridLayout (grid layout manager)
| Tab.
● GridBayLayout: (grid package layout manager)
| --- Non-Rule Matrix
Iv. FrameDemo
(1) FrameProcedure
1. Create a Frame form:
Frame f = new Frame ("my Frame"); // you can set the title, that is, the form name.
2. Perform basic settings on the form, such as size, position, and layout:
F. setSize (int wight, int hight); // set the form size
F. setLocation (int x, int y); // you can specify the position for displaying the form.
F. setBounds (int x, int y, int wight, int hight), you can also directly use this method to set the size and position
F. setLayout (Layout layout). The parameter is the specified Layout manager, such as FlowLayout.
3. Define components:
Button B = new Button ("my Button"); // you can set the component name.
4. add the component to the form using the add method:
F. add (B); // add the button component to the form
5. display the form:
F. setVisible (boolean B); // you can set the parameter to true or false to check whether the form is displayed.
(2) Frame demo
1 class FrameDemo {2 public static void main (String [] args) {3 Frame f = new Frame ("my frame"); 4 // f. setSize (500,400); 5 // f. set location (400,200); 6 f. setBounds (400,200,500,400); 7 f. setLayout (new FlowLayout (); // sets the streaming layout 8 Button but = new Button ("one Button"); 9 f. add (but); // add the button to the form. 10 f. setVisible (true); 11} 12}
V. event listening Mechanism
(1) Composition of the event listening Mechanism
(1) event source (component ).
(2) events ).
(3) Listener (Listener ).
(4) event processing (the processing method after the event is triggered ).
(2)Event listening Flowchart
(3) event handling steps
(1) determine the event source (container or component ). Register the Listener (Listener) to the event source using the addXXXListener () method of the event source object. This method receives the subclass object of XXXListener or the subclass object of XXXAdapter of XXXListener.
(2) It is generally represented by an anonymous internal class. When overwriting a method, the method parameter is generally received as a variable of the XXXEvent type.
1 Frame f = new Frame ("demo mouse and keyboard listener"); 2 f. addWindowlistener (new WindowAdapter () 3 {4 @ Override5 public void windowClosing (invalid wevent e) 6 {7 System. exit (0); // indicates closing the window 8} 9 });
Note:
(1) After an event is triggered, the event is packaged as an object and passed to the variable of the parameters in the override method. (Including the event source object, which can be obtained through getSource () or getComponent .)
(2) If you use a subclass to implement the WindowListener interface, you need to overwrite the seven methods. You can only use the close action. Other actions are not used, but must be overwritten. Because the WindowLister subclass WindowAdapter has implemented this interface and overwritten all the methods. You only need to inherit the WindowAdapter and overwrite the required method.
(3) Identify the event and process the event. In fact, you need to add the event to any listener.
(4) Application Example
1./* Keyboard Events and mouse events */
1 import java. awt. button; 2 import java. awt. flowLayout; 3 import java. awt. frame; 4 import java. awt. textField; 5 import java. awt. event. keyAdapter; 6 import java. awt. event. keyEvent; 7 import java. awt. event. mouseAdapter; 8 import java. awt. event. mouseEvent; 9 import java. awt. event. windowAdapter; 10 import java. awt. event. using wevent; 11 public class MouseAndKeyDemo {12 private Frame f; 13 private TextField Tf; 14 private Button but; 15 // constructor, used to initialize 16 public MouseAndKeyDemo () {17 init (); 18} 19 // form creation and function implementation 20 private void init () {21 f = new Frame ("mouse and keyboard listener Demo"); 22 f. setBounds (400,200,500,400); 23 f. setLayout (new FlowLayout (); 24 25 tf = new TextField (35); 26 but = new Button ("one Button"); 27 28 f. add (tf); 29 f. add (but); 30 31 myEvent (); 32 33 f. setVisible (true); 34} 35 // register event 36 private void myEvent () {37 // Add to the text box Keyboard listening. 38 tf. addKeyListener (new KeyAdapter () {39 @ Override40 public void keyPressed (KeyEvent e) {41 if (e. isControlDown () & e. getKeyCode () = KeyEvent. VK_ENTER) {42 System. out. println ("enter run... "); 43} 44} 45}); 46 47 f. addWindowListener (new WindowAdapter () {48 @ Override49 public void windowClosing (invalid wevent e) {50 System. exit (0); 51} 52}); 53 54 // Add a mouse listener to the button. 55. addMouseListener (new MouseAdapter () {56 private int count = 1; 57 @ Override58 public void mouseClicked (MouseEvent e) {59 if (e. getClickCount () = 2) 60 tf. setText ("mouse double click... "+ count ++); 61} 62}); 63} 64 public static void main (String [] args) {65 new MouseAndKeyDemo (); 66} 67}
2. List the content in the specified directory. If the input path is incorrect, an error message is displayed.
1/* list the content in the specified directory. If the input path is incorrect, an error message is displayed. */2 import java. io. *; 3 import java. awt. *; 4 import java. awt. event. *; 5 class MyWindowDemo {6 // define the required components to reference 7 private Frame f; 8 private Button but, bok; 9 private TextField tf; 10 private TextArea ta; 11 private Dialog d; 12 private Label lab; 13 14 // constructor 15 MyWindowDemo () {16 init (); 17} 18 19 // The form is basically set to function 20 public void init () {21 // component instantiation 22 f = new Frame ("My Window"); 23 but = new Butto N ("jump"); 24 tf = new TextField (58); 25 ta = new TextArea (27, 65); 26 27 // Basic settings 28 f. setBounds (300,150,500,500); 29 f. setLayout (new FlowLayout (); 30 31 // Add component 32 f. add (tf); 33 f. add (but); 34 f. add (ta); 35 36 // form event 37 myEvent (); 38 39 // form display 40 f. setVisible (true); 41} 42 43 // register event 44 public void myEvent () {45 // disable the form function 46 f. addWindowListener (new WindowAdapter () {47 public void windowClo Sing (semantic wevent e) {48 System. exit (0); 49} 50}); 51 52 // "jump" button event 53. addActionListener (new ActionListener () {54 public void actionreceivmed (ActionEvent e) {55 showFile (); // list the contents of the Directory 56} 57} in the region }); 58 59 // text box keyboard event 60 tf. addKeyListener (new KeyAdapter () {61 public void keyPressed (KeyEvent e) {62 // if you press Enter on the keyboard, the contents of the directory will be displayed in the Keys area 63 if (e. getKeyCode () = KeyEvent. VK_ENTER) 64 showFile (); 65} 66}); 67} 68 69 // method 70 private void showFile () {71 String path = tf. getText (); // get the input path 72 File dir = new File (path); // encapsulate the path into an object 73 // determine whether the input path exists, whether the folder is 74 if (dir. exists () & dir. isDirectory () {75 ta. setText (""); // clear 76 77 String names [] = dir. list (); // list contents in the directory 78 79 for (String name: names) {80 ta. append (name + "\ r \ n"); // Add to region 81} 82} else {83 // dialog box Basic settings 8 4 d = new Dialog (f, "error prompt", true); 85 d. setBounds (400,200,280,150); 86 d. setLayout (new FlowLayout (); 87 88 bok = new Button ("OK"); 89 lab = new Label (); 90 91 // Add Button and text 92 d. add (bok); 93 d. add (lab); 94 95 // dialog box close event 96 d. addWindowListener (new WindowAdapter () {97 public void windowClosing (invalid wevent e) {98 d. setVisible (false); // exit Dialog Box 99} 100}); 101 102 // "OK" button event 103 bok. addActionListener (New ActionListener () {104 public void actionreceivmed (ActionEvent e) {105 d. setVisible (false); // press the OK key to exit the dialog box 106} 107}); 108 109 String info = "path you entered:" + path + "is incorrect, please try again! "; 110 111 lab. setText (info); // set the label text content to 112 d. setVisible (true); // displayed Dialog Box 113} 114} 115 116 public static void main (String [] args) {117 // run form 118 new MyWindowDemo (); 119} 120}
[2012 dark horse programmer] JAVA basics 02 [Top] rar and [2012 dark horse programmer] JAVA basics 02 [bottom] rar decompression Password
Www.itheima.com/main/feature/bxd_25.shtml
Download the password for the last two days.
Other: www.itheima.com/main/studyline/heimaline.html
Hope to help you
What are the main tasks of designing and implementing a graphical user interface in java?
Create a Java array: design the data structure; Consider redundancy; what is the difference between the static initiator and the constructor: The variable settings are different. Www.daohei.com doscommand Daquan what is domain hiding, how is the Program executed: hidden fields are invisible elements used to collect or send information; when the form is submitted, the hidden domain will send the information to the server using the name and value defined during your settings.