Search code examples
javaappletjapplet

JApplet constructor giving me compilation errors


I have made a blueprint of an Applet with functionality yet to insert. So, I instantiated arrays of various types in the class and then further instantiated their type in the init() function. But then, I changed the instantiation of the arrays in the constructor GPACalculator and it is giving me 70 Compilation Errors. I checked the opening and closing brackets beforehand and still it remains unresolved.

Errors are of many types. Some of them are as follows:

1. Illegal start of expression or type

2. Not a statement

3. Particular character expected

4. class interface or enum expected

Errors start from the line where I assigned the String[] description it's values.

Here is my program:

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();
    }
}

Solution

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

    should do the trick