Variaveis sempre elas!

2 respostas
C

Srs. tenho dois formularios o primeiro um Jframe onde crio algumas variaveis que funcionam legalzinho, no segundo formulario um Jpanel eh toh apanhnado,
o objetivo eh utilizar algumas variaveis do Jframe no Jpanel, ai eh mora a encrenca, minha duvida:
como no Jpanel verificar se uma determinada variavel existe, se existir pega os valores e pronto se não existir ai se cria,
mas se no Jpanel digo “naveg = 0”, o netbeans me diz que essa variavel não existe, como resolver?

2 Respostas

felipef

carcara:
Srs. tenho dois formularios o primeiro um Jframe onde crio algumas variaveis que funcionam legalzinho, no segundo formulario um Jpanel eh toh apanhnado,
o objetivo eh utilizar algumas variaveis do Jframe no Jpanel, ai eh mora a encrenca, minha duvida:
como no Jpanel verificar se uma determinada variavel existe, se existir pega os valores e pronto se não existir ai se cria,
mas se no Jpanel digo “naveg = 0”, o netbeans me diz que essa variavel não existe, como resolver?

Sao Classes diferentes???

Se sim, set uma instancia do seu JFrame na classe JPanel, e no JFrame crie os getters e setters necessarios para a suas variaveis, e utilize eles nessa classe que tem o JPanel

Olha um exemplo bem feio

package test.x.y.h;

import javax.swing.JFrame;

/**
 *
 * @author felipevianna
 */
public class MeuJFrame extends JFrame {

    private String minhaVariavel;

    public MeuJFrame() {
        MeuJPanel meuJPanel = new MeuJPanel(this);
    }

    public String getMinhaVariavel() {
        return minhaVariavel;
    }

    public void setMinhaVariavel(String minhaVariavel) {
        this.minhaVariavel = minhaVariavel;
    }
}

package test.x.y.h;

import javax.swing.JPanel;

/**
 *
 * @author felipevianna
 */
public class MeuJPanel extends JPanel {

    private MeuJFrame meuJFrame;
    
    public MeuJPanel(MeuJFrame meuJFrame) {
        this.meuJFrame = meuJFrame;
        
        if (meuJFrame.getMinhaVariavel() == null) {
            meuJFrame.setMinhaVariavel("Meu Texto");
        }
    }
}
C

Obrigado Felipe

Criado 8 de outubro de 2009
Ultima resposta 8 de out. de 2009
Respostas 2
Participantes 2