Showing posts with label JPanel in java. Show all posts
Showing posts with label JPanel in java. Show all posts

Sunday, 5 May 2019

JPanel in java - Online Help

JPanel in java:

The JPanel in java is a class which is used to hold components such as JLabel, JTextField, JTextArea, JButton, etc. Panels are created using the class JPanel.

Constructor for the JPanel in Java:

The JPanel class has three constructors. They are:

JPanel()

This constructor is used to create a new panel with a double buffer and a flow layout.

JPanel(Boolean isDblbuf)

This constructor is used to creates a new JPanel with flow layout and the specified buffering strategy.

JPanel(LayoutManager l)

This constructor is used to creates a new buffered JPanel with the specified layout manager.

Methods for JPanel in Java:

The most commonly used JPanel() methods are as follows:

void add(JComponent obj)

This method is used to add JComponent objects such as JLabel, JButton, etc into the JPanel.

void setLayout(Object obj)

This method is used to set the size of the panel with the size of the object obj.

Sample program:

import java.awt.*;
import javax.swing.*;
public class JPanelDemo extents JFrame
{
public JPanelDemo()
{
JPanel jp=new JPanel();
JLabel jl1= new JLabel("online help");
JLabel jl2= new JLabel("online help");
Font f=new Font("Arial",Font.BOLD,20);
jl2.setFont(f);
jp.add(jl1);
jp.add(jl2);
getContentPane().add(jp);
}
public static void main(String args[])
{
JPanelDemo jbd=new JPanelDemo();
jbd.setSize(500,500);
jbd.setVisible(true);
}
}