Java JPanel Basic Tutorial

JPanel Basic Tutorial

 Java JPanel Basic Tutorial
Java JPanel is a container class that provides space for application to attach other components.This class it inherits from JComponent class.
Note that JPanel class has no menubar like JFrame JMenuBar.


How to declare Java JPanel class

[class JPanel extends JComponent implements Accessible
 
]

Java JPanel Constructors

  • JPanel()-creates a JPanel with a doule buffer and flow layout.
  • JPanel(boolean isBuffered)-creates JPanel with flowLayout and specified buffering strategy.
  • JPanel(LayoutManager layout)-create JPanel with specified Layout.

Java JPanel Example

[
/** * This program creates A JFrame and adds JPanel  
* with two buttons
 * * @author Eric */

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
public class JavaJPanel extends JFrame {
    JavaJPanel()
    {
        super("JFrame Example");
        JPanel panel=new JPanel();

        panel.setBounds(10,20,200,100);
        panel.setBackground(Color.BLUE);
        JButton b1=new JButton("Button 1");
        b1.setBounds(10,100,80,30);
        b1.setBackground(Color.PINK);
        JButton b2=new JButton("Button 2");
        b2.setBounds(100,100,80,30);
        b2.setBackground(Color.MAGENTA);
        panel.add(b1); panel.add(b2);
        add(panel);
        setSize(300,200);
        setLocationRelativeTo(null);
        setLayout(null);
        setVisible(true);



    }
    public static void main(String args[])
    {
        new JavaJPanel();
    }

}
]

Java JPanel Example adding ActionListener

[
/** * This program creates A JFrame and adds JPanel 

 * with two buttons which implement ActionListener *

 * @author Eric */

import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class JavaJPanel extends JFrame implements ActionListener{
    public   JButton b1,b2;
    JavaJPanel()
    {
        super("JFrame Example");
        JPanel panel=new JPanel();

        panel.setBounds(10,20,150,100);
        panel.setBackground(Color.BLUE);
        b1=new JButton("Button 1");
        b1.setBounds(10,100,80,30);
        b1.setBackground(Color.PINK);
        b1.addActionListener(this);

        b2=new JButton("Button 2");
        b2.addActionListener(this);
        b2.setBounds(100,100,80,30);
        b2.setBackground(Color.MAGENTA);
        panel.add(b1); panel.add(b2);
        add(panel);
        setSize(300,200);
        setLocationRelativeTo(null);
        setLayout(null);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent evt)
    {
        if(evt.getSource()==b1)
        {
            JOptionPane.showMessageDialog(this,"You clicked Button 1",
                    "JPanel Example",JOptionPane.INFORMATION_MESSAGE);
        }

        if(evt.getSource()==b2)
        {
            JOptionPane.showMessageDialog(this,"You clicked Button 2",
                    "JPanel Example",JOptionPane.INFORMATION_MESSAGE);
        }

    }
    public static void main(String args[])
    {
        new JavaJPanel();
    }

}
]
JPanel example





Related Tutorials

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java JPanel Basic Tutorial
Java JPanel Basic Tutorial
JPanel Basic Tutorial
https://2.bp.blogspot.com/-29EpUu4OFvA/WOIQHJLr0UI/AAAAAAAAHaw/h0ZjskvQi6gsk8VokXVrmQOYhX3opMG0gCLcB/s200/java-jpanel-example-min.PNG
https://2.bp.blogspot.com/-29EpUu4OFvA/WOIQHJLr0UI/AAAAAAAAHaw/h0ZjskvQi6gsk8VokXVrmQOYhX3opMG0gCLcB/s72-c/java-jpanel-example-min.PNG
Java Codes
http://www.download-all.net/2017/04/java-jpanel-basic-tutorial.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/04/java-jpanel-basic-tutorial.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy