-1

这个项目是 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);
}
}

我很困惑为什么会这样。我几乎为另一个项目做了同样的事情并且它工作并且没有网格布局被破坏。

4

3 回答 3

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

于 2012-05-08T21:40:00.780 回答
2

1)new GridLayout(0, 1, 0, 10, 0) GridLayout只有四个参数

2) EmptyBorder(int,int,int,int,int) EmptyBorder只有四个参数

于 2012-05-08T21:40:37.500 回答
2

您将五个 int 值传递给 GridLayout 构造函数。我检查了 API,没有构造函数需要五个值 - 有一个需要四个值。

http://docs.oracle.com/javase/6/docs/api/

于 2012-05-08T21:40:58.140 回答