Foro de elhacker.net

Programación => Java => Mensaje iniciado por: maikmilk en 30 Abril 2013, 15:34 pm



Título: [JAVA] JPanel y JButton
Publicado por: maikmilk en 30 Abril 2013, 15:34 pm
Tengo un problema y es el siguiente.
Uso NeatBeans como IDE y estoy creando una aplicación de escritorio mendiante el editor que ofrece NeatBeans.

He creado un JFrame de un tamaño de 500*400 y sobre ese JFrame añado un JPanle de 500*400 con un textfield y dos botones que al presionarlos llaman a otro JPanel implementado en el mismo paquete tambien atraves del editor de NeatBeans. En este JPanel coloco una serie de cajas de texto, etiquetas,botones etc... , y aqui surge el problema y es que el JPanel se muestra pero lo que no se muestra son los botones, etiquetas y de mas que he añadido mediante el editor.

Cual podría ser el problema?

Adjunto codigo

Código:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aplicacionindemnizaciones.vista;

/**
 *
 * @author Miki
 */
public class Principal extends javax.swing.JFrame {

    /**
     * Creates new form Principal
     */
    public Principal() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        PanelPrincipal = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        PanelPrincipal.setPreferredSize(new java.awt.Dimension(500, 400));

        jLabel1.setFont(new java.awt.Font("Apple Chancery", 1, 36)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 0, 51));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Aplicación FOL");

        jButton1.setText("Calculo Indemnizaciones");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Calculo Nominas");

        org.jdesktop.layout.GroupLayout PanelPrincipalLayout = new org.jdesktop.layout.GroupLayout(PanelPrincipal);
        PanelPrincipal.setLayout(PanelPrincipalLayout);
        PanelPrincipalLayout.setHorizontalGroup(
            PanelPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(PanelPrincipalLayout.createSequentialGroup()
                .add(147, 147, 147)
                .add(PanelPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(152, Short.MAX_VALUE))
        );
        PanelPrincipalLayout.setVerticalGroup(
            PanelPrincipalLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(PanelPrincipalLayout.createSequentialGroup()
                .add(80, 80, 80)
                .add(jLabel1)
                .add(51, 51, 51)
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton2)
                .addContainerGap(108, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(PanelPrincipal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, PanelPrincipal, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        Indemnizaciones i = new Indemnizaciones();
        getContentPane().add(i);
        i.setVisible(true);
        PanelPrincipal.setVisible(false);
        i.setVisible(true);
    }                                       

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify                     
    public static javax.swing.JPanel PanelPrincipal;
    public static javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
}

Código:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aplicacionindemnizaciones.vista;

/**
 *
 * @author Miki
 */
public class Indemnizaciones extends javax.swing.JPanel {

    /**
     * Creates new form Indemnizaciones
     */
    public Indemnizaciones() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        Antigüedad = new javax.swing.JLabel();
        intAnyo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField4 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField6 = new javax.swing.JTextField();

        jLabel3.setText("Salario Base:");

        Antigüedad.setText("Antigüedad");

        intAnyo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2008", "2009", "2010", "2011", "2012", "2013" }));
        intAnyo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                intAnyoActionPerformed(evt);
            }
        });

        jLabel4.setText("Pluses:");

        jLabel5.setText("Pagas extraordinarias:");

        jLabel6.setText("Vacaciones disfrutadas:");

        jLabel7.setText("€");

        jLabel8.setText("días");

        jLabel9.setText("€");

        jLabel10.setText("€");

        jButton1.setText("Calcular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField6ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(jLabel3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 82, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel7)
                                .add(18, 18, 18)
                                .add(Antigüedad)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(intAnyo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(jLabel4)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 56, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel9)
                                .add(18, 18, 18)
                                .add(jLabel5)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 94, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel10))
                            .add(layout.createSequentialGroup()
                                .add(jLabel6)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 40, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel8))))
                    .add(layout.createSequentialGroup()
                        .add(110, 110, 110)
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .add(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(14, 14, 14)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel7)
                    .add(Antigüedad)
                    .add(intAnyo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel9)
                    .add(jLabel5)
                    .add(jTextField4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel10))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(jTextField5, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel8))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jTextField6, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(12, Short.MAX_VALUE))
        );
    }// </editor-fold>                       

    private void intAnyoActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
    }                                       

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                       

    private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                           

    // Variables declaration - do not modify                     
    private javax.swing.JLabel Antigüedad;
    private javax.swing.JComboBox intAnyo;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    // End of variables declaration                   
}


