[英]JApplet constructor giving me compilation errors
我已經制作了具有功能的Applet藍圖。 因此,我實例化了類中各種類型的數組,然后在init()
函數中進一步實例化了它們的類型。 但是,然后,我在構造函數GPACalculator
更改了數組的實例化,這給了我70編譯錯誤 。 我事先檢查了開括號和閉括號,但仍然無法解決。
錯誤有很多類型。 其中一些如下:
1.表達式或類型的非法開頭
2.沒有聲明
3.預期的特殊字符
4.類接口或枚舉預期
錯誤從我為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();
}
}
description = new String[] { "Select", "A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "D+", "D", "D-", "F" };
應該可以
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.