Site icon TestingDocs.com

Swing JPanel Container Class

Overview

In this tutorial, we will learn about Swing JPanel Container Class. JPanel class is a lightweight container.

JPanel Container Class

JPanel is a subclass of an abstract class JComponent and implements an Accessible interface.

Java Program

package com.testingdocs.swing.components;

import java.awt.*; 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**********************************************
 * FileName: JPanelDemo.java
 * Package : com.testingdocs.swing.components
 * 
 * Java Tutorials - www.TestingDocs.com
 **********************************************/

public class JPanelDemo {
	public static void main(String[] args) {

		JFrame frame = new JFrame("JPanelDemo");
		JPanel panel = new JPanel();
		panel.setLayout(new FlowLayout());
		JLabel greeting = new JLabel("Hello World!\n");
		JLabel websiteLabel = new JLabel("www.TestingDocs.com");
 
		//add labels to panel
		panel.add(greeting);
		panel.add(websiteLabel);

		//Frame properties
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(400, 300);
		frame.getContentPane().add(panel);
		frame.setVisible(true);
	}
}

Output

Run the Java application to view the output.

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/

Exit mobile version