Java programming-sun Xin Java is no difficulty lesson10 Java graphic interface programming
Highlights of this section:
1. Java graphical interface programming
2. AWT, layout manager, event model,
3. jfc and swing programming. Use JBuilder to quickly develop graphical interface programs.
Details:
1. AWT (Abstract Window Toolkit)
AWT Abstract Window Toolkit, a class library provided by Sun for graphic interface programming (GUI. The basic AWT library processes user interface elements by entrusting the creation and behavior of these elements to a local GUI tool on each target platform (such as Windows, UNIX, and Macintosh) for processing. For example, if we use awt to place a button in a Java window, we actually use a button with a local look and feel. In this way, theoretically, the graphic interface program we have compiled can run on any platform to achieve cross-platform running of the graphic interface program.
2. layout manager
The position and size of components in the container are determined by the layout manager. The container keeps a reference to a specific instance of the layout manager. When the container needs to locate a component, it will call the layout manager. This is also true when determining the size of a component. In AWT, Five layout managers are provided: borderlayou, flowlayout, gridlayout, cardlayout, and gridbaglayout.
We can control the size and position of components by setting an empty layout manager. Call setlayout (null ).
After setting the empty layout manager, you must call setlocation (), setsize (), or setbounds () for all components to locate them in the container.
The Java layout test code is as follows:
// Java layout test code import Java. AWT. borderlayout; import Java. AWT. button; import Java. AWT. cardlayout; import Java. AWT. color; import Java. AWT. flowlayout; import Java. AWT. frame; import Java. AWT. gridlayout; import Java. AWT. panel; import Java. AWT. event. actionevent; import Java. AWT. event. actionlistener; import Java. AWT. event. windowadapter; import Java. AWT. event. extends wevent; public class yourframe extends frame {/*****/Private Static final long serialversionuid = 1l; private panel boderpanel; private panel flowpanel; private panel gridpanel; private panel cardpanel; // event listening class yourwindowlistener extends windowadapter {}// framework class public yourframe (string strtitle) {super (strtitle); setsize (600,400); setlocation (100,100 ); // set the starting position setborderlayoutpanel (); setflowlayoutpanel (); setgridpanel (); setcardlayout (); color Cl = new color (0,255, 0); setbackground (CL ); setlayout (New gridlayout (2, 2); add (boderpanel); add (flowpanel); add (gridpanel); add (cardpanel); addwindowlistener (New yourwindowlistener () {public void windowclosing (invalid wevent arg0) {// todo auto-generated method stub system. exit (0) ;}}) ;}// borderlayout layout public void setborderlayoutpanel () {boderpanel = new Panel (); boderpanel. setlayout (New borderlayout (); button btn1 = new button ("center"); button btn2 = new button ("East "); button btn3 = new button ("South"); button btn4 = new button ("West"); button btn5 = new button ("North"); boderpanel. add (btn1, borderlayout. center); boderpanel. add (btn2, borderlayout. east); boderpanel. add (btn3, borderlayout. south); boderpanel. add (btn4, borderlayout. west); boderpanel. add (btn5, borderlayout. north);} // flowlayout layout public void setflowlayoutpanel () {flowpanel = new Panel (); flowpanel. setlayout (New flowlayout (); button btn1 = new button ("Java button test"); // modify the tag string btn1.addactionlistener (New youractionlistener () {public void actionreceivmed (actionevent E) {(button) E. getsource ()). setlabel ("button pressed") ;}}); button btn2 = new button ("frame"); flowpanel. add (btn1); flowpanel. add (btn2);} // gridlayout layout public void setgridpanel () {gridpanel = new Panel (); gridpanel. setlayout (New gridlayout (2, 2); button btn1 = new button ("one"); button btn2 = new button ("two "); button btn3 = new button ("three"); button btn4 = new button ("four"); gridpanel. add (btn1); gridpanel. add (btn2); gridpanel. add (btn3); gridpanel. add (btn4);} // cardlayout layout public void setcardlayout () {final cardlayout Cl = new cardlayout (); cardpanel = new Panel (); cardpanel. setlayout (CL); button btn1 = new button (" a"); button btn2 = new button (" K"); actionlistener Al = new actionlistener () {public void actionreceivmed (actionevent e) {Cl. next (cardpanel) ;}}; btn1.addactionlistener (Al); btn2.addactionlistener (Al); cardpanel. add (btn1, "1"); cardpanel. add (btn2, "2") ;}@ suppresswarnings ("deprecation") public static void main (string [] ARGs) {yourframe YF = new yourframe ("frame test "); YF. show () ;}} class youractionlistener implements actionlistener {@ override public void actionreceivmed (actionevent arg0) {// todo auto-generated method stub }}
Shows the running effect:
3. AWT event model
Events: an object that describes what happened.
Event Source: The event generator.
Event Handlers: A method used to receive event objects, interpret event objects, and process user interactions.
The event model diagram is as follows:
The code of the Program for reading files with menus is as follows:
Import Java. AWT. filedialog; import Java. AWT. frame; import Java. AWT. menu; import Java. AWT. menubar; import Java. AWT. menuitem; 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. fileinputstream; import Java. io. filenotfoundexception; import Java. io. ioexcep Tion; public class hisframe extends frame {/*****/class hiswindowlistener extends windowadapter {} Private Static final long serialversionuid = 1l; /*** @ Param ARGs */hisframe (string strtitle) {super (strtitle); Final hisframe HF = This; setsize (400,400); setlocation (100,100 ); // textfield TF = new textfield ("", 20); Final textarea TA = new textarea (); add (TA); addwindowlistener (New hiswindowlistene R () {public void windowclosing (invalid wevent arg0) {// todo auto-generated method stub system. exit (0) ;}}); // create the directory toolbar menubar MB = new menubar (); // create a menu and menu item menu mfile = new menu ("file "); menuitem mfile1 = new menuitem ("new"); menuitem mfile2 = new menuitem ("open"); // response menu command mfile2.addactionlistener (New actionlistener () {@ suppresswarnings ("deprecation") Public void actionreceivmed (actionevent e) {filed Ialog FD = new filedialog (HF, "Open File", filedialog. load); FD. show (); string strfile = FD. getdirectory () + FD. getFile (); // get the object if (! Strfile. isempty () {try {fileinputstream FCM = new fileinputstream (strfile); byte [] Buf = new byte [3000]; int Len = fiis. read (BUF); string STR = new string (BUF, 0, Len); TA. append (STR);} catch (filenotfoundexception E1) {// todo auto-generated Catch Block e1.printstacktrace ();} catch (ioexception E2) {// todo auto-generated Catch Block e2.printstacktrace () ;}}}); menuitem mfile3 = new menuitem ("save "); menuitem mfile4 = new menuitem ("Save as"); menuitem mfile5 = new menuitem ("exit"); mfile5.addactionlistener (New actionlistener () {public void actionreceivmed (actionevent e) {system. exit (0) ;}}); mfile. add (mfile1); mfile. add (mfile2); mfile. add (mfile3); mfile. add (mfile4); mfile. add (mfile5); MB. add (mfile); menu medit = new menu ("edit"); menuitem medit1 = new menuitem ("undo"); menuitem medit2 = new menuitem ("copy "); menuitem medit3 = new menuitem ("cut"); menuitem medit4 = new menuitem ("replace"); medit. add (medit1); medit. add (medit2); medit. add (medit3); medit. add (medit4); MB. add (medit); setmenubar (MB);} public static void main (string [] ARGs) {// todo auto-generated method stub hisframe HF = new hisframe ("Java frame"); HF. show ();}}
Shows the program running effect:
4. Java Basics
Jfc (Java foundation classes): a complete set of GUI components and services, including five APIs: AWT, java2d, accessibility, Drag & drop, and swing. Jfc provides a complete set of application development kits to help developers design complex applications.
Java2d is a set of graphic APIs that provide Java applications with a set of advanced classes for 2D (2D) graphic image processing. The java2d API extends the java. AWT and Java. AWT. Image classes, provides a rich set of drawing styles, defines the complex graphics mechanism, and carefully adjusts the painting process methods and classes. These APIs simplify the development of graphic applications independent from the platform.
The Accessibility API provides an advanced set of tools to help develop applications that use non-traditional inputs and outputs. It provides an auxiliary technical interface, such as screen reader, screen amplifier, and auditory text reader (voice processing.
Drag & drop technology provides the interoperability between Java and local applications, used to exchange data between Java applications and applications that do not support Java technology.
The focus of the jfc module is swing. Swing is used for window-based application development. It provides a rich set of components and work frameworks to specify how the GUI is independent from the platform to display its visual effects. The following code is used to test the interface program created using the classes in the swing package:
import javax.swing.*;public class SwingTest{ @SuppressWarnings("deprecation") public static void main(String[] args) { JFrame jf=new JFrame("JFrame"); jf.setSize(300, 400); jf.show(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE ); JButton jb=new JButton("Javax.swing"); jf.getContentPane().add(jb); }}
// Shows the running effect:
Use the auxiliary tools provided by the system to create interface programs.