Java swing | Java JPanel

Programming languages or concepts
0

Java JPanel :- 

JPanel, a part of Java Swing package, is a container that can store a group of components. The main task of JPanel is to organize components,

It provides space in which an application can attach any other component. It inherits the JComponents class, In Application or Frame it provides some space so that we can use it by putting other components.



JPanel class declaration :-

public class JPanel extends JComponent implements Accessible  


Commonly used Constructors:


  Constructor      Description
JPanel() It is used to create a new JPanel with a double buffer and a flow layout.
JPanel(LayoutManager l) creates a new JPanel with specified layoutManager
JPanel(boolean isDoubleBuffered) It is used to create a new JPanel with FlowLayout and the specified buffering strategy.
JPanel(LayoutManager l, boolean isDoubleBuffered) creates a new JPanel with specified layoutManager and a specified buffering strategy


Commonly used functions :




  Constructor      Description
add(Component c)  adds component to a specified container
getUI( ) returns the look and feel object that renders this component.
updateUI( )  resets the UI property with a value from the current look and feel.
paramString()  returns a string representation of this JPanel.
getUIClassID()  returns the name of the Look and feel class that renders this component.
getAccessibleContext() gets the AccessibleContext associated with this JPanel.


Methods Inherited

This class inherits methods from the following classes −

  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JPanel simple example 


   import java.awt.*;  
import javax.swing.*; 
/**
 *
 * @author Saurabh
 */
public class jPanel {

     jPanel()  
         {  
        JFrame f= new JFrame("Panel Example");    
        JPanel panel=new JPanel();  
        panel.setBounds(80,80,200,200);    
        panel.setBackground(Color.orange);  
        JButton b1=new JButton("click ");     
        b1.setBounds(200,200,400,400);    
        b1.setBackground(Color.red);   
        
         
        panel.add(b1);  
        f.add(panel);  
                f.setSize(400,400);    
                f.setLayout(null);    
                f.setVisible(true);    
        }  
        public static void main(String args[])  
        {  
        new jPanel();  
        }  
    }  


Output :




Post a Comment

0Comments

Post a Comment (0)
close