Jpanel In Java Example
Chapter:
Swing
Last Updated:
28-09-2016 17:05:31 UTC
Program:
/* ............... START ............... */
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JavaJPanelExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel buttonPanel = new JPanel(new BorderLayout());
JButton northButton = new JButton("North");
JButton southButton = new JButton("South");
JButton eastButton = new JButton("East");
JButton westButton = new JButton("west");
buttonPanel.add(northButton, BorderLayout.NORTH);
buttonPanel.add(southButton, BorderLayout.SOUTH);
buttonPanel.add(eastButton, BorderLayout.EAST);
buttonPanel.add(westButton, BorderLayout.WEST);
frame.add(buttonPanel, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
/* ............... END ............... */
Output
Notes:
-
The JPanel class provides general-purpose containers for lightweight components. Jpanel In Java, Swing, JavaBy default, panels do not add colors to anything except their own background.
- The class JPanel is a generic lightweight container.
- JPanel() - Creates a new JPanel with a double buffer and a flow layout.
- JPanel(boolean isDoubleBuffered) - Creates a new JPanel with FlowLayout and the specified buffering strategy.
- JPanel(LayoutManager layout) - Create a new buffered JPanel with the specified layout manager.
- JPanel(LayoutManager layout, boolean isDoubleBuffered) - Creates a new JPanel with the specified layout manager and buffering strategy.
Tags
Jpanel In Java, Swing, Java