Java GUI Java Panel Basics: JPanel

Source: Internet
Author: User

There are two types of panels, normal panel (JPanel) and scroll panel (jscrollpane)

Jpanel

A panel is a general-purpose container in which the JPanel function is to implement the hierarchical structure of the interface, put some components on it, or paint on it, and put the components and the painted JPanel into another container. The default layout for JPanel is FlowLayout.

The basic content of the panel handler has the following aspects:
① the subclasses of the JPanel class through inheritance, there are components in the subclass, and the components are added to the panel in the constructor method
② declaring JPanel sub-class objects
③ creating a JPanel Subclass Object
④ adding a JPanel subclass object to a container

The common construction methods of the JPanel class are

JPanel();//创建一个JPanel对象JPanel(LayoutManager layout);//创建Jpanel对象时指定布局layout

JPanel How to add components to an object

add(组件);//添加组件add(字符串,组件);//当面板采用GardLayout布局时,字符串是引用添加组件的代号
//Small application has two JPanel subclass objects and a button. Each JPanel subclass object has two buttons and a labelImport Java.applet.*;import javax.swing.*; class mypanel extends JPanel {JButton Button1,button2;    JLabel Label; Mypanel (String s1,string s2,string s3) {button1 =NewJButton (S1); Button2 =NewJButton (S2); Label =NewJLabel (S3);        Add (button1);        Add (button2);    Add (Label); }} Public  class Test extends Applet {Mypanel Panel1,panel2; JButton Button; Public voidInit () {Panel1 =NewMypanel ("OK","Cancel","label, we're in Panel 1"); Panel2 =NewMypanel ("OK","Cancel","label, we're in Panel 2"); Button =NewJButton ("I am not a button in the Panel");        Add (Panel1); add (Panel2); add (Button); SetSize ( -, $); }}

Java GUI Java Panel Basics: JPanel

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.