Java basics-GUI programming (2), java basics gui Programming
I. event listening Mechanism
-- Event Source: the graphical components in the awt or swing package, that is, the components in which the event occurs.
-- Event: An operation performed by the Event user on the component.
-- Listener: Listener is responsible for event processing.
Ii. classes in the java. awt. event package
WindowEvent // window events, such as user-clicked semi-closed windows, window gain or loss of focus, maximized and minimized
MouseEvent // mouse event, mouse press, mouse release, click (Press and then release), etc.
ActionEvent // Action event, which does not represent a specific action, but a semantics, such as a button, menu clicked, or press enter in the text box, can be understood as follows: A user action causes the basic function of a component. This is the ActionEvent event.
Different event types correspond to different event listener interfaces. The interface names correspond to the event names.
WindowEvent-> WindowListener
MouseEvent-> MouseListener
ActionEvent-> ActionListener
Sample Code:
Import java. awt. frame; import java. awt. event. using wevent; import java. awt. event. windowListener; public class Test20 {public static void main (String [] args) {Frame f = new Frame (); f. setSize (400,400); f. setVisible (true); f. addWindowListener (new WindowListener () {@ Override public void windowOpened (custom wevent e) {// The window is opened // TODO Auto-generated method stub} @ Override public void windowClosing (custom wevent e) {// set the disable event // TODO Auto-generated method stub System. exit (0) ;}@ Override public void windowClosed (incluwevent e) {// The user has closed the window // TODO Auto-generated method stub} @ Override public void incluwiconified (incluwevent e) {// when the value is minimized // TODO Auto-generated method stub} @ Override public void windowDeiconified (incluwevent e) {// minimize the restoration time. // TODO Auto-generated method stub} @ Override public void windowActivated (receivwevent e) {// form activated // TODO Auto-generated method stub} @ Override public void windowDeactivated (incluwevent e) {// when the focus is lost // TODO Auto-generated method stub }});}}
Have you found that when you use the WindowListener interface, a lot of uncommonly used code will be introduced (Here we just want to disable it). The methods in the excuse can only be overwritten and cannot be deleted, in this way, the entire project is very arrogant. In order to solve this problem, there is an event adapter.
Iii. Event Adapter
JDK defines the corresponding implementation class for most event listener interface classes (there are many empty implementation methods in it to facilitate the creation of listener objects), which is called the event adapter class. WindowAdapter is used here.
Import java. awt. frame; import java. awt. event. windowAdapter; import java. awt. event. using wevent; public class Test21 {public static void main (String [] args) {Frame f = new Frame ("event adapter chestnut"); f. setSize (400,400); f. setVisible (true); f. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0 );}});}}
You can look at the source code of the WindowAdapter class and find the feeling.
public abstract class WindowAdapter implements WindowListener, WindowStateListener, WindowFocusListener{ /** * Invoked when a window has been opened. */ public void windowOpened(WindowEvent e) {} /** * Invoked when a window is in the process of being closed. * The close operation can be overridden at this point. */ public void windowClosing(WindowEvent e) {} /** * Invoked when a window has been closed. */ public void windowClosed(WindowEvent e) {} /** * Invoked when a window is iconified. */ public void windowIconified(WindowEvent e) {} /** * Invoked when a window is de-iconified. */ public void windowDeiconified(WindowEvent e) {} /** * Invoked when a window is activated. */ public void windowActivated(WindowEvent e) {} /** * Invoked when a window is de-activated. */ public void windowDeactivated(WindowEvent e) {} /** * Invoked when a window state is changed. * @since 1.4 */ public void windowStateChanged(WindowEvent e) {} /** * Invoked when the Window is set to be the focused Window, which means * that the Window, or one of its subcomponents, will receive keyboard * events. * * @since 1.4 */ public void windowGainedFocus(WindowEvent e) {} /** * Invoked when the Window is no longer the focused Window, which means * that keyboard events will no longer be delivered to the Window or any of * its subcomponents. * * @since 1.4 */ public void windowLostFocus(WindowEvent e) {}}
Practice a few event handlers.
Example 1:
Import java. awt. button; import java. awt. flowLayout; import java. awt. frame; import java. awt. event. actionEvent; import java. awt. event. actionListener; // Example 1: place a button in the form and click it to exit the class TestFrame implements ActionListener {// there is only one method in the ActionListener interface. The private Frame f is overwritten below; public TestFrame () {f = new Frame ("window"); init ();} private void init () {f. setSize (300,300); f. setLayout (new FlowLayout (); // Layout mode: Button B = new Button ("Exit program"); B. addActionListener (this); f. add (B); f. setVisible (true) ;}@ Override public void actionreceivmed (ActionEvent e) {// TODO Auto-generated method stub f. setVisible (false); f. dispose (); // you can use it to destroy the System. exit (0); // exit} public class Test22 {public static void main (String [] args) {new TestFrame ();}}
In the preceding example, you can click the exit program button to exit. You cannot exit by clicking X in the upper-right corner. Because WindowListener is not set.
In this example, the ActionListener interface is used. You can check its source code as follows:
public interface ActionListener extends EventListener { /** * Invoked when an action occurs. */ public void actionPerformed(ActionEvent e);}
Example 2:
Import java. awt. color; import java. awt. flowLayout; import java. awt. frame; import java. awt. textField; import java. awt. event. keyAdapter; import java. awt. event. keyEvent; import java. awt. event. windowAdapter; import java. awt. event. define wevent; // In the form, place a text box to filter out invalid characters (except numbers) public class TestFrame {private Frame f; private TextField txtNo; // TextField indicates the public TestFrame () {f = new Frame ("enter the password"); // f. setBackground (red); I tried it here, but it didn't work.
F. setBackground (Color. red); // you can
F. setBounds (50, 50,400,400); // set the window coordinate and size. setLayout (new FlowLayout (); // sets the form layout txtNo = new TextField (10); // sets the display length of the input window. The length of the input content has no limit. add (txtNo); txtNo. addKeyListener (new KeyAdapter () {// paste this method from JDk to use public void keyPressed (KeyEvent e) {int code = e. getKeyCode (); if (! (Code> = KeyEvent. VK_0 & code <= KeyEvent. VK_9) {// set the input content to 0-9 System. out. println (KeyEvent. getKeyText (code) + "incorrect input"); e. consume () ;}}); f. setVisible (true); f. addWindowListener (new WindowAdapter () {public void windowClosing (WindowEvent e) {f. dispose (); System. exit (0 );}});}}
Test class:
public class Test23 { public static void main(String[] args) { new TestFrame(); }}
Example 3:
List the contents of a specified directory:
Import java. awt. button; import java. awt. flowLayout; import java. awt. frame; import java. awt. textArea; import java. awt. textField; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import java. io. file; public class Test24 {public static void main (String [] args) {new MyWindow () ;}} class MyWindow {MyWindow () {init () ;}private Frame f; private Button B; private TextField txtDir; // enter the directory name private TextArea txtFileList; // display the file list private void init () {f = new Frame ("window "); f. setBounds (44, 44,500,500); f. setLayout (new FlowLayout (); txtDir = new TextField (8); B = new Button ("show"); txtFileList = new TextArea (20, 30 ); // Area f used to display the file list. add (txtDir); f. add (B); f. add (txtFileList); initEvent (); f. setVisible (true);} private void initEvent () {// TODO Auto-generated method stub f. addWindowListener (new WindowAdapter () {/*** Invoked when a window is in the process of being closed. the * close operation can be overridden at this point. */public void windowClosing (invalid wevent e) {System. exit (0) ;}}); B. addActionListener (new ActionListener () {/*** Invoked when an action occurs. */public void actionreceivmed (ActionEvent e) {txtFileList. setText (""); String dirStr = txtDir. getText (); // retrieve the user input path File = new file (dirStr); if (File. isDirectory () & file. exists () {String [] fileNameList = file. list (); for (String s: fileNameList) {txtFileList. append (s + "\ r \ n"); // do not forget the line break} else {txtFileList. append ("incorrect input. Please enter it again ");}}});}}
Result: (I want it to display the directory of my d disk)