JApplet构造函数给我编译错误

时间:2015-11-10 08:17:29

标签: java applet japplet

我已经制作了一个Applet的蓝图,其功能尚未插入。因此,我在类中实例化了各种类型的数组,然后在init()函数中进一步实例化了它们的类型。但后来,我在构造函数GPACalculator中更改了数组的实例化,它给了我 70编译错误。我事先检查了开始和结束括号,但仍然没有解决。

错误有很多种类。其中一些如下:

1。非法开始表达类型

2。不是声明

第3。预期特定字符

4。 class interface或enum expected

错误从我指定String[] description值的行开始。

这是我的计划:

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

1 个答案:

答案 0 :(得分:1)

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

应该做的伎俩