Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Create JPanel in java with example

    • 0
    • 2
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 275
    Comment on it

    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.

    1. package com.findnerd;
    2.  
    3. import java.awt.*;
    4. import java.awt.event.*;
    5. import javax.swing.*;
    6.  
    7. public class SwingContainerDemo {
    8. private JFrame mainFrame;
    9. private JLabel headerLabel;
    10. private JLabel statusLabel;
    11. private JPanel controlPanel;
    12. private JLabel msglabel;
    13.  
    14. public SwingContainerDemo(){
    15. mainFrame = new JFrame("Java Swing Examples");
    16. mainFrame.setSize(400,400);
    17. mainFrame.setLayout(new GridLayout(3, 1));
    18. mainFrame.addWindowListener(new WindowAdapter() {
    19. public void windowClosing(WindowEvent windowEvent){
    20. System.exit(0);
    21. }
    22. });
    23. headerLabel = new JLabel("", JLabel.CENTER);
    24. statusLabel = new JLabel("",JLabel.CENTER);
    25.  
    26. statusLabel.setSize(350,100);
    27.  
    28. msglabel = new JLabel("Welcome to Findnerd Blog.", JLabel.CENTER);
    29.  
    30. controlPanel = new JPanel();
    31. controlPanel.setLayout(new FlowLayout());
    32.  
    33. mainFrame.add(headerLabel);
    34. mainFrame.add(controlPanel);
    35. mainFrame.add(statusLabel);
    36. mainFrame.setVisible(true);
    37. }
    38.  
    39. public static void main(String[] args){
    40. SwingContainerDemo swingContainerDemo = new SwingContainerDemo();
    41. swingContainerDemo.showJPanelDemo();
    42. }
    43.  
    44.  
    45. private void showJPanelDemo(){
    46. headerLabel.setText("Container in action: JPanel");
    47.  
    48. JPanel panel = new JPanel();
    49. panel.setBackground(Color.magenta);
    50. panel.setLayout(new FlowLayout());
    51. panel.add(msglabel);
    52.  
    53. controlPanel.add(panel);
    54. mainFrame.setVisible(true);
    55. }
    56. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: