gpt4 book ai didi

java - JApplet 构造函数给我编译错误

转载 作者:行者123 更新时间:2023-12-01 10:55:44 27 4
gpt4 key购买 nike

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

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

<强>1。表达式类型的开头非法

<强>2。不是声明

<强>3。 预期特定字符

<强>4。预期类接口(interface)或枚举

错误从我分配String[]描述它的值的行开始。

这是我的程序:

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" };

应该可以解决问题

关于java - JApplet 构造函数给我编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33625500/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com