Título: Re: [JAVA] JPanel y JButton
Publicado por: dennis094 en 30 Abril 2013, 23:14 pm
El codigo es demasiado largo para mirarlo ahora mismo ya que ha perdido el madrid y no estoy de animo...  :-\ jajajja pero bueno

Asi a primera vista no encuentro nada raro, pero tendria que mirarlo mas, lo que puede ser, es que al crear un jFrame de 500*800 y un jPanel del mismo tamaño, si luego creas otro jPanel, este no se va a mostrar ya que no tiene espacio suficiente en el Frame.

Prueba a cambiar el tamaño .

Si no se arregla con eso vuelve a escribir y ya miro el codigo en profundidad.
Un saludoo


Título: Re: [JAVA] JPanel y JButton
Publicado por: sapito169 en 1 Mayo 2013, 00:25 am
desistala netbeans
agarra algun libro o tutorial y ponte a estudiar y tus problemas estaran resueltos


Título: Re: [JAVA] JPanel y JButton
Publicado por: visualfree en 1 Mayo 2013, 05:59 am
+1 sapito169.


Título: Re: [JAVA] JPanel y JButton
Publicado por: sapito169 en 1 Mayo 2013, 15:49 pm
tu primera lección es que nadie entiende el código creado por netbean si te dice que lo entiende es que te esta mintiendo

nadie en su sano juicio intentara usar el código creado por netbeans por que es horrible

1 creas un jframe
2 le pones al jframe un BorderLayaut
3 creas tu Jpanel
4 agregas tu jpanel con BorderLayaut.CENTER al JFrame

lo que he explicado lo puedes hacer a mano o con netbeans o con cualquier ide es mas con block de notas

si te suena a chino los pasos que te di es que no estudiaste bien


Título: Re: [JAVA] JPanel y JButton
Publicado por: maikmilk en 2 Mayo 2013, 02:36 am
A ver gente la pregunta es como hacerlo con neatbeans lo que quiero hacer ya lo e hecho sin usar neatbeans, deberías centrarse más en contestar a las preguntas que a suponer que no se hacer algo que de sobra se hacer.


Título: Re: [JAVA] JPanel y JButton
Publicado por: sapito169 en 2 Mayo 2013, 15:20 pm
entonces ponte un vídeo tutorial en youtube

ahora ya sabes por que netbeans apesta la única forma de corregir tu error es ir hasta tu casa mirarte del hombro y darte un sopapo cuando cometiste un error para luego corregir te

tu única esperanza es que o mires vídeo tutoriales donde creen interfaces de usuario en youtube

o busques a alguien que vaya a tu casa a darte un sopapo cuando vea que cometiste un error

a propósito me ofrezco como voluntario para ir asta tu casa para cor regirte si me pagas los pasajes


Título: Re: [JAVA] JPanel y JButton
Publicado por: maikmilk en 2 Mayo 2013, 18:31 pm
entonces ponte un vídeo tutorial en youtube

ahora ya sabes por que netbeans apesta la única forma de corregir tu error es ir hasta tu casa mirarte del hombro y darte un sopapo cuando cometiste un error para luego corregir te

tu única esperanza es que o mires vídeo tutoriales donde creen interfaces de usuario en youtube

o busques a alguien que vaya a tu casa a darte un sopapo cuando vea que cometiste un error

a propósito me ofrezco como voluntario para ir asta tu casa para cor regirte si me pagas los pasajes

No entiendo? Bueno gracias por la NO ayuda


Título: Re: [JAVA] JPanel y JButton
Publicado por: 0xFer en 2 Mayo 2013, 20:55 pm
por que no creas en vez de un panel,un Frame nuevo


Título: Re: [JAVA] JPanel y JButton
Publicado por: YairMon en 19 Mayo 2013, 22:57 pm
Tienen razon!!... ¿Por que razon vienes aqui a postear codigo (horrible) de netBeans?

Solo haz un frame y un panel y pon el boton

 :xD  

deja la pereza >_<

PD. Muy claramente dice en las reglas que no es obligacion de nadie responder las preguntas

PD2: Pon en el metodo paint(graphics g) al final "super.paintcomponents(g)"




Título: Re: [JAVA] JPanel y JButton
Publicado por: jhonatanAsm en 19 Mayo 2013, 23:47 pm
netBeans es muy cerrado en su código.
te recomiendo que uses eclipse.
salu2.


Título: Re: [JAVA] JPanel y JButton
Publicado por: darkerek en 23 Mayo 2013, 02:11 am
cuendo muestres el otro panel igual esto te ayuda:

getContentPane().add(miOtroPanel, java.awt.BorderLayout.CENTER);