2017-02-28 147 views
-2

我的GUI程序是爲我的課寫作心理測驗。它使用了swing,但是我在終端上遇到了兩個錯誤,我不確定問題出在哪裏。我能知道我的錯誤是什麼嗎?Java swing GUI錯誤

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; 
} 
} 
+0

將來你會更仔細地想校對出錯的行。 –

+1

'int JLabel scoreK' ...? – MadProgrammer

+0

刪除int,實例是JLabel而不是int。 – Rudra

回答

1

這是一個int或一個JLabel?

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

嘗試

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

非常感謝。我真的很感謝你的幫助。但是,我現在收到了這條消息。我認爲那部分沒有任何錯誤。 MyGuiProject.java:236:error:can not find symbol \t \t angBaby.setWindow(); \t \t^ 符號:方法setWindow() 位置:類型的變量angBaby MyGuiProject – user7632817

+0

你沒有一個叫method''在 –

+0

這個類setWindow'聲明謝謝你這麼多。我從你的評論中得到了很多幫助。祝你有美好的一天 :) – user7632817