Teach you how to use Java Swing achieve game developers
Abstract: teach you how to use Java Swing achieve game developers
<tr> <td> Java cafe has opened a lot of time, if you are already fond of the Java cup of coffee flavor, often to remember oh. This time, we all prepared for a large cup aromatic coffee - will be the development of a "Lianliankan" modelled on the game, and we learn together in Java Swing usage, when you taste carefully cup of coffee, you will found that not only Java cup of coffee also have some other flavor, but also learned the professional game development, is the best of both worlds!
In order to let everyone see in advance the facilities, the chart is the title game. Members can download the game demo (download games; download source files), then use the command line mode java-jar kyodai.jar to run. In addition, I can go to the Home http://www.ismyway.com download stand-alone version of the game as well as a demo version of mobile phones (see Figure 1).
Java cafe previously introduced AWT knowledge, Swing and AWT then what is the difference? Learning Java people may have heard or seen a heavyweight controls and controls lightweight word AWT is that we usually referred to the heavyweight controls, it is lightweight Swing controls.
We all know that Java is the slogan of "one preparation, running everywhere," which will require our procedures, to make the best use of pure Java code. Unfortunately, AWT rely on the local interface platform, therefore, in different operating systems, making use of AWT interface may seem there are some subtle differences.
Swing is completely different, using pure Java Swing is prepared, therefore, prepared by the use of Swing interface can be guaranteed in all platforms have the same appearance. There is also a Tips: JDK, in order to facilitate the distinction between all Swing controls all capital letters J to the beginning, for instance, JButton (AWT in the corresponding Button is), so you can easily distinguish between AWT Swing controls and controls the.
Swing early experience
For those who want to learn Swing programming friends, we are all specially prepared for some tips. First, download and read the code is very necessary. Since this is a tutorial on the Swing, therefore, we are just as much as possible on some of the contents of the Swing, Swing has nothing to do with the content of the general will not be involved, for example, some algorithm.
Secondly, to space restrictions, it will not be possible here will be part of the code is written in every End integrity of the whole, therefore, we also need to control integrity of the code. Finally, in order to make it easier for you to concentrate on Swing learning, we will also required in game development resources on the downloaded file, you can download after compiler operation, to see the results. (Download the game source files)
1, the top containers
What is the top containers? When we use graphical programming Java, where plans drawn? We need to provide a graphic containers, the containers are called top-level containers, and you can imagine it as a window. Top container is the foundation for graphical programming, and all graphical things will inevitably included in the top container. In Swing, we have the top three can be used in containers, they are:
JFrame: similar to the Windows operating system used to design window in the form of applications.
JDialog: JFrame and similar, but JDialog is used to design the dialog box.
JApplet: design can be used in embedded in the Web page Java applets.
If you need to use a window of Swing production processes, we should be the code looks like this:
2. Control
Control is an Application Program Interface of the basic elements, buttons, text boxes, such as the progress of these controls are. Controls (here we only discuss visualization Control) containers can be divided into control and non-control containers. From the literal meaning of understanding, containers can contain other controls that controls the special controls, for example, in Java JPanel controls on the type of vessel control, we can JPanel placed buttons, text boxes, and other non-container control , you can even further in the JPanel placed several JPanel Control (It is worth noting that the top container-type containers also controls every window in the application and there can be only one top containers controls, in other words , the top containers could not be included in the other controls).
Java containers in a lot of controls, in addition to the JPanel just mentioned, there are JTabbedPane, JScrollPane, a non-container control JButton, JLabel, such as JTextField. If you need to control a certain type of containers to add controls, you can use the add (Component comp) method to achieve, such as:
3. Layout
What is the layout? Java is the layout of controls are used to control the location of an interface management system. The use of other visual programming language Java in the initial contact interface design, always feel very uncomfortable Java interface design: there were no controls to provide WYSIWYG settings coordinate method! However, facts have proved that the layout of Java itself provide the same management system can be successfully fulfilled our needs, but also in cross-platform performance when there are more advantages.
The layout of commonly used are:
BorderLayout: interface will be divided into upper and lower middle, as well as about a regional management system, in BorderLayout layout, the most you can only put five controls, if more than five controls, the proposed distribution or use other systems it.
GridLayout: GridLayout user interface is the same cutting board for the distribution management system. If we are to design a similar built-in calculator in Windows software, GridLayout is undoubtedly the best option.
FlowLayout: FlowLayout layout with the above-mentioned two types of management systems do not, as in FlowLayout, you do not have to specify on which each control, you need only to control into FlowLayout, FlowLayout will, in accordance with you to add controls Controls placed the order, if the space is insufficient, will be automatically newline.
In the layout of this management system has several basic understanding, we have to design an interface to enter it. QQ in the careful observation of the game "Lianliankan" settings, we can be found, the entire interface is divided into three zones, is the top menu system, the area is the largest user games, in addition to a user Interactive areas, in each region by the number of control components.
So many controls, where we start with the beginning? The containers can be placed in the control of other controls, therefore, we have only to make sure the containers placed controls on it.
Since it has been aware of the need to control the number of containers used, let us enter the next distribution management system. With GridLayout? Seems to be some reluctance by FlowLayout? There are better options? Right, I think you have thought of is BorderLayout bar, as shown in Figure 2.
Before hands, we must pay attention to is that the interface design must consider good size, is the main program interface regardless of the size or the size of each region, in the absence of good design appropriate size, with future changes will be very painful. Below is the corresponding source:
Let us look at the above is how this process operates. First of all, this extends JFrame that is inherited from the JFrame, JFrame top container is the most basic controls. In fact, in the JDK, beginning with the letter J controls are Swing controls.
And then set up a container attributes, including setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) is used to tell the Java Virtual Machine, when users click on the upper right corner of the form "off" button, the process of closing the window. If we do not do so, you will find that although you can be dispatched window closed, but did not withdraw from the process.
In the next code, we added three top containers Panel containers. It should be noted that in the AWT, we can write directly to add (toolBar, BorderLayout.NORTH), and it must be in the Swing wrote getContentPane (). Add (toolBar, BorderLayout.NORTH), otherwise the process will be wrong.
Now we can look at the operation on the compiler, and I was not the same as the operation (see figure 3)
4. Borders
Although we use different Jianjingse to distinguish between different regions, but did not layered with beautiful borders will be many.
In Java, all J beginning of the Swing controls can be used for setBorder ways to set up their own borders. There are many types of borders, linear, convex, Waxia, air, and you can even free combination formed personal style. All Border javax.swing.BorderFactory must be provided in the use of the static method to create, for example:
Now, we will toolBar.setBackground (Color.white) to toolBar.setBorder (border), the three-dimensional effect is not there have been?
Actual combat - write their own name
Now we have a functioning interface, although it do anything, but please Biehuang, Rome was not built in one day.
Let us now in the area to provide a menu "on the" menu, the procedures used to display information, can you do not want to let others know your name? Swing itself provides a ready JButton button control, we only need to create a new button: JButton about = new JButton ( ""); how this button into the menu area and not elsewhere?
We can join the following code: toolBar.add (about); Yi, how push of a button does not respond? This is because you have not told the procedures to be done at the click of a button is what happened. Add the button to respond to the incident, first of all, need to use about.addActionListener (this) to tell procedures eavesdropping button pressed the incident, due ActionListener is a procedural interface Therefore, we stated in the category have to do a bit of local small Laws: public class KyodaiUI extends JFrame implements ActionListener {…} achieve ActionListener interface to tell the procedures I have to carry out the deal with the incident. Of course, we must add the final response to the incident code:
Among them, e.getSource () that controls the current trigger events, as we often have procedures in a number of the above controls, these controls are likely to produce the event, we must use this method to find an issues Control.
Summary
Let us together look at the content of today: First, we know the top-level containers, know that the control is divided into control and non-container vessel control, but also noted that the use of frames, finally, we also deal with minor a touch of a button events.
When learning of the school and not between also said that, let me leave small operations to help consolidate what we have learned today: Add button above us in the middle of the menu bar is not handsome, please put Try the right or left.
Finally to provide Members with some good references:
â— Creating a GUI with JFC / Swing
Http://java.sun.com/docs/books/tutorial/uiswing/index.html
â— 2 D Graphics
Http://java.sun.com/docs/books/tutorial/2d/index.html
â— JDK API
Http://java.sun.com/j2se/1.4.2/docs/api/index.html
↑ Back
Tags: java game, java swing






