Java Swing Component Full Demonstration

zhaozj2021-02-11  248

Java Swing Component Full Demonstration

Author: Li Tianyi (Skyala.Li) robocode@etang.com Robocode China Union www.robochina.org.

Content: Throwing jade feeling effect design ideas and method containers, layout manager, panel event model summary specific code Reference information About the author 2003 One Throwing brick 引 玉 This article refers to the use of a small program "Swing component Daquan" to guide you to learn Java this Door language, familiar with each component of Java Swing. And in the process of studying the procedures and ideas of the author designed this procedure, finally forming their own thinking and learning methods. You can give a back and trip to the author's happiest thing. The applet demonstrates most of the Java Swing. Finally, all code and detailed notes are attached. I hope this article can help you learn Java. As the Java trend is sweeping, learning Java's approach and path is more and more. In front of a large number of books, everyone a little lost direction before Java, is learning J2EE, J2ME or. . . . . . However, all of these are more or less inseparable from J2SE. Wanzhang high rise. J2se is the foundation of the entire Java, learning this is very beneficial to all directions of Java. And the initial point of learning J2SE is designed in the interface, because interface programming is your most direct means, it is a good start to improve your programming, because you can see your results immediately after writing the program. And don't feel the effects like background programming. The good interface is also the basis for you to test your other procedures. interface design! Is it a drag operation? If it is VB, VC, etc., it is only some of the drag operation. Because all things in the Java language design are program code, if you want to design a good interface in Java, it is not a simple drag operation. of. Even if you use such a strong visualization tool such as JBuilder, you have to write some interface design code to complete your task very well. If you can understand the idea of ​​Java interface design, you will have a great help to you learn this language. Ok, I said that such a long nonsense, let's take a look at the foundation of Java interface design. Java interface design mainly involves both AWT, Swing two categories, Swing is AWT extension and functional strengthening. Below we are based on Swing, the entire Java interface design. The second feeling effect is before the beginning, let us see the effect of this article, there is a learning interest in learning, at least I think so, thinking and learning. As shown in the figure below, this is the interface we design, which includes most common components in Swing. Some function interfaces can not be reflected, you can try to build the source code point to attack each part and see their response events. Three design thoughts and methods don't know if you (generally beginners) have this experience when watching programming books: During the process of watching, it seems that most of them understand, and the efficiency is not bad, but it will write it yourself. The same procedure as the book, maybe many people only have to take the book; some people have played over the photo, and it seems to understand. When you go back, you don't remember it. Author I have experienced many times when I start school. Chasing the reason, there is no more than the knowledge you have learned, the programs are the most prominent, and the true meaning of the handshake is to write the procedures you want to understand rather than COPY. Because only, it is thinking, design can really solve the knowledge on your own brain. Below we talk about this method and ideas with the "Swing Components". After reading the Swing Components introduced in the book, I had a understanding of Swing in my heart, only a lot of hand. And I don't want to take a photo on the book. So I thought of making a program put all the components described in the book in an interface and tried to use all the methods described in the book.

Cut the firewood does not grind the knife! Before starting, I hand draw a design drawing, as shown above (of course, is not so beautiful). Simply divided by simple software engineering ideas. Here, the entire interface is divided into a master module and a five major child module. All sub-modules are inherited to the corresponding container, and the main module SWINGTEST is also the management module is also the main interface, inheriting the JFrame main frame, the property itself, and other submodules on the JFrame. The Menutest submodule inherits the display and event processing of the JMenubar responsible for menus. Other submodules are inherited to JPANEL, manage layouts and display of components placed thereon. You can also see their location and function from the name of the submodule. As shown in the figure below: It is a good habit of thinking about the structure and ideas before writing programs. It will help you reduce your workload, improve program readability and efficiency. All components in four containers, layout managers, and panel swing are placed in the container, and the main containers include: JFRAME, JPANEL, JWINDOW, JDIALOG, JPANLE. JFrame is the main frame of Java, almost all Java application interfaces are designed in the main framework. Some containers are not a layer, composed of several layers of panels, which will be used herein in one: splitting panel (JSPLITPANE), content panel, tabbedpane split panel: press specified The direction and mode split two subcomponents, remember that a split palace can only be split two subcomponents, if you want to split, everyone wants to find a way. This section finally gives an answer. Content panel: JFRAME, JWINDOW, JDIALOG is a multi-layered container mentioned above, and if you want to place components on these containers, you must be placed on their content panel (PANE). We get the content panel of the current container through the function getContentPane (). Pagemapped panel: This palace is like a card, which provides a folder tab on it, and only one page is currently displayed. To go to the next page, just click on the tab. Some JPANEL is a container that can be placed directly. So don't need to use the panel. If you are arranged in your own ideas, we must use the Layout Manager (Layout) to manage. The Default Layout Manager of the Java System is Border Manter (BorderLayout). It divides the layout into five major blocks, central region (center), bottom (South), left side (West), right side (EAST), careful you may find this of our main interface Layout architecture. Other several layout managers are: FlowLayout: Press the components from left to right and arranged from top to bottom. GridLayout: Place the component into each grid of the settings in the layout (GridbagLayout): Similar to the grid layout, but more powerful is more complicated. Can handle all layouts. If you carefully observe will find the rightmost right side of our main interface to find the grid layout. Let's take a look at our main module constructor code to feel the above structure design.

