Java (6)-Swing programming (I), advanced swing

Source: Internet
Author: User

Java (6)-Swing programming (I), advanced swing

Swing is a GUI (graphical user interface) Development Kit with many contents. It will be written in multiple parts here. However, in the advanced article, only the basic elements of Swing, including containers, components, and layout, are written, more in-depth content will appear in the advanced article. Friends who want to study in depth can access relevant materials or books, suchJava Swing graphic interface development and case description-Tsinghua University Press.

The graphical user interface developed by the early AWT (Abstract Window Toolkit) component depends on the local system. When the application developed by the AWT component is transplanted to the system of other platforms for running, the appearance style cannot be guaranteed, so AWT depends on the local system platform. The interface of Java applications developed using Swing is not restricted by the local system platform. That is to say, when Java applications developed by Swing are transplanted to other system platforms, the appearance of the interface will not change. However, although the components provided by Swing can be used to develop Java applications, Swing cannot replace AWT, when developing Swing programs, you often need to design applications together with some AWT objects.

I. Common forms

A Swing form is a component of Swing and a container for creating a graphical user interface. Other components can be placed in a form container.

1. JFrame frame form

A JFrame form is a container that is often used in Swing development. It is the carrier of each component in a Swing program. The syntax format is as follows:

JFrame jf = new JFrame(title);

Of course, the more common method in development is to create a form by inheriting the java. swing. JFrame class. You can use the this keyword to call its method.

After the JFrame object is created, call the getContentPane () method to convert the form to a container, and then add components to the container or set the layout manager. Generally, this container is used to include and display components. To add a component to a Container, you can use the add () method of the Container class to set the component. The JPanel container will be mentioned later.

The following is an example of a JFrame form.

