2012-05-08 79 views
-1

該項目是jpanel中的java測驗。當試圖編譯程序時,我得到這個錯誤。我的gidlayout看起來有些問題,但我在另一個程序中使用它,並且它完美地工作。Java編程給出錯誤「找不到符號」

錯誤:

QuizGUI.java:53: cannot find symbol 
symbol : constructor GridLayout(int,int,int,int,int) 
location: class java.awt.GridLayout 
JPanel questionPanel = new JPanel(new GridLayout(0, 1, 0, 10, 0)); 
^ 
QuizGUI.java:89: cannot find symbol 
symbol : method createEmptyBorder(int,int,int,int,int) 
location: class javax.swing.BorderFactory 
mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap, ebGap)); 

代碼:

import java.awt.BorderLayout; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import javax.swing.*; 
import javax.swing.BorderFactory; 
import javax.swing.ButtonGroup; 
import javax.swing.ButtonModel; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.SwingConstants; 

public class QuizGUI{ 

private JPanel mainPanel = new JPanel(); 
private Question[] questions = 
{ 
new Question("What percent is the earth made up of water?","70", new String[] 
{ 
"80", "85", "50", "69" 
}), 
new Question("How many months in a year?", "12", new String[] 
{ 
"1", "Several", "100", "Heck if I know?" 
}), 
new Question("Who was buried in Grant's Tomb?", "Grant", new String[] 
{ 
"Washington", "Jefferson", "Lincoln", "Mickey Mouse" 
}), 
new Question("What's the air-speed velocity of a fully ladden swallow", 
"African or European?", new String[] 
{ 
"100 mi/hr", "25 mi/hr", "50 mi/hr", "-10 mi/hr" 
}), 
new Question("What color was Washington's white horse?", "White", 
new String[] 
{ 
"Blue", "Brown", "Chartreuse", "Mauve" 
}) 
}; 
private QuestionGUI[] questionGuis = new QuestionGUI[questions.length]; 

public QuizGUI() 
{ 
JPanel questionPanel = new JPanel(new GridLayout(0, 1, 0, 10, 0)); 
for (int i = 0; i < questionGuis.length; i++) 
{ 
questionGuis[i] = new QuestionGUI(questions[i]); 
JComponent comp = questionGuis[i].getComponent(); 
comp.setBorder(BorderFactory.createEtchedBorder()); 
questionPanel.add(comp); 
} 


JButton checkAnswersBtn = new JButton("CheckAnswers"); 
checkAnswersBtn.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
int score = 0; 
for (QuestionGUI quest : questionGuis) 
{ 
if (quest.isSelectionCorrect()) 
{ 
score++; 
} 
else 
{ 
System.out.println("For the question: \"" + quest.getQuestion().getQuestion() + "\","); 
System.out.println("\"" + quest.getSelectedString() + "\" is the wrong answer"); 
System.out.println("The correct answer is: \"" + quest.getQuestion().getCorrectAnswer() + "\""); 
} 
} 
System.out.println("Score: " + score); 
} 
}); 
JPanel btnPanel = new JPanel(); 
btnPanel.add(checkAnswersBtn); 

int ebGap = 10; 
mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap, ebGap)); 
mainPanel.setLayout(new BorderLayout()); 
mainPanel.add(questionPanel, BorderLayout.CENTER); 
mainPanel.add(btnPanel, BorderLayout.SOUTH); 
} 

public JComponent getComponent() 
{ 
return mainPanel; 
} 

private static void createAndShowUI() 
{ 
JFrame frame = new JFrame("Quiz"); 
frame.getContentPane().add(new QuizGUI().getComponent()); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.pack(); 
frame.setLocationRelativeTo(null); 
frame.setVisible(true); 
} 

public static void main(String[] args) 
{ 
java.awt.EventQueue.invokeLater(new Runnable() 
{ 
public void run() 
{ 
createAndShowUI(); 
} 
}); 
} 
} 

class QuestionGUI 
{ 
private JPanel mainPanel = new JPanel(); 
private Question question; 
private ButtonGroup buttonGrp = new ButtonGroup(); 

public QuestionGUI(Question question) 
{ 
this.question = question; 

JPanel radioPanel = new JPanel(new GridLayout(1, 0, 10, 0)); 
for (String str : question.getAnswers()) 
{ 
JRadioButton rButton = new JRadioButton(str); 
rButton.setActionCommand(str); 
radioPanel.add(rButton); 
buttonGrp.add(rButton); 
} 
mainPanel.setLayout(new BorderLayout(10, 10)); 
mainPanel.add(new JLabel(question.getQuestion(), SwingConstants.LEFT), 
BorderLayout.NORTH); 
mainPanel.add(radioPanel, BorderLayout.CENTER); 
} 

public Question getQuestion() 
{ 
return question; 
} 

public String getSelectedString() 
{ 
ButtonModel model = buttonGrp.getSelection(); 
if (model != null) 
{ 
return model.getActionCommand(); 
} 
else 
return null; 
} 

public boolean isSelectionCorrect() 
{ 
ButtonModel model = buttonGrp.getSelection(); 
if (model != null) 
{ 
return question.isCorrect(model.getActionCommand()); 
} 
return false; 
} 

public JComponent getComponent() 
{ 
return mainPanel; 
} 
} 

class Question 
{ 
private String question; 
private String answer; 
private List<String> answers = new ArrayList<String>(); 

public Question(String q, String answer, String[] badAnswers) 
{ 
question = q; 
this.answer = answer; 
for (String string : badAnswers) 
{ 
answers.add(string); 
} 
answers.add(answer); 
Collections.shuffle(answers); 
} 

public String getQuestion() 
{ 
return question; 
} 

public String[] getAnswers() 
{ 
return answers.toArray(new String[0]); 
} 

public String getCorrectAnswer() 
{ 
return answer; 
} 

public boolean isCorrect(String selection) 
{ 
return answer.equals(selection); 
} 
} 

我很困惑,爲什麼這是怎麼回事。我幾乎爲另一個項目做了同樣的事情,它工作,沒有網格佈局被打破。

+1

也許這將是比格式化代碼加只放相關的部分。 –

+0

沒有理由恐慌,請仔細閱讀錯誤信息。這是相當具有描述性的,當然,如果你將它與你試圖使用的javadoc/API進行比較 – Robin

回答

2

你傳入五個參數中createEmptyBorder在你的代碼:

mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap, ebGap)); 

但它實際上有四個:http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/BorderFactory.html#createEmptyBorder%28int,%20int,%20int,%20int%29

+0

哦,哇,我沒有看到..非常感謝!但現在我有一個新的錯誤 –

+0

異常在線程「AWT-EventQueue-0」java.lang.NoClassDefFoundError:問題 \t在QuizGUI。 (QuizGUI.java:23) \t在QuizGUI.createAndShowUI(QuizGUI.java:98) \t在QuizGUI.access $ 100(QuizGUI.java:20) \t在QuizGUI $ 2.run(QuizGUI.java:111) \t在java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) \t在java.awt.EventQueue.dispatchEventImpl(EventQueue.java:646) \t在java.awt.EventQueue.access $ 000(EventQueue.java :84) \t在java.awt.EventQueue中$ 1.run(EventQueue.java:607) \t在java.awt.EventQueue中$ 1.run(EventQueue.java:605) \t在java.sec urity.AccessController.doPrivileged(Native方法) –