This section contains the detail about the JPanel in java.
Working with JPanel
JPanel class is an extension of the JComponent class that provides a replacement for the AWT Panel class. A panel is used to hold other Swing components. You create a panel and add various components to it. The panel is further added to the content pane, which represents the display area of a window, a dialog, or a frame. JPanel supports all layout managers of AWT. By default, JPanel applies the flow layout manager. The following list describes the constructor that you can use to create an instance of the JPanel class:
- JPanel(): Creates a new JPanel with double buffer and flow layout as the default layout.
- JPanel(boolean isDoubleBuffered): Creates a new JPanel with flow layout andthe specified buffering strategy.
- JPanel(LayoutManager layout): Creates a new buffered JPanel with the specified layout manager.
- JPanel(LayoutManager layout, boolean isDoubleBuffered): Creates a new JPanelwith the specified layout.
Example :
package corejava; import javax.swing.*; public class JPanelDemo { public static void main(String[] a) { JPanel panel = new JPanel(); JButton okButton = new JButton("OK"); panel.add(okButton); JButton cancelButton = new JButton("Cancel"); panel.add(cancelButton); JFrame frame = new JFrame("JPanel Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
[ 0 ] Comments