Panel in java using applet

In this program, we have created a panel using the applet with the AWT package in java. So first we will discuss what is a panel then we will learn how to create panel in Java.

Panel in Java using applet with AWT Package

The panel is the simplest container class. The panel provides us space so that an application can attach any other component. It also inherits the container class.

AWT panel class declaration public

 public class Panel extends Container implements Accessible

Java AWT panel example

Java program to create panel:

import java.awt.*;  
public class PanelExample {  
     PanelExample()  
        {  
        Frame f= new Frame("Panel Example");    
        Panel panel=new Panel();  
        panel.setBounds(40,80,200,200);    
        panel.setBackground(Color.gray);  
        Button b1=new Button("Button 1");     
        b1.setBounds(50,100,80,30);    
        b1.setBackground(Color.yellow);   
        Button b2=new Button("Button 2");   
        b2.setBounds(100,100,80,30);    
        b2.setBackground(Color.green);   
        panel.add(b1); panel.add(b2);  
        f.add(panel);  
        f.setSize(400,400);    
        f.setLayout(null);    
        f.setVisible(true);    
        }  
        public static void main(String args[])  
        {  
        new PanelExample();  
        }  
}
panel in Java using applet

Output: Panel in Java using applet

p1=new JPanel();
p1.setBounds(400,0,100,500);
p1.setLayout(new GridLayout(1,2));

 

In this program first, we import the AWT package and create the frame and button if anybody did not know Jbutton in java please Click this link

PanelTest()throws Exception
{
p1=new JPanel();
p1.setBounds(0,0,400,500);
p1.setLayout(new GridLayout(4,4));

p1=new JPanel();
p1.setBounds(400,0,100,500);
p1.setLayout(new GridLayout(1,2));

for(int i=1;i<b1.length;i++)
{
b1[i]=new JButton(""+i+"");
p1.add(b1[i]);
}
b2=new JButton("show");
p1.add(b2);
b3=new JButton("sumbit");
p2.add(b3);
p1.setVisible(false);
p2.setVisible(true);
}

 




Now we have created the panel using the JPanel method. which is mentioned above already and this we create the buttons on the panel. JPanel() is used to create a new JPanel with a double buffer and a flow layout.Jpanel(boolean isDoubleBuffered) JPanel(LayoutManager layout)

I hope all of you are satisfied if not please do comments. All of us are interested in new things, please check regularly.


Leave a Reply

Your email address will not be published. Required fields are marked *