Duvida sobre Jframe

Ola pessoal tudo bem. Eu estou com uma duvida no meu codigo. Eu criei uma clase Jframe de nome Aplicativo e tambem criei outras classes “Entrar JPanel” e tambem um classe “Mostrar” JPanel. No JFrame eu consigo fazer com que apareça os meus JPanel. No “Entrar JPanel” eu tenho 2 campos e um botão gravar aonde eu insiro nome e idade e ficam gravados OK? Como eu faço para colocar esse codigo String aux = jTextField1.getText(); no Jframe, porque sempre que eu compilo diz que o JTextField1não tem. Mas como? se este campo esta no “Entrar JPanel”!! Minha duvida é. Como eu faço o Jframe conheçer um campo do JPanel. Descupla se me expressei errado. Meu codigo esta ai. Abraço

public class Aplicativo1 extends javax.swing.JFrame {

    Painel painel;
    Mostrar mostrar;
    Entrar entrar,b;
    Dados vet[];

    int cont = 0;
    /** Creates new form Aplicativo1 */
    public Aplicativo1()
    {
        vet = new Dados[3];
        for(int x=0;x<vet.length; x++)
        {
            vet[x]= new Dados();
        }
        painel = new Painel();
        mostrar = new Mostrar();
        entrar = new Entrar();
        getContentPane().add(painel);
        getContentPane().add(mostrar);
        getContentPane().add(entrar);

        painel.setBounds(0, 0, painel.getWidth(), painel.getHeight());
        painel.setVisible(false);
        mostrar.setBounds(0, 0, mostrar.getWidth(), mostrar.getHeight());
        mostrar.setVisible(false);
        entrar.setBounds(0, 0, entrar.getWidth(), entrar.getHeight());
        entrar.setVisible(false);

        initComponents();
      //  b.JtextField1.setVisible(true);
    }

    /** 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() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(null);

        jButton1.setText("Mostra Painel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(60, 250, 270, 23);

        jButton2.setText("Entrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(60, 190, 90, 23);

        jButton3.setText("Mostrar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3);
        jButton3.setBounds(210, 190, 100, 23);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-416)/2, (screenSize.height-338)/2, 416, 338);
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        painel.setVisible(true);
        mostrar.setVisible(false);
        entrar.setVisible(false);
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       entrar.setVisible(true);
       mostrar.setVisible(false);
       painel.setVisible(false);
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        mostrar.setVisible(true);
        entrar.setVisible(false);
        entrar.setVisible(false);
    }                                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                new Aplicativo1().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    // End of variables declaration                   

}

Abaixo o JPanel

public class Entrar extends javax.swing.JPanel {

    Dados d;
    /** Creates new form Entrar */
    public Entrar() {
        initComponents();
        setSize(340, 130);
    }