public SwingTest () {// Initialize all submodules MenuTest menuTest = new MenuTest (); LeftPanel leftPanel = new LeftPanel (); RightPanel rightPanel = new RightPanel (); BottomPanel bottomPanel = new BottomPanel (); CenterPanel centerPanel = new CenterPanel () ; // Set the layout of the main frame Container C = this.getContentPane (); // c.setLayout (new borderLayout () this.setjmenubar (menutest); C.Add (LeftPanel, BorderLayout.west); C.Add ( RightPanel, BorderLayout.east); C.Add (CenterPanel, BorderLayout.Center); C.Add (Bottompanel, BorderLayout.South); Setsize (700, 500); SetTitle ("Swing Components Master Simplified Edition"); // Hide Frame The title bar, this function is temporarily closed to facilitate the use of Window Event // Setundecoated (True); SETLOCATION (200, 150); show ();} If you split three subcomponents, you can make another split panel as another split Subcaps of the panel ! Five Event Models You can trigger events in Swing, and once the trigger event is received by one or more Listener (listener). Different types of events are represented by individual CLASS. The event occurrence point can be separated from the code of the handling event. To handle events, just generate a listner object, and call the component's AddXXXListener () method registration. We mostly handle events by inheriting event interfaces. But inherited the Java interface We must implement all the methods in the interface. Some interfaces contain a large number of functions. If you want to implement a very troublesome event, the Adapter assigner class of the corresponding interface is defined in Java to solve this situation. The connector class has helped us implement all the methods of the interface, and we can do what we want to do in the code within the code as long as we inherit the connectors. That is, as long as you implement or override the way we want to implement. Let's take the main module as an example to process the closing window event.

// Use the unnamed intimacity, add window event this.addwindowlistener (new windowadapter () {// release resource, exit program dispose (); system.exit (0);}}); As shown above, we registered a window event to the main framework addWindowListener (), and the event inherits the WindowAdapter connector and overloads the method WindowClosing (), using Dispose () release window resources, system.exit (0) shut down window. Six summary, let's review a content of our above: Designing ideas, containers, panels, layout managers, and Swing components. Everyone understands why the project can quickly improve his level. In fact, the principles of the above method are almost the same, and anything will take care of their own cerebelings, hand it, I want to forget is not so easy. I believe this article and the code re-adjusted book introduction, everyone has a certain sense of sensibility to Swing. Start your hand. Play your genius thinking, design a better Swing component. Of course, you can also improve the more complex features on this process. For example, use the tree list content instead of the Window file resource, connect the table's processing and database or related data connections, allowing the text area to display pictures. . . . . Oh my god. It will be born with a good product. Once again, please design your interface before you start, and draw your program map. Even if you do a small program, you can also hook a contour. Seven specific code has been attached in detail due to the code in the code, there is nothing explained herein. To remind everyone that you should pay attention to the processing of several complex components when studying the source code: such as the tree component (JTREE), the JTABLE, the text area (JTextArea), Timer. In order to facilitate everyone to copy the source code directly, all modules are put in a primary class. All source code events are implemented with nested internal hidden methods. If you are not used to this writing, the relevance between the modules has been eliminated in the program, which can easily separate each sub-module into class files. The code only demonstrates most components of Swing, and each component has not been deepened. I believe that this part of the foundation, everyone can be deepened on this basis, you can download all source code here. All rights reserved, please indicate the source

转载请注明原文地址:https://www.9cbs.com/read-4281.html

New Post(0)