Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the JavaSwingJPanel panel

Shulou Source: shulou.com Published: 2022-06-03 11:00:11 10月05日 Update

This article is about how to use the JavaSwingJPanel panel. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Overview

Official JavaDocsApi: javax.swing.JPanel

JPanel, panel. JPanel is a general lightweight panel container component that is frequently used in development.

Common construction methods of JPanel:

/ / create a panel JPanel () that uses streaming layout by default / / create a panel JPanel (LayoutManager layout) that specifies the layout manager

two。 Code example

Package com.xiets.swing;import javax.swing.*;import java.awt.*;public class Main {public static void main (String [] args) {JFrame jf = new JFrame ("user login"); jf.setDefaultCloseOperation (WindowConstants.EXIT_ON_CLOSE); / / 1st JPanel, using the default floating layout JPanel panel01 = new JPanel (); panel01.add (new JLabel ("username")); panel01.add (new JTextField (10)) / / 2nd JPanel, using the default floating layout JPanel panel02 = new JPanel (); panel02.add (new JLabel ("password")); panel02.add (new JPasswordField (10)); / / 3rd JPanel, using floating layout, and the components in the container are centered with JPanel panel03 = new JPanel (new FlowLayout (FlowLayout.CENTER)); panel03.add (new JButton ("login")); panel03.add (new JButton ("registration")) / / create a vertical box container and add the above three JPanel as content panels to the window Box vBox = Box.createVerticalBox (); vBox.add (panel01); vBox.add (panel02); vBox.add (panel03); jf.setContentPane (vBox); jf.pack (); jf.setLocationRelativeTo (null); jf.setVisible (true);}}

Thank you for reading! This is the end of the article on "how to use the JavaSwingJPanel panel". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Panels layouts content containers floats more users articles components login good practical code official examples commonly used articles methods is in the user name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno macOS Linux Apple