2015-11-10 1 views
0

J'ai fait un plan d'une applet avec fonctionnalité à insérer. J'ai donc instancié des tableaux de différents types dans la classe, puis instancié leur type dans la fonction init(). Mais alors, j'ai changé l'instanciation des tableaux dans le constructeur GPACalculator et il me donne 70 erreurs de compilation. J'ai vérifié les supports d'ouverture et de fermeture à l'avance et encore il reste non résolu.Constructeur JApplet me donnant des erreurs de compilation

Les erreurs sont de plusieurs types. Certains d'entre eux sont les suivants:

1. début illégale d'expression ou Type

2. Pas une déclaration

3. particulier le caractère attendu

4. interface de classe ou énumération attendue

Les erreurs commencent à partir de la ligne où j'ai attribué les String[] description ses valeurs.

Voici mon programme:

import java.awt.BorderLayout; 
import java.awt.FlowLayout; 
import java.awt.GridLayout; 
import javax.swing.JApplet; 
import javax.swing.JButton; 
import javax.swing.JComboBox; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 
import javax.swing.SpringLayout; 
import javax.swing.SwingUtilities; 
import javax.swing.border.TitledBorder; 

public class GPACalculator extends JApplet { 

    private JButton[] button; //initially instantiated here instead of the constructor 
    private JComboBox[] c; //initially instantiated here instead of the constructor 
    private JFrame frame; 
    private JPanel[] p; //initially instantiated here instead of the constructor 
    private JTextField[] course; //initially instantiated here instead of the constructor 
    private JTextField finalGPA; 
    private JTextField[] points; //initially instantiated here instead of the constructor 
    private SpringLayout s1; 
    private String[] description; //initially instantiated here instead of the constructor 

    public GPACalculator() { 
     button = new JButton[2]; 
     c = new JComboBox[7]; 
     p = new JPanel[9]; 
     course = new JTextField[7]; 
     points = new JTextField[7]; 
     s1 = new SpringLayout(); 

     //error from the next line 

     description = { "Select", "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F" }; 
     p[0] = new JPanel(s1); 
     for(int i=1; i<9; i++) 
      p[i] = new JPanel(new FlowLayout()); 
     prepareGUI(); 
    } 

    public void prepareGUI() { 
     frame = new JFrame("GPA Calculator"); 
     frame.setSize(400, 400); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLayout(new GridLayout(9,1)); 
     frame.setVisible(true); 
    } 

    public void init() { 
     //Execute a job on the event-dispatching thread; creating this applet's GUI. 
     JLabel label1 = new JLabel("<html><br>Course</html>"); 
     JLabel label2 = new JLabel("<html>Credit<br>Points</html>"); 
     JLabel label3 = new JLabel("<html><br>Grade</html>"); 
     p[0].add(label1); 
     p[0].add(label2); 
     p[0].add(label3); 
     s1.putConstraint(SpringLayout.NORTH, label1, 5, SpringLayout.NORTH, p[0]); 
     s1.putConstraint(SpringLayout.NORTH, label2, 5, SpringLayout.NORTH, p[0]); 
     s1.putConstraint(SpringLayout.NORTH, label3, 5, SpringLayout.NORTH, p[0]); 
     s1.putConstraint(SpringLayout.WEST, label1, 100, SpringLayout.WEST, p[0]); 
     s1.putConstraint(SpringLayout.WEST, label2, 143, SpringLayout.WEST, label1); 
     s1.putConstraint(SpringLayout.WEST, label3, 72, SpringLayout.WEST, label2); 
     frame.add(p[0]); 
     for (int i = 0; i<c.length; i++) { 
      course[i] = new JTextField(18); 
      points[i] = new JTextField(6); 
      p[i+1].add(course[i]); 
      p[i+1].add(points[i]); 
      c[i] = new JComboBox(); 
      for(int j=0; j<description.length; j++) { 
       c[i].addItem(description[j]); 
      } 
      p[i+1].add(c[i]); 
      frame.add(p[i+1]); 
     } 
     button[0] = new JButton("Clear All"); 
     button[1] = new JButton("Calculate your GPA"); 
     finalGPA = new JTextField(6); 
     p[8].add(button[0]); 
     p[8].add(button[1]); 
     p[8].add(finalGPA); 
     frame.add(p[8]); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 

     GPACalculator applet = new GPACalculator(); 
     applet.init(); 
    } 
} 
+0

et quelles erreurs est-il donner? – Stultuske

+0

@Stultuske Edited ma question avec la description des erreurs. –

+0

le stacktrace vous indique exactement quelle ligne ces erreurs sont. Avez-vous vérifié ceux-ci? – Stultuske

Répondre

1
description = new String[] { "Select", "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F" }; 

devrait faire l'affaire

+0

L'erreur a disparu :) J'ai donc oublié d'instancier la chaîne [] dans le constructeur, mais cela n'a pas causé de problème lorsque j'ai écrit la même chose dans la classe. Comment venir? –

+1

@ Mr.Robot si vous le déclarez et l'initiez dans la même instruction 'String [] t = {" A "," B "}', alors ceci est une syntaxe valide. Sinon, si vous le déclarez d'abord 'String [] t' et l'initialisez plus tard, vous devez le déclarer comme' t = new String [] {"A", "B"} '. – SomeJavaGuy

+0

Content de savoir. Je vous remercie. Gardera cette chose à l'esprit pour le codage futur :) –