Duvida private static ... frame

1 resposta Resolvido
Emma_Forst

Olá galera como já disse antes sou iniciante e estou com uma dúvida estou trabalhando com o eclipse estava desenvolvendo uma classe swing e estava dando um erro, usando o mecanismo de correção do eclispe o eclise incluiu oum private que estava faltando, o private que estava falatando era esse: (private static GuiLoguin frame; ) a partir dai consegui desenvolver a classe numa boa, Bendito Eclispe, mas eu gostaria de entende-lo para que esse comando serve e porque só ele contém o static em sua assinatura. Segue abaixo código e obrigada!

import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class GuiLoguin extends JFrame {

	private JPanel contentPane;    
	private JTextField tfLoguin;
	private JPasswordField pfSenha;
	private JButton btLogar;
	private JButton btCancelar;
	[b]private static GuiLoguin frame; [/b]


	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() { 
			public void run() {
				try {
					 frame = new GuiLoguin(); 
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public GuiLoguin() {          
		iniciarComponetes();
		definirEventos();
	}


	private void iniciarComponetes(){
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lbLoguin = new JLabel("Loguin:");
		lbLoguin.setBounds(42, 35, 43, 14);
		contentPane.add(lbLoguin);
		
		JLabel lbSenha = new JLabel("Senha:");
		lbSenha.setBounds(44, 81, 41, 14);
		contentPane.add(lbSenha);
		
		tfLoguin = new JTextField();
		tfLoguin.setBounds(95, 32, 247, 20);
		contentPane.add(tfLoguin);
		tfLoguin.setColumns(10);
		
		pfSenha = new JPasswordField();
		pfSenha.setBounds(98, 78, 157, 20);
		contentPane.add(pfSenha);
		
		btLogar = new JButton("Logar");
		btLogar.setBounds(45, 132, 139, 36);
		contentPane.add(btLogar);
		
		btCancelar = new JButton("Cancelar");
		btCancelar.setBounds(212, 132, 147, 36);
		contentPane.add(btCancelar);
	}
	private void definirEventos() {  
		btLogar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				String senha = String.valueOf(pfSenha.getPassword());
				if(tfLoguin.getText().equals("java") && senha.equals("java")){
					frame.setVisible(false); 
					GuiPrincipal.abrir();
				}else{
					JOptionPane.showMessageDialog(null, "Senha errada");
				}
				
			}
		});
		btCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.exit(0);   
			}
		});
	}

}

1 Resposta

Marky.Vasconcelos
Solucao aceita

No seu caso é por que você está usando o valor dessa variavel dentro de um método static (o “static main”).

No seu caso não é necessario guarda-lo statico na classe, você pode declarar dentro do proprio método sem o static.

public static void main(String[] args) {  
        EventQueue.invokeLater(new Runnable() {   
            public void run() {  
                try {  
                    GuiLoguin frame = new GuiLoguin();   
                    frame.setVisible(true);  
                } catch (Exception e) {  
                    e.printStackTrace();  
                }  
            }  
        });  
    }

Caso você precise usar a instancia use-a pelo this.

Criado 13 de julho de 2015
Ultima resposta 13 de jul. de 2015
Respostas 1
Participantes 2