    /** 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() {

        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setLayout(null);

        jButton1.setText("Gravar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(110, 100, 120, 23);
        add(jTextField1);
        jTextField1.setBounds(120, 10, 110, 20);

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });
        add(jTextField2);
        jTextField2.setBounds(120, 60, 110, 20);

        jLabel1.setText("Nome: ");
        add(jLabel1);
        jLabel1.setBounds(30, 10, 60, 14);

        jLabel2.setText("Idade: ");
        add(jLabel2);
        jLabel2.setBounds(30, 50, 60, 14);
    }// </editor-fold>                        

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
        String aux = jTextField1.getText();
        String aux1 = jTextField2.getText();
        int y = Integer.parseInt(aux1);
        d.setNome(aux);
        d.setIdade(y);
    }                                        


    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                   

    void setD(Dados d1) { //aqui chama uma classe de encapsulamento.
        d = d1;
    }

}

E ai pessoal alguem pode me ajudar a resolver esse meu problema/

cade a classe PAinel? Tenta dar um repaint no painel. Mas coloca ai o codigo da classe PAinel

Está aqui o JPanel.

[code]
public class Painel extends javax.swing.JPanel {

/** Creates new form Painel */
public Painel() {
    initComponents();
    setSize(338, 182);
}

/** 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();
    jLabel2 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jButton1 = new javax.swing.JButton();

    setLayout(null);

    jLabel1.setText("Nome: ");
    add(jLabel1);
    jLabel1.setBounds(30, 50, 70, 14);

    jLabel2.setText("Idade: ");
    add(jLabel2);
    jLabel2.setBounds(30, 90, 60, 14);
    add(jTextField1);
    jTextField1.setBounds(130, 50, 140, 20);
    add(jTextField2);
    jTextField2.setBounds(130, 90, 70, 20);
    add(jTextField3);
    jTextField3.setBounds(270, 90, 60, 20);

    jButton1.setText("Maior Idade");
    add(jButton1);
    jButton1.setBounds(110, 150, 160, 23);
}// </editor-fold>                        


// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
// End of variables declaration                   

[/code] Só para explicar de novo. Quando eu clicar no Botão “entrar” do Jframe é para aparecer o os campos do JPanel Entrar ai como eu faço no JFrame para chamar essa classe para eu poder inserir os nome e idade. Abraço

cara rodei aqui esse seu exemplo e nao entendi bem oque vc quer…quando eu clico em “Entrar” me aparece uns 2 campos e o botão gravar, vc quer que o botao gravar funcione? Nao tenho somente a classe mostrar e dados mas rodei sem elas e nao entendi oq vc quer…flw…Tenta explicar melhor…

Primeira coisa muito obrigado por tentar me ajudar. Então vamos lá. É bem isso mesmo Eu quero colocar o nome e a idade e depois grave. OK! mas aonde é que eu coloco o codigo fonte? É no proprio codigo do Entrar Jpanel ou no JFrame. è isso que eu quero entendeu?
por exemplo. Se eu colocar esse codigo no Jframe ele diz que o TextField não tem no pacote. Eu quero chamar o campo de texto que está no Jpanel para o JFrame.

String aux = jTextField1.getText();//nome

lembre-se que o codigo é do PANEL e o texto e os botao estao dentro do panel o codigo de salvar tem que ficar nele, ou vc cria uma classe para fazer esse controle que seja acessada por todas as telas com metodos estaticos. mas se vc salvar no panel, o outro panel ou frame nao enxergarao aos valores, entao acho melhor vc fazer a classe.

Meu amigo não entendi muito bem. Pode por favor dar um exemplo. Não tem como eu colocar o meu codigo dentro no meu Frame e atravez dele chamar as outras classe Panel. Porque no meu frame tem o main né.
Esse codigo que eu quero colocar no programa no JFrame. so que dá erro nas classe. abraço

[code]
public Aplicativo()
{
vet = new Dados[2]; // dimensionamento do vetor
for(int x=0;x<vet.length;x++)
vet[x] = new Dados(); //instanciação dos elementos
initComponents();
jTextField3.setVisible(true);
}
private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
if(cont<vet.length)
{
String aux = jTextField1.getText();//nome
String aux1 = jTextField2.getText();//idade
int y = Integer.parseInt(aux1);
jTextField1.setText("");
jTextField2.setText("");
jTextField1.requestFocus();
vet[cont].setNome(aux);
vet [cont++].setIdade(y);

    }
    else
    {
        JOptionPane.showMessageDialog(null, "Vetor Cheio");
        jTextField3.setVisible(true);
    }
}                                           [/code]

da uma lida sobre metodos static. Ou pode passar o objeto por parametro. ai é com vc…:wink: se nao conseguir amanha continuamos…

fazendo pooII com o rolf? :stuck_out_tongue:
eu ja fiz esse exercicio, ta ai o codigo…da uma olhada

public class Janela extends javax.swing.JFrame {

    Entrar e;
    Mostrar m;
    Dados d[];


    /** Creates new form Janela */
    public Janela() {
        d = new Dados[5];
        e = new Entrar();
        m = new Mostrar();
        for(int x=0; x<d.length; x++)
            d[x] = new Dados();

        e.setD(d);
        m.setD(d);

        getContentPane().add(e);
        getContentPane().add(m);
        e.setBounds(0, 0, e.getWidth(), e.getHeight());
        m.setBounds(0, 0, m.getWidth(), m.getHeight());
        e.setVisible(false);
        m.setVisible(false);


        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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

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

        jButton2.setText("Mostrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(95, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(191, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        e.setVisible(true);
        m.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        e.setVisible(false);
        m.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Janela().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    // End of variables declaration//GEN-END:variables

}
public class Entrar extends javax.swing.JPanel {

    Dados d[];
    int indice = 0;

    /** Creates new form Entrar */
    public Entrar() {
        d = new Dados[5];
        for(int x=0;x<d.length;x++)
            d[x] = new Dados();
        initComponents();
        setSize(311, 143);
    }

    /** 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        setLayout(null);

        jLabel1.setText("Nome:");
        add(jLabel1);
        jLabel1.setBounds(30, 20, 70, 14);

        jButton1.setText("Gravar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(110, 100, 90, 23);

        jLabel2.setText("Idade:");
        add(jLabel2);
        jLabel2.setBounds(30, 60, 70, 14);
        add(jTextField1);
        jTextField1.setBounds(120, 20, 120, 20);
        add(jTextField2);
        jTextField2.setBounds(120, 60, 30, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(indice<d.length){
            String aux = jTextField1.getText();
            String aux1 = jTextField2.getText();
            jTextField1.setText("");
            jTextField2.setText("");
            int y = Integer.parseInt(aux1);
            d[indice].setNome(aux);
            d[indice].setIdade(y);
            indice++;
        }
        else{
            JOptionPane.showMessageDialog(this, "Vetor cheio!");
        }

    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

 
    void setD(Dados[] d1) {
        for(int x = 0; x < d1.length; x++){
            d[x] = d1[x];
        }
    }

}
public class Mostrar extends javax.swing.JPanel {
    Dados d[];
    /** Creates new form Mostrar */
    public Mostrar() {
        d = new Dados[5];
        for(int x=0;x<d.length;x++)
            d[x] = new Dados();
        initComponents();
        setSize(311, 143);
    }

    /** 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">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("Maior Idade:");

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(63, 63, 63)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(94, 94, 94)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(53, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int maior = 0;
        for(int x=1; x<d.length; x++){
            if(d[x].getIdade() > d[maior].getIdade())
                maior = x;
        }

        jTextField1.setText(String.valueOf(d[maior].getIdade()));
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

    void setD(Dados[] d1) {
        for(int x = 0; x < d1.length; x++){
            d[x] = d1[x];
        }
    }

}

ai só usar a classe dados junto…

Edited by Lina –