このプロジェクトは、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);
}
}
なぜこれが起こっているのかとても混乱しています。別のプロジェクトでもほぼ同じことをしましたが、うまくいき、グリッドレイアウトが壊れていません。