java - Java Swing GUI错误

标签 java swing user-interface compiler-errors

我的GUI程序是关于为类(class)编写心理测验。它使用了swing,但是在终端上出现两个错误,我不确定是什么问题。我能知道我的错误是什么吗?

MyGuiProject.java:78: error: ';' expected
    int JLabel scoreK = new JLabel("Your score is " + score + ".");
              ^
MyGuiProject.java:78: error: <identifier> expected
    int JLabel scoreK = new JLabel("Your score is " + score + ".");
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyGuiProject
extends JFrame implements ActionListener
{
    //instance variables

    //JFrame

    private JFrame frame1 = new JFrame("Psychology Quiz");
    private JFrame frame2 = new JFrame("Solutions");

    //JPanel (p1 = panel 1)

    private JPanel p1 = new JPanel();
    private JPanel p2 = new JPanel();
    private JPanel p3 = new JPanel();

    //Fonts (f1 = font 1)

    private Font f1 = new Font("Times New Roman", Font.BOLD, 30);
    private Font f2 = new Font("Arial", Font.ITALIC, 30);

    //Description (d1 = description)

    private JLabel d1 = new JLabel("Psychology - Classical Conditioning");

    //questions (q1 = question 1)

    private JLabel q1 = new JLabel("Any behavior or action is...");

    private JLabel q2 = new JLabel(
        "All mental process associated with thinking, knowing, and remembering is...");

    private JLabel q3 = new JLabel("American psychologist and founder of behaviorism is...");

    //answers (a1 = answer 1)
    private JButton a1 = new JButton("response");
    private JButton a2 = new JButton("reaction");
    private JButton a3 = new JButton("stimulus");

    private JButton b1 = new JButton("recognition");
    private JButton b2 = new JButton("cognition");
    private JButton b3 = new JButton("classical conditioning");

    private JButton c1 = new JButton("John B. Watson");
    private JButton c2 = new JButton("Mr. Morgan");
    private JButton c3 = new JButton("Mr. Ansari");

    //Images

    private ImageIcon image1 = new ImageIcon("ang.jpg");
    private ImageIcon image2 = new ImageIcon("psych.jpg");

    //JMenu

    private JMenuBar ppap = new JMenuBar();

    private JMenu menu1 = new JMenu("Questions");
    private JMenuItem item1 = new JMenuItem("1");
    private JMenuItem item2 = new JMenuItem("2");
    private JMenuItem item3 = new JMenuItem("3");

    //Solutions (s1 = solution 1)

    private JLabel s1 = new JLabel(
        "Answers: 1) response || 2) cognition || 3) John B. Watson (unfortunately)");

    //Another program for adding points and label for adding points

    int score = 0;
    int JLabel scoreK = new JLabel("Your score is " + score + ".");
    ScoreKeeper ang1 = new ScoreKeeper();

    public void angWindow()
    {
        //setting frame

        frame1.setBounds(0, 0, 300, 400);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //addActionListener for JButton and JMenuItem

        a1.addActionListener(this);
        a2.addActionListener(this);
        a3.addActionListener(this);

        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);

        c1.addActionListener(this);
        c2.addActionListener(this);
        c3.addActionListener(this);

        item1.addActionListener(this);
        item2.addActionListener(this);
        item3.addActionListener(this);

        //setting font

        d1.setFont(f1);
        scoreK.setFont(f2);

        //JPanel for questions

        p1.add(a1);
        p1.add(a2);
        p1.add(a3);

        p2.add(b1);
        p2.add(b2);
        p2.add(b3);

        p3.add(c1);
        p3.add(c2);
        p3.add(c3);

        //JMenu on JMenuBar

        ppap.add(menu1);

        //setting frame again

        frame1.setJMenuBar(ppap);
        frame1.add(q1, BorderLayout.SOUTH);
        frame1.add(p1, BorderLayout.NORTH);
        frame1.setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        String command = e.getActionCommand();
        if(command.equals("response"))
            {
                frame1.add(q2, BorderLayout.NORTH);
                frame1.remove(q1);
                frame1.remove(p1);
                frame1.add(p2, BorderLayout.SOUTH);
                score = ang1.trackScore(score);
                frame1.validate();
                frame1.repaint();
            }
        if(command.equals("reaction"))
            {
                frame1.remove(p1);
                frame1.remove(q1);
                frame1.add(p2, BorderLayout.NORTH);
                frame1.add(q2, BorderLayout.SOUTH);
                score = ang1.trackScore(score);
                frame1.validate();
                frame1.repaint();
            }
        if(command.equals("stimulus"))
            {
                frame1.remove(p1);
                frame1.remove(q1);
                frame1.add(p2, BorderLayout.NORTH);
                frame1.add(q2, BorderLayout.SOUTH);
                score = ang1.trackScore(score);
                frame1.validate();
                frame1.repaint();
            }

            if(command.equals("recognition"))
            {
                frame1.add(q2, BorderLayout.NORTH);
                frame1.remove(q2);
                frame1.remove(p2);
                frame1.add(p3, BorderLayout.SOUTH);
                score = ang1.trackScore(score);
                frame1.validate();
                frame1.repaint();
            }
        if(command.equals("cognition"))
            {
                frame1.add(q3, BorderLayout.NORTH);
                frame1.remove(q2);
                frame1.add(p3, BorderLayout.SOUTH);
                frame1.validate();
                frame1.repaint();
            }
        if(command.equals("classical conditioning"))
            {
                frame1.add(q2, BorderLayout.NORTH);
                frame1.remove(q2);
                frame1.remove(p2);
                frame1.add(p3, BorderLayout.SOUTH);
                score = ang1.trackScore(score);
                frame1.validate();
                frame1.repaint();
            }

            if(command.equals("John B. Watson"))
            {
                frame1.remove(q3);
                frame1.remove(p3);
                score = ang1.trackScore(score);
                frame1.validate();
                frame1.repaint();
                frame2.setVisible(true);
            }
        if(command.equals("..."))
            {
                frame1.remove(p3);
                frame1.remove(q3);
                frame1.validate();
                frame1.repaint();
                score = ang1.trackScore(score);
                frame2.setVisible(true);
            }
        if(command.equals("..."))
            {
                frame1.remove(p3);
                frame1.remove(q3);
                frame1.validate();
                frame1.repaint();
                score = ang1.trackScore(score);
                frame2.setVisible(true);
            }

            frame1.remove(scoreK);
            scoreK.setText("Your score is " + score + ".");
            frame1.add(scoreK, BorderLayout.CENTER);
    }

public static void main(String[] args)
  {
    MyGuiProject angBaby = new MyGuiProject();
    angBaby.setWindow();
  }
}


public class ScoreKeeper
{
public int trackScore(int score)
{
    score = score + 2;
    return score;
}
}

最佳答案

这是int还是JLabel?

int JLabel scoreK = new JLabel("Your score is " + score + ".");

尝试
 JLabel scoreK = new JLabel("Your score is " + score + ".");

关于java - Java Swing GUI错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42498863/

相关文章:

java - 将 JComboBox 连接到 JTextField

python - 单击时如何获取形状的标签

r - 观察另一个模块中的事件

java - 无法使用firebase云消息传递在mysql数据库中注册 token

java - JFileChooser 保存对话框建议文件名

java - 为什么 Java 的 SimpleDateFormat 类是非线程安全的?

java - 如何使用 KeyAdapter 检测 CTRL+SHIFT+ANY_KEY?

需要 Python TKinter 线程帮助

Java 安卓 android.view.WindowManager$BadTokenException : Unable to add window

java - 在 Java 中将 Collection<SubClass> 传递到期望 Collection<BaseClass> 的函数失败