Constructors
- Panel(): It creates the new panel using the default layout manager.
- Panel(LayoutManager layout): It creates the new panel using the specified layout manager.
Read More About, Basics of Java
Methods
- void addNotify(): This method creates the Panel’s peer.
- AccessibleContext getAccessibleContext(): It returns the AccessibleContext associated with this Panel.
AWT Panel Example
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();
}
}
Output
Practice by yourself on online java compiler for better understanding.
FAQs
What is AWT Panel used for?
AWT panel is a container inside which an application can connect with other components.
Which all classes AWT Panel class inherits?
AWT Panel class inherits Container, Component, and Object class.
Conclusion
In this article, we have extensively discussed the AWT Panel in Java. We start with a brief introduction to AWT Panel Class in Java, then discussed its various constructors, methods, and its examples.
After reading this blog, you will be able to use AWT Panel class in Java, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see Java, AWT vs Swing.
You can refer to the Data Structures and Algorithms, JavaScript courses which can upskill you. You can discover more such courses by referring to the Guided Path on Coding Ninjas Studio. You can also test your knowledge by taking up the mock test and contests which are held by Code studio.
If you are about to interview with companies like Google, Adobe, Amazon, etc. then you can refer to problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!