Java Swing components and event handling

Source: Internet
Author: User

1, the common container

JComponent is a subclass of Container, intermediate containers must be added to the underlying container in order to function,

JPanel panel: Use JPanel to create a panel, and then add components to the panel, JPanel the default layout is FlowLayout

Jscrollpanel slide pane: The slide pane can only add one component, you can put a component in the sliding pane, scroll bar view, JTextArea without scroll bar can be placed in Jscrollpanel.

Jscorollpanel scroll=new Jscorollpanel (New JTextArea ());

Jsplitpanel split panes: Two types, horizontal and vertical. Jsplitpanel (int a,component B, Component c);

Jlayerpanel Hierarchy pane: Add (jcomponent com,int layer)

2. Common layout

The object created by the Flowlayout:flowlayout class is called the FlowLayout layout, and the FlowLayout layout is the default layout of the JPanel container.

FlowLayout flow=new FlowLayout (); If a container uses this layout, con.setlayout (flow) con can use the Add method provided by the container class to add the build order to the container, and the form is left to right in order of accession,

Borderlayout:borderlayout is the default layout for Windows, Jframe, JDialog are all Windows default subclasses, and the panel is divided into east by default. Add Method Con.add (B,borderlayout.center)

The Cardlayout:cardlayout layout can hold multiple components, the components used are stacked into the container, only one component can be displayed at a time, and the displayed component occupies the entire space of the container.

CardLayout card=new cardlayout ();

Con.setlayout (Card).

The container calls Add (String s,component b) where S is the display designator of the component. The layout card uses the method provided by the CardLayout class Show () method to display a con component, such as a component that displays the component designator s in the container con.

Card.show (CON.S)

The Gridlayout:gridlayout layout divides the panel into multiple meshes, and the components are added to the grid. GridLayout layout is more flexible. Use the GridLayout layout method. First divide the grid GridLayout (int m,int N)

GridLayout grid=new GridLayout (10,2); The second add component to the layout, add (Component C) adds component C to the container, and the component adds the straight start from the first row, one at a time.

Java Swing components and event handling

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.