Java experience: Very inspiring (v)

Source: Internet
Author: User

With regard to Java Learning , this article I am going to talk about XML parsing packages and Java Swing.

1. Java parsing of XML

I believe you are not familiar with XML, meaning is Extensible Markup language. itself it is also the carrier of a data in the form of a tree representation. Then slowly the data becomes information, the difference is that the information can include a mutable state so that the process of hard coding for the program changes to hard-coded for the unified interface and mutable state as information into the XML storage. The only way to change the state to extend is to add a piece of textual information to the XML, and the code does not need to be altered or recompiled. This flexibility was not thought of when the XML was born.

Of course, if the interface is to be able to extract the information configured in the XML requires the program to parse the canonical XML file, Java, of course, should improve the package to support this behavior. The two packages I intend to talk about are org.w3c.dom and javax.xml.parsers. (You can browse through the interfaces and class definitions in the middle of these packages)

Javax.xml.parsers package is very simple, no interface, two factories with two parsers. There are obviously two ways to parse XML: DOM parsing and sax parsing. In essence, no one who is good or bad, but the realization of the idea is not the same.

The idea of Dom parsing is to put the whole tree into memory, need that node only need to search in the tree to read the properties of the node, content, etc., the advantage is that all nodes in memory can be repeated search reuse, the disadvantage is that the need to consume the corresponding memory space.

The idea of natural sax parsing is to overcome the shortcomings of the DOM, to trigger as the basic idea, the order of the search down, encountered element before triggering what events, encountered after doing what action. Due to the need to write the processing of the triggering event, it is necessary to use another custom handler in the Org.xml.sax.helpers package. Its advantages of course is not the entire package is read into the memory, the disadvantage is only the sequential search, go through once again.

It is easy to guess which of the Java EE frames it is exposed to, obviously the DOM. Because a similar struts,hibernate framework configuration file is a small part of the configuration information, and requires frequent searches to read, of course, the DOM approach (in fact, sax is also used in the DOM structure to store node information). Now, no matter what the framework, it is difficult to find the use of Sax parsing XML technology, if anyone knows, please let the author also learn to learn.

Now that the parsing is in place, you need to have the parsed storage location. Do not know if you find org.w3c.dom This package is not implemented classes are all interfaces. Here I would like to say how Java for XML parsing is what the JDK should consider, it is its responsibility. While the organization is to maintain the definition of XML standards of the organization, so an XML structure is the decision of the world, it does not care about how to implement Java, so that the structure of all XML storage should follow the rules, this is the Org.w3c.dom all the interface purposes. In my opinion, a simple understanding of the concept of interface is the principle that the realization must obey.

The structure of the entire XML is called the document, sub-elements corresponding to the called element, as well as node-related nodes, NodeList, Text, Entity, Characterdata, Cdatasection and other interfaces, they can be in the XML Find the corresponding meaning in the middle of the grammar. Since this is not an explanation of the basic XML syntax, there is no more introduction. If you are interested, I can also write a special article about the syntax of XML to share with you.

2. Java Swing

Swing is a love-and-hate thing, and the cute part is that it's easy to get started, and more powerful than AWT provides the interface, and what's hateful is that the complexity of the interface work is huge. I've written more than 3000 lines of swing interface, feeling that the user experience is not so good. Recently wrote more than 6000 lines, due to more functional modules, the overall effect is just general-like. Experience the deepest one word: tired! So now we're not using swing much in the real development of the project, too many interface technology can replace it. The author is forced to write is also under the helpless group inside everyone has not written, I do not enter the region who into?

Although swing is slowly being overlooked, especially with the B/s slowly drowning in C, the author is willing to stand out for swing. The mastery of every technology is by no means a fad for fashion. Friends who really like Java should still have a good experience of swing, believing that many students in school are learning it. It is likely that many of the university teachers who came from JDK 1.1 and 1.2 are probably the least familiar with it.

Swing provides a set of lightweight components collectively known as JComponent, the biggest difference between them and AWT components is that jcomponent are all container, and container is characterized by the ability to load other components inside. Any other component can be mounted in swing components, JButton, JLabel, JPanel, JList, and so on. The advantage is that programmers can "re-develop" the swing components and build their own buttons, labels, artboards, lists and the like for specific needs.

There is light and natural weight, then light components and reorganization of the difference is? The form represented by the reorganization varies depending on the operating system, and the light component is Swing's own GUI, which is the most consistent across platforms.

So what do you pay attention to when you're programming? I talk about some of my own experience:

A. Clear a concept, only the frame component can be displayed separately, perhaps some people will say joptionpane inside the static method to achieve a separate window appears, but the pursuit of the source code will find that the reality of the dialog also need to rely on a frame form, If it is not specified, it will be generated by default and then loaded by this dialog display.

B. JFrame is made up of a few parts:

The bottom layer of the JRootPane, above is GlassPane (a jpanel) and Layeredpane (a jlayeredpane), And Layeredpane is composed of ContentPane (a jpanel) and menubar. Our components are added to the ContentPane, and the background image can only be added to the Layeredpane. As for GlassPane is a transparent overlay of the ContentPane layer, which will be used in certain effects to record mouse coordinates or mask components.

C. In order to enhance the user experience, we will add shortcut keys on some buttons, but swing usually only recognize the keyboard Alt key, to join other shortcut keys, you must implement a ActionListener.

D. setlayout (NULL) allows all components to accurately position their size and position using the four parameters of SetBounds (), but is not recommended because a good programming style should not hardcode specific numbers in swing code. All numbers should be in the form of constants that exist in a static no-instance resource class file. This static no-instance class is responsible for the style of the swing interface, including fonts and colors that should be included.

E. Good interface design There is a golden rule: users can learn to use the software without using any manual in a few attempts. So try to put the button in the form of a menu (whether it is the right-click menu or the form comes with the top menu) to the customer, unless it is a frequent click of the button is necessary to directly present in the interface.

In fact, the function of swing is quite powerful, but now the application is not extensive, specifically to study is probably to spend a lot of time. The author in each website forum to browse about swing's skill article or is more credible, own study very limited, personal experience to swing each component's grasp is a practice accumulates the process. I have only used the above, so can only talk about some of the ideas, but also hope that you forgive me!

Java experience: Very inspiring (v)

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.