1 import javax. swing. JFrame; 2 import javax. swing. windowConstants; 3 4 public class JFrameDemo {5 6 public void CreateJFrame () {7 JFrame jf = new JFrame ("This Is A JFrame form"); // instantiate a JFrame object 8 jf. setVisible (true); // set the form to be visible 9 jf. setSize (500,350); // set the size of the form to 10 jf. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); // set the form close mode 11} 12 13 public static void main (String [] args) {14 new JFrameDemo (). createJFrame (); // call the CreateJFrame () method 15} 16 17}

The running result is as follows:

  

This is a 500*350 form, which uses the setSize () method; the title is "This Is A JFrame form", which can be defined when the object is instantiated; for how to close a form, see "EXIT_ON_CLOSE" in the upper-right corner of the Form. When the parameter in the form visible setVisible () method is "false" or the setVisible () method is not written, this form is invisible.

There are four common forms to close: "DO_NOTHING_ON_CLOSE", "DISPOSE_ON_CLOSE", "HIDE_ON_CLOSE", and "EXIT_ON_CLOSE ". The first indicates that the form is closed when nothing is done. The second indicates that the form is automatically hidden and released after any listener object is registered. The third indicates that the default window of the hidden window is closed; the fourth option indicates that the default window for exiting the application is closed.

Next, let's take a look at the specific process by writing code that inherits the JFrame and adding the iner Container and the JLabel label (which will be mentioned later.

1 import java. awt. color; 2 import java. awt. container; 3 4 import javax. swing. JFrame; 5 import javax. swing. JLabel; 6 import javax. swing. swingConstants; 7 import javax. swing. windowConstants; 8 9 public class JFrameDemo2 extends JFrame {10 11 public void init () {12 this. setVisible (true); // visualize 13 this. setSize (500,350); // size 14 this. setTitle ("blog"); // Title 15 this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); // close mode 16 17 JLabel jl = new JLabel ("http://www.cnblogs.com/adamjwh/"); // create a JLabel label Label 18 jl. setHorizontalAlignment (SwingConstants. CENTER); // CENTER the label text 19 20 Container container = this. getContentPane (); // get a container 21 iner. add (jl); // add the tag to container 22 container. setBackground (Color. YELLOW); // set the container background color 23} 24 25 public static void main (String [] args) {26 new JFrameDemo2 (). init (); 27} 28 29}

The running result is as follows:

  

The JFrame class is inherited here, so you can use the this keyword to implement the method (or directly implement it without adding this ). 12 ~ 15. Create a JFrame frame. 17. 18. Use and add a JLabel label. 20 ~ 22. Act as the Container. Use the getContentPane () method to obtain the Container for the JFrame form and add the JLabel label to the Container using the add () method.

2. JDialog form

The JDialog form is a Dialog box in the Swing component and inherits the java. AWT. Dialog class in the awt component. Function is to pop up another form from one form.

Here is an example:

1 import java. awt. container; 2 import java. awt. event. actionEvent; 3 import java. awt. event. actionListener; 4 5 import javax. swing. JButton; 6 import javax. swing. JDialog; 7 import javax. swing. JFrame; 8 import javax. swing. JLabel; 9 import javax. swing. windowConstants; 10 11 public class JDialogDemo extends JDialog {// inherits JDialog class 12 13 public JDialogDemo () {14 super (new MyJFrame (), "This is a JDialog form", true ); // instantiate a JDialog Class Object, specifying its parent form, window title, and type 15 Container container = this. getContentPane (); 16 container. add (new JLabel ("http://www.cnblogs.com/adamjwh/"); 17 this. setSize (500,350); 18} 19 20 21 public static void main (String [] args) {22 new JDialogDemo (); 23} 24 25} 26 27 // The following content contains the listener, which will introduce 28 class MyJFrame extends JFrame {29 public MyJFrame () {30 this. setVisible (true); 31 this. setSize (700,500); 32 this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); 33 34 Container container = this. getContentPane (); 35 container. setLayout (null); 36 37 JButton jb = new JButton ("click the pop-up dialog box"); // create button 38 jb. setBounds (30, 30,200, 50); // The button position and size 39 jb. addActionListener (new ActionListener () {// listener, used to listen to the Click Event 40 @ Override41 public void actionreceivmed (ActionEvent e) {42 new JDialogDemo (). setVisible (true); 43} 44}); 45 container. add (jb); 46} 47}

The running result is as follows:

  

When we click the button, the click event is triggered. At this time, execute the 42nd line statement to create a JDialog instantiated object. A window is displayed. Here we have learned a lot, such as the super keyword. previously mentioned, JDialog (Frame f, String title, boolean model) constructor is used; listener implementation is an anonymous internal class, which has been mentioned before. The content of JButton, JLable, and listener will be introduced later.

Ii. Tag Components

Labels are used to display text or prompt information in Swing. They support text strings and icons. The JLabel we mentioned above is the content here.

1. Tag

A label is defined by the JLabel class and can display a line of read-only text, an image, or text with an image.

The JLabel class provides many constructor methods for you to select APIs, such as displaying text-only labels, icon-only labels, and labels containing text and icons. Because the above has already appeared, I will not give an example here. The common syntax format is as follows. A JLabel object without icons and text is created:

JLabel jl = new JLabel();
2. icon

Icons in Swing can be placed on buttons, labels, and other components to describe the purpose of the component. Icons can be created using the image file types supported by Java, or using the functional methods provided by the java. awt. Graphics class.

You can use the Icon interface in Swing to create an Icon. You can specify the Icon size, color, and other features when creating the Icon. Note that the Icon is an interface. When using the Icon interface, you must implement three methods of the Icon interface:

public int getIconHeight()public int getIconWidth()public void paintIcon(Component arg0, Graphics arg1, int arg2, int arg3)

The first two methods are used to obtain the length and width of an image. The paintIcon () method is used to draw a picture at a specified coordinate position.

The following shows an example of creating an Icon using the Icon interface:

1 import java. awt. component; 2 import java. awt. container; 3 import java. awt. graphics; 4 5 import javax. swing. icon; 6 import javax. swing. JFrame; 7 import javax. swing. JLabel; 8 import javax. swing. swingConstants; 9 import javax. swing. windowConstants; 10 11 public class IconDemo extends JFrame implements Icon {12 13 private int width; // declare the width of the Icon 14 private int height; // declare the length of the icon 15 16 public IconDemo () {}// define the Construction Method 17 18 public IconDemo (int width, int height) {// define the construction method with parameters 19 this. width = width; 20 this. height = height; 21} 22 23 @ Override24 public int getIconHeight () {// implement the getIconHeight () method 25 return this. height; 26} 27 28 @ Override29 public int getIconWidth () {// implement the getIconWidth () method 30 return this. width; 31} 32 33 @ Override34 public void paintIcon (Component arg0, Graphics arg1, int arg2, int arg3) {// implement paintIcon () method 35 arg1.fillOval (arg2, arg3, width, height); // draw a Circular 36} 37 38 public void init () {// define a method to implement interface 39 IconDemo iconDemo = new IconDemo (15, 15 ); // define the length and width of the icon. JLabel jb = new JLabel ("icon test", iconDemo, SwingConstants. CENTER); // set the text on the tag to 41 42 Container container = getContentPane (); 43 container. add (jb); 44 45 this. setVisible (true); 46 this. setSize (500,350); 47 this. setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); 48} 49 50 public static void main (String [] args) {51 new IconDemo (). init (); 52} 53 54}

The running result is as follows:

  

In this way, if you need to use the icon in the form, you can use the following code in line 39th to create the icon:

IconDemo iconDemo = new IconDemo(15, 15);
3. Image icons

Icons in Swing can be created by using a specific image. Use the javax. swing. ImageIcon class to Create Icons based on existing images.

Let's take a look at an example. We first place an image in the package (note the placement location, different paths), as shown below:

The following is the implementation code:

1 import java. awt. container; 2 import java.net. URL; 3 4 import javax. swing. icon; 5 import javax. swing. imageIcon; 6 import javax. swing. JFrame; 7 import javax. swing. JLabel; 8 import javax. swing. swingConstants; 9 import javax. swing. windowConstants; 10 11 public class ImageIconDemo extends JFrame {12 13 public ImageIconDemo () {14 JLabel jl = new JLabel ("This Is A JFrame form with an image next to it "); 15 URL url = ImageIconDemo. class. getResource ("pencil.jpg"); // obtain the URL16 Icon = new ImageIcon (url) of the image; // instantiate the icon object 17 jl. setIcon (icon); // set the image 18 jl for the label. setHorizontalAlignment (SwingConstants. CENTER); 19 jl. setOpaque (true); // set the label to Opacity status 20 21 Container container = getContentPane (); 22 container. add (jl); 23 24 setVisible (true); 25 setdefaclocloseoperation (WindowConstants. EXIT_ON_CLOSE); 26 setSize (500,350); 27} 28 29 public static void main (String [] args) {30 new ImageIconDemo (); 31} 32 33}

The running result is as follows:

  

For image labels, we often place images on labels and use the setIcon () method in JLabel. Of course, you can also specify the icon for the label when initializing the JLabel object, this is to obtain an Icon instance.

The getResource () method of Row 3 can obtain the URL path of the resource file. The path here is relative to the previous class, so you can put the image and the class in the same folder; if the directory is not in the same folder, you must use other methods to obtain the path.

 

This article first introduces the content here, so it is divided into multiple articles. The next article is commonly used layout manager, Jpanel, and other content.

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.