Ajuda com JFrame

4 respostas
K45T

Olha pessoal Minha apredizagem Melhorou muito quando comprei o livro do Deitel Mais em um de seus exemplos ele faz essa aplicativo para exemplificar o JTextField...

O que acontesse é: quando eu tento copilar o programa ele me retorna a seguinte erro:

C:\java>javac 01.java
01.java:58: non-static variable JFrame cannot be referenced from a static context
JFrame.EXIT_ON_CLOSE );
^
01.java:58: cannot find symbol
symbol : variable EXIT_ON_CLOSE
location: class javax.swing.JTextField
JFrame.EXIT_ON_CLOSE );
^
2 errors

o que esta acontecendo ?

o cod. que eu tento copilar é:
// pacote do núcleo do java


import java.awt.*;
import java.awt.event.*;

// Pacotes de extenção do java
import javax.swing.*;

class TextFieldTest extends JFrame {
    private JTextField JFrame, textField1, textField2, textField3;
        private JPasswordField passwordField;
        
        // configura a GUI
        public TextFieldTest() {
            super( "Testing JTextField and JPasworField");
            
            Container container = getContentPane();
            container.setLayout( new FlowLayout() );
            
            // constroi campo de texto com dimenções default
            textField1 = new JTextField( 10 );
            container.add( textField1 );
            
            // constroi campo de texto cm texto default
            textField2 = new JTextField( "Enter text here" );
            container.add( textField2 );
            
            // constroi campo de texto default e 20 elementos visiveis sem tratador de eventos
            textField3 = new JTextField( "Uneditable text field", 20);
            textField3.setEditable( false );
            container.add( textField3 );
            
            // constrói campo de texto com texto default
           TextFieldHandler handler = new TextFieldHandler();
           textField1.addActionListener( handler );
           textField2.addActionListener( handler );
           textField3.addActionListener( handler );
           passwordField.addActionListener( handler );
           
           setSize( 325, 100 );
           setVisible(true);
    }
        // executa o aplicativo
        public static void main( String args[] ){
            TextFieldTest application = new TextFieldTest();
            
            application.setDefaultCloseOperation( 
                  JFrame.EXIT_ON_CLOSE );
        }
        
        // CLASSE INTERNA PRIVADA PARA O TRATAMENTO DO EVENTO
       private class TextFieldHandler implements ActionListener
    {
       // process text field events
        public void actionPerformed( ActionEvent event )
       {
           String string = ""; // declare string to display

           // user pressed Enter in JTextField textField1
          if ( event.getSource() == textField1 )
             string = String.format( "textField1: %s",
                     event.getActionCommand() );

          // user pressed Enter in JTextField textField2
           else if ( event.getSource() == textField2 )
              string = String.format( "textField2: %s",
                 event.getActionCommand() );

           // user pressed Enter in JTextField textField3
           else if ( event.getSource() == textField3 )
              string = String.format( "textField3: %s",
                 event.getActionCommand() );

           // user pressed Enter in JTextField passwordField
           else if ( event.getSource() == passwordField )
              string = String.format( "passwordField: %s",
                 new String( passwordField.getPassword() ) );

           // display JTextField content
           JOptionPane.showMessageDialog( null, string );

        } // end method actionPerformed
     } // end private inner class TextFieldHandler
  } // end class TextFieldFrame

4 Respostas

ViniGodoy

É impressão minha ou você criou uma variável JTextField e chamou ela de JFrame?

Dá uma olhada na primeira linha da sua classe:

private JTextField JFrame

Quando você está fazendo
JFrame.EXIT_ON_CLOSE

O java está pensando que você está buscando o código dessa variável não estática. Ela não vai ter nem o EXIT_ON_CLOSE (por se tratar de um JTextField) e nem vai estar disponível no main (pois a variável JFrame não é estática).

Mude o nome dela para algo mais intuitivo como:
private JTextField jTextField

fabiel

É isso mesmo cara o problema é na sua varial JTextField.

mude essa variavel e tudo ira funfar direitinho

K45T

obrigado karas deu certo

Luiz_Aguiar

Moderação: tópico movido para Interfaces Gráficas!

Criado 2 de abril de 2007
Ultima resposta 3 de abr. de 2007
Respostas 4
Participantes 4