2010-11-04 5 views
0

Je suis un débutant ici, mais j'ai eu affaire à ce problème inconnu pendant un certain temps maintenant. S'il vous plaît voir ce programme simple que j'ai codé et expliquer pourquoi il ne compilera pas pour moi. La chose est que j'ai copié ce code d'un enseignant et cela fonctionne très bien sur sa machine. Puis quand j'ai essayé de lancer l'exemple sur ma machine, j'ai 13 erreurs!Java Erreurs pour le programme simple arrêt du processus de compilation, s'il vous plaît aider

Voici la première classe:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

class radio extends JFrame{ 

private JTextField tf; 
private Font pf; 
private Font bf; 
private Font itf; 
private Font bif; 
private JRadioButton pb; 
private JRadioButton bb; 
private JRadioButton ib; 
private JRadioButton bib; 
private ButtonGroup group; 

public radio(){ 
    super("raido buttonseses"); 
    setLayout(new FlowLayout()); 

    tf = new JTextField("buggedy buggedy boo", 25); 
    add(tf); 

    pb = new JRadioButton("plain", true); 
    bb = new JRadioButton("bold", false); 
    ib = new JRadioButton("italic", false); 
    bib = new JRadioButton("bold and italic", false); 

    add(pb); 
    add(bb); 
    add(ib); 
    add(bib); 

    group = new ButtonGroup(); 
    group.add(pb); 
    group.add(bb); 
    group.add(ib); 
    group.add(bib); 


    pf = new Font("Serif", Font.PLAIN, 14); 
    bf = new Font("Serif", Font.BOLD, 14); 
    itf = new Font("Serif", Font.ITALIC, 14); 
    bif = new Font("Serif", Font.BOLD + Font.ITALIC, 14); 
    tf.setFont(pf); 

    //wait for event to happen pass in font obj to constructor 
    pb.addItemListener(new HandlerClass(pf)); 
    bb.addItemListener(new HandlerClass(bf)); 
    ib.addItemListener(new HandlerClass(itf)); 
    bib.addItemListener(new HandlerClass(bif)); 





} 


    private class HandlerClass implements ItemListener{ 

    private Font font; 
    // font obj gets variable font 
    public HandlerClass(Font f){ 
    font = f; 


    } 

    //sets font to font obj that was passed in 
    public void itemStateChanged(ItemEvent event){ 
    tf.setFont(font); 


} 


} 

    } 

Ensuite, voici la deuxième classe principale que je suis en train de courir:

import javax.swing.JFrame; 

public class radiobutton{ 
public static void main(String[] args) { 

    radio go = new radio(); 
    go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    go.setSize(300,200); 
    go.setVisible(true); 

} 


} 

Je sais que quelqu'un regarde cela et penser que la la réponse est si évidente, mais pour un débutant comme moi, ce n'est pas très clair. Voici les erreurs que je dois me Renvoyé quand je tente de compiler:

javac radiobutton.java 
./JFrame.java:1: JFrame is already defined in this compilation unit 
import javax.swing.JFrame; 
^ 
radiobutton.java:7: cannot find symbol 
symbol : method setDefaultCloseOperation(int) 
location: class radio 
      go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      ^
radiobutton.java:8: cannot find symbol 
symbol : method setSize(int,int) 
location: class radio 
      go.setSize(300,200); 
      ^
radiobutton.java:9: cannot find symbol 
symbol : method setVisible(boolean) 
location: class radio 
      go.setVisible(true); 
      ^
./JFrame.java:8: cannot find symbol 
symbol : variable EXIT_ON_CLOSE 
location: class JFrame 
      go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
              ^
./radio.java:19: cannot find symbol 
symbol : constructor JFrame(java.lang.String) 
location: class JFrame 
      super("raido buttonseses"); 
      ^
./radio.java:20: cannot find symbol 
symbol : method setLayout(java.awt.FlowLayout) 
location: class radio 
      setLayout(new FlowLayout()); 
      ^
./radio.java:22: cannot find symbol 
symbol : class JTExtField 
location: class radio 
      tf = new JTExtField("buggedy buggedy boo", 25); 
        ^
./radio.java:23: cannot find symbol 
symbol : method add(javax.swing.JTextField) 
location: class radio 
      add(tf); 
      ^
./radio.java:30: cannot find symbol 
symbol : method add(javax.swing.JRadioButton) 
location: class radio 
      add(pb); 
      ^
./radio.java:31: cannot find symbol 
symbol : method add(javax.swing.JRadioButton) 
location: class radio 
      add(bb); 
      ^
./radio.java:32: cannot find symbol 
symbol : method add(javax.swing.JRadioButton) 
location: class radio 
      add(ib); 
      ^
./radio.java:33: cannot find symbol 
symbol : method add(javax.swing.JRadioButton) 
location: class radio 
      add(bib); 
      ^
13 errors 

Je pense que cela a quelque chose à voir avec l'astérisque dans l'importation des fonctions java. Suis-je sur la bonne voie ici? Merci d'avoir jeté un coup d'oeil à ce problème stupide et toute aide est très appréciée.

+0

Il compile parfaitement pour moi, essayez d'exécuter java -version, nous dire la sortie –

Répondre

0

Ceci est une photo dans le noir car je n'ai pas touché Java depuis un moment, mais compilez-vous radio.java, puis compilez radiobutton.java?

+0

Vous êtes tous fantastiques! Merci pour votre aide. Cela me remet sur la bonne voie maintenant. :) – Presto

1

Il se compile très bien pour moi.

L'erreur est probablement la suivante: Vous avez un autre fichier appelé JFrame.java dans votre répertoire source. Supprimer ce fichier! JFrame est déjà défini dans l'API.

Le message d'erreur ...

./JFrame.java:1: JFrame is already defined in this compilation unit 
import javax.swing.JFrame; 

... révèle que ./JFrame.java existe dans le même paquet que radiobutton.

(Comme une note de côté, je tiens à mentionner que selon les conventions de codage Java, vous devez toujours commencer vos noms de classe avec une lettre majuscule.)

+1

Devinez vous avez raison. On dirait qu'il y a un 'JFrame.java' personnalisé dans le même paquet/répertoire. –

0
./JFrame.java:8: cannot find symbol 
symbol : variable EXIT_ON_CLOSE 
location: class JFrame 
      go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

Ce message d'erreur fait référence à un autre fichier source (JFrame.java). Comme aioobe, je parie que vous avez un fichier nommé JFrame.java dans le même répertoire (package).

Pour tester, essayez ce code:

// no import statement here! 

public class radiobutton{ 
    public static void main(String[] args) { 
     radio go = new radio(); 
     go.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE); 
     //      ^changed^
     go.setSize(300,200); 
     go.setVisible(true);  
    } 
} 
Questions connexes