Chapter 2: create a window and Applet

Source: Internet
Author: User
Tags new set

One basic principle to be followed in the design is to make simple things easier and make difficult things more feasible!
1. Java GUI, including the old version of AWT and the new swing, eclipse also has a new set of GUI library SWT.
AWT is very old and should not be used unless in special cases.

2. Swing is much better than other GUI programming models, and JavaBean is the framework of this library. Complex GUI that can be used
Quick development of GUI construction tools.
It is specific to: 1) dynamically binding events. 2) Separate the business logic from the user interface logic.
3. Writing an applet is very limited. This means that the applet cannot access the local disk. Generally, you need to package the applet into a jar file,
Each entity in the jar file can use "Digital Signature ".
However, the advantages of Applet are as follows: 1) installation is not required. 2) Don't worry that bad code will damage others' systems.

4. The Program Framework of the applet.
Init () is automatically called to initialize the applet for the first time, including the layout of the component. The method must always be overwritten.
Start () whenever the applet enters the web browser's field of view, this method is called so that the applet can enable its common functions.
(Especially the function that is disabled by stop ). It is called after init.
Stop () is called whenever the applet leaves the browser. This allows the applet to disable its "expensive" operations and be called before destroy.
Destroy (). When the applet is not used, it is called to uninstall the applet from the webpage to release the resource.

5. To allow the applet in the browser, you only need to add the following to HTML:
<APPLET code = applete1 width = 100 Height = 50> </APPLET>
However, for platform reasons, the browser cannot be expected to support the correct style of Java. Therefore, Java provides the htmlconverter tool,
Process the previous applet.
6. Sun provides the appletviewer tool to extract the <APPLET> tag from the HTML file, and then only run this applet instead
Displays the content of the surrounding HTML file. It ignores all information except <APPLET>. You can add it to the Java source file.
// <APPLET code = myapplet width = 20 Height = 100> </APPLET>
Then use appletviewer <JAVA Source File>

7. You can add the main function for the applet so that the applet can run from the command line.

8. All swing components have the Add ××× listener () and remove ××× listener () methods. Add or remove event processors to or from components.
This event model can be expanded. The event processor XXX implements the corresponding interface. A simple reality is to use the inherited ×× adapter.

9. Basic swing components include descriptions:
1) buttons, including check boxes, single-choice buttons, and menu items are inherited by abstractbutton.
(Use basicarrowbutton, jtogglebutton). To make the single-choice button show some exclusive behavior, you must add them
To the button group ).
2) jlabel or any component inherited from abstractbutton can use the icon and settooltiptext of jcomponet.
Method to add a tooltip or jtooltip class. setborder sets various borders for the component (titledborder, etchedborder,
Lineborder, bevelborder, softbevelborder ).
3) The text field jtextfield, common jtextarea listeners documentlistener, plaindocument
4) rolling Panel (jscrollpane ).
5) The jtextpane control supports multiple editing operations without any effort.
6) check box (jcheckbox), single-choice button (jradiobutton), combo box (jcombobox), and list box (jlist)
Page Panel (jtabbedpane), message box (joptionpane. showmessagedialog ),
Menu (related classes, jmemu, jmenuitem, jcheckboxmenuitem, jradiobuttonmenuitem)
Pop-up menu (jpopupmenu), dialog box (jdialog, similar to jframe), file dialog box (jfilechooser,
Slider and progress bar (jprogressbar, jslider), tree (jtree), table (jtable)
7) Any jcomponent can be drawn, but if you only want to have a plane for direct plotting, inherit jpanel and overwrite it.
Paintcomponent. Swing will pass a graphics object when this method is called.
8) Any component that can accept text can accept HTML text.
9) Select the appearance, instance code:
Try {
Uimanager. setlookandfeel (uimanager. getsystemlookandfeelclassname ());
}...
10) jfc provides Clipboard support (in the Java. AWT. datatransfer package ):
Clipboard clipbd = gettoolkit (). getsystemclipboard ();

10. Use jar to package the applet, for example, Jar CF packagename. jar *. Class. Keytool is the applet signature. It is an applet.
Access the local disk.

11. Java Web Start is a reference implementation of the Java Network Publishing protocol JNLP. Through the JNLP program, you can download and install it on the client.
Standalone version of Java applications.

12. Swing is multi-threaded. In swing, multithreading may compete for resources with swing event scheduling threads. Therefore, swing provides
Two mechanisms. Swingutilities. invokelater (), swingutilities. invokeandwait (), they accept runable objects.
Swingutilities. invokeandwait is a necessary condition for deadlock. Be careful .. Invokelater is more common, after Initialization
You can use these methods to set the properties of the swing component at any time.

13. Javabean is a coding rule:
1) The object XX attribute. Generally, two methods are written: getxxx () and setxxx ().
2) For Boolean properties, you can use is to replace get.
3) The common bean method does not have to comply with the above rules, but must be public.
4) for events, use swing to process the listener. Addxxxlistener (), removexxxlistener ()

14. The Java reflection mechanism can discover all the methods of the unknown class (JavaBean. Another simpler solution is the introspector class. The most important one is
The static method getbeaninfo is used to pass a Class Object application.

15. When creating a JavaBean, you must assume that it may run in a multi-threaded environment. So;
1) Use synchronized whenever possible for all public methods in bean.
2) When a multi-channel event triggers a group of listeners Interested In the event, it must be assumed that the listener may be
Add or remove.

16. For more information, see JavaBean, by elliotte Rusty Harold.

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.