JPanel is class of swing package in java and swing package part of j2se to develop standalone application. JPanel is a container and component of the JFrame.
Constructor & Description
1. JPanel()
Creates a JPanel with a double buffer and a flow layout.
2. JPanel(boolean isDoubleBuffered)
Creates a JPanel with FlowLayout and the specified buffering strategy.
3. JPanel(LayoutManager layout)
Create a buffered JPanel with the specified layout manager.
4. JPanel(LayoutManager layout, boolean isDoubleBuffered)
Creates a JPanel with the specified layout manager and buffering strategy.
package com.findnerd;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SwingContainerDemo {
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JLabel msglabel;
public SwingContainerDemo(){
mainFrame = new JFrame("Java Swing Examples");
mainFrame.setSize(400,400);
mainFrame.setLayout(new GridLayout(3, 1));
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
headerLabel = new JLabel("", JLabel.CENTER);
statusLabel = new JLabel("",JLabel.CENTER);
statusLabel.setSize(350,100);
msglabel = new JLabel("Welcome to Findnerd Blog.", JLabel.CENTER);
controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
mainFrame.add(headerLabel);
mainFrame.add(controlPanel);
mainFrame.add(statusLabel);
mainFrame.setVisible(true);
}
public static void main(String[] args){
SwingContainerDemo swingContainerDemo = new SwingContainerDemo();
swingContainerDemo.showJPanelDemo();
}
private void showJPanelDemo(){
headerLabel.setText("Container in action: JPanel");
JPanel panel = new JPanel();
panel.setBackground(Color.magenta);
panel.setLayout(new FlowLayout());
panel.add(msglabel);
controlPanel.add(panel);
mainFrame.setVisible(true);
}
}
0 Comment(s)