Swing Programming Overview

Source: Internet
Author: User

Swing, as an "enhanced version" of the AWT component, was created primarily to overcome the problem that the AWT-built GUI was not universally available on all platforms. Allowing programmers to specify a unified GUI display style across platforms is also the biggest advantage of swing. Swing is an addition to AWT, not a replacement.

The following are the hierarchies and inheritance relationships for swing packages.

Javax.swingclass JComponent
    • Java.lang.Object
      • Java.awt.Component
        • Java.awt.Container
          • Javax.swing.JComponent
  • All implemented Interfaces:
    ImageObserver, Menucontainer, Serializable
    Direct known subclasses:
    AbstractButton, Basicinternalframetitlepane, Box, Box.filler, JColorChooser, JComboBox, JFileChooser, JInternalFrame, Jinternalframe.jdesktopicon, JLabel, Jlayer, JLayeredPane, JList, JMenuBar, Joptionpane,jpanel, JPopupMenu, JProgressBar, JRootPane, Jscrollbar, JScrollPane, Jseparator, JSlider, JSpinner, JSplitPane, JTabbedPane, JTable, Jtableheader, JTextComponent, JToolBar, JToolTip, JTree,
    JViewport
    There are two main types of swing containers, the window and Panel,window windows are divided into two types: JFrame (Form) and JDialog (dialog), the Panel panel has two types: JPanel panel and JScrollPane (with scroll bar). JPanel must be put into a top-level container such as JFrame, which can be said to be jpanel for a more elaborate layout.

    Programmatic implementation steps for Windows:
    1. Inheriting JFrame class (JDialog Class)
    2. Instantiate a JFrame object (JDialog object) and call the Getcontentpane () method to convert the form into a container;
    3, set the parameters of the object (form size, etc.), set the closing method (if not set, the default is Windows General shutdown mode), complete the implementation of the window.

    Below, let's look at a simple example.

    public class Jlayout extends jframe{
    public void Createjframe (String title) {
    JFrame jf=new JFrame (title);//Instantiate a Form object
    Container Ct=jf.getcontentpane ();//Convert a form into a container
    JLabel jl=new JLabel ("jframe form Example");//
    Jl.sethorizontalalignment (swingconstants.center);//Set the alignment of the label text
    Ct.add (JL);//Add a label to the container
    Ct.setbackground (Color.White);//Set Background color
    Ct.setlayout (NULL);
    Jf.setvisible (TRUE);
    Jf.setsize (200, 150);//Set size
    Jf.setdefaultcloseoperation (Windowconstants.exit_on_close);
    JButton bt=new JButton ("point Me");//define a button
    Bt.sethorizontalalignment (Swingconstants.center);
    Bt.setbounds (10, 10, 100, 21);
    Bt.addactionlistener (new ActionListener () {//(new Actionlinstener () {
    public void actionperformed (ActionEvent e) {
    New Jd (Jlayout.this). setvisible (True);
    }
    });
    Ct.add (BT);
    }

    Class Jd extends jdialog{
    Public Jd (Jlayout frame) {
    Super (Frame, "dialog box", true);
    Container Ct=getcontentpane ();
    Ct.add (New JLabel ("Let you order It"));
    SetBounds (120,120,100,100);
    }
    }

    public static void Main (String args[]) {
    New Jlayout (). Createjframe ("");
    }

    }

Swing Programming Overview

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.