Java-Foundation (10) Swing

Source: Internet
Author: User
Tags gettext

When I saw the applets and swing, I thought of WinForm, and all the so-called components in the Java beans package, all so familiar.

Swing is an extension of AWT, which provides a more powerful and flexible collection of components. In addition to the components we already know, such as buttons, checkboxes, and labels, swing also includes many new components, such as palettes, scrolling windows, trees, and tables. Many of the components that developers are already familiar with, such as buttons, add new functionality to swing. Also, the button's icon can change when the button's state changes. Unlike AWT components, the swing component implementation does not include any platform-related code. Swing components are pure Java code and therefore platform agnostic. This type of component is generally described in the term lightweight (lightweight). The number of classes and interfaces in the swing package is numerous, and this chapter briefly describes only a subset of them. The swing package is a part of the developer's desire to study carefully.

Packages to be introduced when using swing:

Import java.awt.*;

Import java.awt.event.*;

Import javax.swing.*;

Some common controls and the corresponding events:

1) content pane:

Get content Space Container Getcontentpane () Container ContentPane = Getcontentpane ();

The Add () method of the container adds a component Add (comp) Contentpane.add (JL) to the contents pane;

2) JTextComponent class, JTextComponent class is a subclass of JComponent

3) Jbuttom class, AbstractButton class extension JComponent class. The AbstractButton class contains several methods for controlling button behavior, checking check boxes, and radio buttons

You can read and write button-related text in the following ways: String GetText () void SetText (string s)

AbstractButton the subclass of an abstract class generates a behavior event when the button is pressed.

Register and unregister listeners for these events by using the following methods: void addActionListener (ActionListener al) void Removeactionlistener (ActionListener al)

4) Jcheckbox class provides check box function

When the check box is selected or canceled, a project event is generated. This event is handled by Itemstatechanged (). Inside Itemstatechanged (), the GetItem () method gets the Jcheckbox object that generated the event. The GetText () method gets the text of the check box and sets the text field with the text.

Cb.additemlistener (this); Increase Event

Processing Manager:

public void itemstatechanged (ItemEvent IE)

{Jcheckbox cb = (jcheckbox) ie.getitem (); Jtf.settext (Cb.gettext ());}

5) radio buttons are supported by the Jradiobutton class, and Jradiobutton is also a subclass of the AbstractButton abstract class.

The radio button presses the resulting behavior event, which is handled by actionperformed (). The Getactioncommand () method gets the text associated with the radio button and sets the text field with this text.

B2.addactionlistener (this); Increase Event

public void actionperformed (ActionEvent ae) {tf.settext (Ae.getactioncommand ());} Handling Events

6) Swing supports combo boxes through the JComboBox Class (combo box, a combination of text fields and drop-down lists), and the JComboBox class is a subclass of JComponent. A combo box typically displays an optional entry, but allows the user to select several different entries in a 628 Part 3 Java Software development Technology drop-down list. Users can also type selections within a text field

Import java.awt.*;import java.awt.event.*;import javax.swing.*;/* <applet code= "Jcomboboxdemo" width=300 height= 100> </applet>*/public class Jcomboboxdemo extends jappletimplements itemlistener {JLabel jl; ImageIcon France, Germany, Italy, Japan; public void init () {//Get content pane Container ContentPane = Getcontentpane (); Contentpane.setlayout (new FlowLayout ()) ; Create a combo box and add it//to the panel JComboBox JC = new JComboBox (); Jc.additem ("France"); Jc.additem ("Germany"); Jc.additem ("Italy"); Jc.additem ("Japan"); Jc. Additemlistener itemstatechanged (ItemEvent IE) {String s = (string) ie.getitem (); Jl.seticon (new ImageIcon (S + ". gif"));  

7) Options Pane

The options pane is encapsulated as a JTabbedPane class, and JTabbedPane is a subclass of JComponent. When using the default constructor, the options are defined as follows: void AddTab (String str, Component comp) where Str is the title of the label, comp is the component that should be added to the label. Typically, a jpanel or its subclasses are added. The general procedure for using the Options pane in a small application is as follows: 1. Creates a JTabbedPane object. 2. Call the AddTab () method to add a label to the pane (the parameter of this method is the caption of the label and the component it contains). 3. Repeat step 2 to increase the label. 4. Add the options pane to the content pane of the applet.

8) JScrollPane Class Flow pane

9) JTree tree-shaped controls

When a node expands or shrinks, the JTree object generates events. The Addtreeexpansionlistener () and Removetreeexpansionlistener () methods register or unregister listeners that listen for these notifications.

() Jtable table-type controls

Java-Foundation (10) Swing

Related Article

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.