Add and set JPanel Method Instance parsing in JFrame, jframejpanel

Source: Internet
Author: User

Add and set JPanel Method Instance parsing in JFrame, jframejpanel

Swing programs use JFrame objects to implement their windows. The JFrame class is a subclass of the AWT Frame class. It also adds some unique features of Swing. Similar to Frame. The only difference is that you cannot add components to JFrame. You can either add the component to the content pane (content panel) of the JFrame, or provide a new content pane (content panel ).

Differences between a panel and a top-layer container: the Panel cannot exist independently and must be added to another container (the Panel can be nested ).

JFrame has a Content Pane. All components displayed in the window are added to this Content Pane. JFrame provides two methods: getContentPane and setContentPane are used to obtain and set its Content Pane.

You can add components to a JFrame in either of the following ways:

1) Use the getContentPane () method to obtain the JFrame content panel, and then add the component frame. getContentPane (). add (childComponent) to it)

2) create an intermediate container such as Jpanel or JDesktopPane, add the component to the container, and set the container to the JFrame content panel using setContentPane:

JPanel contentPane = new JPanel ();...... // Add other components to the Jpanel; frame. setContentPane (contentPane); // set the contentPane object to the content Panel of the frame.

Instance program:

Import java. awt. *; import javax. swing. *; public class JFrameWithPanel {public static void main (String [] args) {JFrame frame = new JFrame ("Frame With Panel"); Container contentPane = frame. getContentPane (); contentPane. setBackground (Color. CYAN); // set the JFrame instance background to blue-green JPanel = new JPanel (); // create a JPanel instance panel. setBackground (Color. yellow); // set the JPanel instance background to yellow JButton button = new JButton ("Press me"); panel. add (button); // add the JButton instance to the JPanel contentPane. add (panel, BorderLayout. SOUTH); // Add the JPanel instance to the SOUTH frame of the JFrame. setSize (300,200); frame. setVisible (true );}}

:

Summary

The above is all the content about how to add and set JPanel Method Instance parsing in JFrame. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.