Los JPanel en Java son objetos contenedores, la finalidad de estos objetos es la agrupación de otros objetos tales como botones, campos de texto, etiquetas, selectores, etc; una gran ventaja de Usar JPanel en Java es que podemos manejar la agrupación de una mejor forma, supongamos que tenemos una serie de botones en un panel, y deseamos desactivarlos todos a las vez, en lugar de hacerlo individualmente con los botones, podemos desactivar el panel y con esto los botones.
CREANDO JPANEL EN JAVA
Con el siguiente ejemplo voy a mostrar tres paneles, cada uno con un layout diferente, ademas de esto tres botones los cuales mostraran como quitar o poner la visibilidad en cada panel obteniendo el beneficio de la agrupación que comentaba en la introducción:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class GuiJava implements ActionListener{//implementando el listener de eventos JPanel jp1, jp2, jp3; JButton jb1, jb2, jb3, jbP1, jbP2, jbP3; public GuiJava(){ JFrame jfM = new JFrame("JPanel En Java"); jfM.setLayout(null); gridJP(); bordJP(); flowJP(); //invocamos los metodos que contienen los paneles jbP1 = new JButton("Panel 1"); jbP2 = new JButton("Panel 2"); jbP3 = new JButton("Panel 3"); jp1.setBounds(10, 10, 200, 200); jp2.setBounds(240, 10, 400, 250); jp3.setBounds(10, 270, 400, 150); jbP1.setBounds(10, 430, 90, 20); jbP2.setBounds(110, 430, 90, 20); jbP3.setBounds(210, 430, 90, 20); jfM.add(jp1); jfM.add(jp2); jfM.add(jp3); jfM.add(jbP1); jfM.add(jbP2); jfM.add(jbP3); jbP1.addActionListener(this); jbP2.addActionListener(this); jbP3.addActionListener(this); jfM.setLocation(100, 50); jfM.setResizable(false); jfM.setVisible(true); jfM.setSize(800, 600); jfM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void gridJP(){ jp1 = new JPanel(new GridLayout(3, 1, 5, 7));//filas, columnas, espacio entre filas, espacio entre columnas jb1= new JButton("B1"); jb2= new JButton("B2"); jb3= new JButton("B3");//creamos los objetos para el panel jp1.add(jb1); jp1.add(jb2); jp1.add(jb3);//añadimos los objetos al jpanel jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jp1.setVisible(true); } public void bordJP(){ jp2 = new JPanel(new BorderLayout(2, 3));//espacio entre las regiones, horizontal y vertical jb1= new JButton("B1"); jb2= new JButton("B2"); jb3= new JButton("B3");//añadiendo objetos al jpanel jp2.add(jb1, BorderLayout.NORTH);//boton al panel norte jp2.add(jb2, BorderLayout.WEST); //boton a la region oeste jp2.add(jb3, BorderLayout.CENTER); //boton a la region centro jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jp2.setVisible(true); } public void flowJP(){ jp3 = new JPanel(new FlowLayout()); jb1= new JButton("B1"); jb2= new JButton("B2"); jb3= new JButton("B3");//añadiendo objetos al jpanel jp3.add(jb1); jp3.add(jb2); jp3.add(jb3);//añadimos los objetos al jpanel jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jp3.setVisible(true); } public static void main(String[] args) { GuiJava gj = new GuiJava();//uso de constructor para la ventana } @Override public void actionPerformed(ActionEvent e) {//sobreescribimos el metodo del listener if(e.getSource() == jbP1){ if(jp1.isVisible()){ jp1.setVisible(false); }else jp1.setVisible(true); }else if(e.getSource() == jbP2){ if(jp2.isVisible()){ jp2.setVisible(false); }else jp2.setVisible(true); }else if(e.getSource() == jbP3){ if(jp3.isVisible()){ jp3.setVisible(false); }else jp3.setVisible(true); }else{ JOptionPane.showMessageDialog(null, e.getActionCommand()); } } } |
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class GuiJava implements ActionListener{//implementando el listener de eventos JPanel jp1, jp2, jp3; JButton jb1, jb2, jb3, jbP1, jbP2, jbP3; public GuiJava(){ JFrame jfM = new JFrame("JPanel En Java"); jfM.setLayout(null); gridJP(); bordJP(); flowJP(); //invocamos los metodos que contienen los paneles jbP1 = new JButton("Panel 1"); jbP2 = new JButton("Panel 2"); jbP3 = new JButton("Panel 3"); jp1.setBounds(10, 10, 200, 200); jp2.setBounds(240, 10, 400, 250); jp3.setBounds(10, 270, 400, 150); jbP1.setBounds(10, 430, 90, 20); jbP2.setBounds(110, 430, 90, 20); jbP3.setBounds(210, 430, 90, 20); jfM.add(jp1); jfM.add(jp2); jfM.add(jp3); jfM.add(jbP1); jfM.add(jbP2); jfM.add(jbP3); jbP1.addActionListener(this); jbP2.addActionListener(this); jbP3.addActionListener(this); jfM.setLocation(100, 50); jfM.setResizable(false); jfM.setVisible(true); jfM.setSize(800, 600); jfM.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void gridJP(){ jp1 = new JPanel(new GridLayout(3, 1, 5, 7));//filas, columnas, espacio entre filas, espacio entre columnas jb1= new JButton("B1"); jb2= new JButton("B2"); jb3= new JButton("B3");//creamos los objetos para el panel jp1.add(jb1); jp1.add(jb2); jp1.add(jb3);//añadimos los objetos al jpanel jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jp1.setVisible(true); } public void bordJP(){ jp2 = new JPanel(new BorderLayout(2, 3));//espacio entre las regiones, horizontal y vertical jb1= new JButton("B1"); jb2= new JButton("B2"); jb3= new JButton("B3");//añadiendo objetos al jpanel jp2.add(jb1, BorderLayout.NORTH);//boton al panel norte jp2.add(jb2, BorderLayout.WEST); //boton a la region oeste jp2.add(jb3, BorderLayout.CENTER); //boton a la region centro jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jp2.setVisible(true); } public void flowJP(){ jp3 = new JPanel(new FlowLayout()); jb1= new JButton("B1"); jb2= new JButton("B2"); jb3= new JButton("B3");//añadiendo objetos al jpanel jp3.add(jb1); jp3.add(jb2); jp3.add(jb3);//añadimos los objetos al jpanel jb1.addActionListener(this); jb2.addActionListener(this); jb3.addActionListener(this); jp3.setVisible(true); } public static void main(String[] args) { GuiJava gj = new GuiJava();//uso de constructor para la ventana } @Override public void actionPerformed(ActionEvent e) {//sobreescribimos el metodo del listener if(e.getSource() == jbP1){ if(jp1.isVisible()){ jp1.setVisible(false); }else jp1.setVisible(true); }else if(e.getSource() == jbP2){ if(jp2.isVisible()){ jp2.setVisible(false); }else jp2.setVisible(true); }else if(e.getSource() == jbP3){ if(jp3.isVisible()){ jp3.setVisible(false); }else jp3.setVisible(true); }else{ JOptionPane.showMessageDialog(null, e.getActionCommand()); } } }
Si se ejecuta el codigo se vera la siguiente ventana:
Y si queremos algo mas, como adornar el panel, hacer visible la frontera de este, configurar el borde con color al jpanel, o asignarle margen interno al jpanel; esto lo haremos con la siguiente linea:
1 2 | //la siguiente linea es para dar margen interior y color al jpanel jp1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(10, 15, 5, 15))); |
//la siguiente linea es para dar margen interior y color al jpanel jp1.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black),BorderFactory.createEmptyBorder(10, 15, 5, 15)));
El jp1, corresponde al objeto del primer panel, si se desea este borde a determinado panel, hay que configurarlo por separado, en el siguiente ejemplo se configuro para el primer y segundo panel:
Como se ve en la anterior imagen, un cambio mínimo hace una buena diferencia para la vista del usuario, y esto es importante 😉