这个项目是jpanel中的java测验。在尝试编译程序时,我收到此错误。我的gidlayout似乎有问题,但我在另一个程序中使用它并且它完美地工作。
错误:
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);
}
}
我很困惑为什么会这样。我几乎为另一个项目做了同样的事情并且它工作,并且没有打破gridlayout。
答案 0 :(得分:2)
您在代码中传递了五个参数给createEmptyBorder:
mainPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap, ebGap));
答案 1 :(得分:2)
1)new GridLayout(0, 1, 0, 10, 0)
GridLayout有only four parameters
2)EmptyBorder(int,int,int,int,int)
EmptyBorder有only four parameters
答案 2 :(得分:2)
您将五个int值传递给GridLayout构造函数。我检查了API,并且没有构造函数需要五个值 - 有一个需要四个。