|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

05-16-2008, 07:51 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
|
Please help me with GUI
Hi, everyone
I am writing a program which reads test questions from a file, and then displays the questions with GUI, and the program also has to listen to the test taker's answer and then score the exam.
The program has five classes
public class Question
{
this class defines constructors, and score exams, display questions, display test, etc
}
public class MultipleChoiceQuestion extends Question
{
}
public class TrueFalseQuestion extends Question
{
}
public class Test
{
in this class, I create an ArrayList of type Question to hold the questions read from file,
the ArrayList class is private
}
public class QuizDriver
{
This is the main class
}
I tried the following two ways to display the questions
1. I tried to get the questions from the ArrayList of the Test class, but as it's of type private, I can not get access to it, can you tell me how to get the questions from the ArrayList, please? I don't want to read the questions directly from the file again(I did it before in the Test class), and if I did that, I would not know what is the total number of questions the test is going to display, and I cannot choose to display questions randomly either.
2. I tried to create the GUI in the Test class, and it turns out that there is no syntax error, but there is nothing being diplayed when I run the program
The GUI I have written in the Test class is as follow, I want to know, is it mandatory that one has to create GUI in the main class? Thank you very much!
public void sampleTest ()
{
JFrame mainWindow = new JFrame("This is a test");
mainWindow.setSize(800, 600);
// Centering the window
Dimension frameSize = mainWindow.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
// Add a test name
Container contentPane = mainWindow.getContentPane();
JLabel testName = new JLabel("Sample Test", JLabel.CENTER);
contentPane.add(testName, BorderLayout.NORTH);
// Read questions from the Question arrayList
for (int i = 0; i < questions.size(); i++)
{
Scanner sc = new Scanner (questions.get(i).getQuestion());
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(questions.size(), 1));
if (questions.get(i).getQuestionType().equals("TF"))
{
// Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
JPanel trueFalsePanel = new JPanel();
trueFalsePanel.setLayout(new GridLayout(3, 1));
String questionText = sc.nextLine();
// Add content to the text fields
JTextField field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + ")");
field1.setEditable(false);
field1.setEnabled(true);
trueFalsePanel.add(field1);
........................... other JTextFields
}
if (questions.get(i).getQuestionType().equals("MC"))
{
// Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
JPanel multipleChoicePanel = new JPanel();
multipleChoicePanel.setLayout(new GridLayout(7,1));
String questionText = sc.nextLine();
String answerChoiceA = sc.nextLine();
String answerChoiceB = sc.nextLine();
String answerChoiceC = sc.nextLine();
String answerChoiceD = sc.nextLine();
JTextField field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + ")");
field4.setEditable(false);
field4.setEnabled(true);
multipleChoicePanel.add(field4);
............. other TextFields
}
|
|

05-16-2008, 10:25 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
I tried to get the questions from the ArrayList of the Test class, but as it's of type private, I can not get access to it, can you tell me how to get the questions from the ArrayList, please?
The usual way is to use a get method to return the list values. Or, if security is a concern, you can make up a new array/list of Question, copy the values of the list into it and return the array/list. This is sometimes referred to as making a defensive copy.
class ReadsFileIntoList {
private List<Question> questions = new ArrayList<Question>();
public List<Question> getQuestions() {
return questions;
}
}
is it mandatory that one has to create GUI in the main class?
No. You have creative license in class design.
|
|

05-16-2008, 11:55 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
Thank you very much
Now I have written the following code to display the test with GUI
I supposed the program should have displayed all the questions it reads from the input file, but it turns out to be just printing the last question of the input file. Can you tell me where I have made the mistake, please? Thank you very much!
public void sampleTest ()
{
JFrame mainWindow = new JFrame("This is a test");
mainWindow.setSize(800, 600);
// Centering the window
Dimension frameSize = mainWindow.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
// Add a test name
Container contentPane = mainWindow.getContentPane();
JLabel testName = new JLabel("Sample Test", JLabel.CENTER);
contentPane.add(testName, BorderLayout.NORTH);
// Read questions from the Question arrayList
for (int i = 0; i < questions.size(); i++)
{
Scanner sc = new Scanner (questions.get(i).getQuestion());
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(questions.size(), 1));
if (questions.get(i).getQuestionType().equals("TF"))
{
// Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
JPanel trueFalsePanel = new JPanel();
trueFalsePanel.setLayout(new GridLayout(3, 1));
String questionText = sc.nextLine();
// Add content to the text fields
JTextField field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
field1.setEditable(false);
field1.setEnabled(true);
trueFalsePanel.add(field1);
JTextField field2 = new JTextField ("" + questionText);
field2.setEditable(false);
field2.setEnabled(true);
trueFalsePanel.add(field2);
JTextField field3 = new JTextField ("Your answer");
field3.setEditable(true);
field3.setEnabled(true);
trueFalsePanel.add(field3);
centerPanel.add(trueFalsePanel); // Add the trueFalsePanel to the centerpanel, and then later add the centerPanel to the center of the JFrame
}
if (questions.get(i).getQuestionType().equals("MC"))
{
// Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
JPanel multipleChoicePanel = new JPanel();
multipleChoicePanel.setLayout(new GridLayout(7,1));
String questionText = sc.nextLine();
String answerChoiceA = sc.nextLine();
String answerChoiceB = sc.nextLine();
String answerChoiceC = sc.nextLine();
String answerChoiceD = sc.nextLine();
JTextField field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
field4.setEditable(false);
field4.setEnabled(true);
multipleChoicePanel.add(field4);
JTextField field5 = new JTextField ("" + questionText);
field5.setEditable(false);
field5.setEnabled(true);
multipleChoicePanel.add(field5);
JTextField field6 = new JTextField ("" + answerChoiceA);
field6.setEditable(false);
field6.setEnabled(true);
multipleChoicePanel.add(field6);
JTextField field7 = new JTextField ("" + answerChoiceB);
field7.setEditable(false);
field7.setEnabled(true);
multipleChoicePanel.add(field7);
JTextField field8 = new JTextField ("" + answerChoiceC);
field8.setEditable(false);
field8.setEnabled(true);
multipleChoicePanel.add(field8);
JTextField field9 = new JTextField ("" + answerChoiceD);
field9.setEditable(false);
field9.setEnabled(true);
multipleChoicePanel.add(field9);
JTextField field10 = new JTextField ("Your answer");
field10.setEditable(true);
field10.setEnabled(true);
multipleChoicePanel.add(field10);
centerPanel.add(multipleChoicePanel);
}
contentPane.add(centerPanel, BorderLayout.CENTER);
}
mainWindow.setVisible(true);
}
|
|

05-17-2008, 12:57 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
// Read questions from the Question arrayList
for (int i = 0; i < questions.size(); i++)
{
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(questions.size(), 1));
...
if (questions.get(i).getQuestionType().equals("TF"))
centerPanel.add(trueFalsePanel);
if (questions.get(i).getQuestionType().equals("MC"))
centerPanel.add(multipleChoicePanel);
...
// When you add a component to a section of a BorderLayout which
// is occupied, the component that previously occupied that section
// is removed and the new component is added. So the last component
// added will show up in the gui.
contentPane.add(centerPanel, BorderLayout.CENTER);
}
Try moving the centerPanel and the contentPane.add call outside of the for loop:
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(questions.size(), 1));
// Read questions from the Question arrayList
for (int i = 0; i < questions.size(); i++)
{
if (questions.get(i).getQuestionType().equals("TF"))
centerPanel.add(trueFalsePanel);
if (questions.get(i).getQuestionType().equals("MC"))
centerPanel.add(multipleChoicePanel);
}
contentPane.add(centerPanel, BorderLayout.CENTER);
|
|

05-17-2008, 01:50 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
|
Thank you so much!
With your help, now the program can display all the questions.
But there is still a problem,
For the mutiple choice problems, it seems that there is not enough space to display them, so that both the question texts and answer choices of mutiple choices problems are very small. Can I do something to add a scroll bar in the side of the window so that all the questions can be displayed in normal size?
Thank you again for your very sincere help!
|
|

05-17-2008, 02:51 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
add a scroll bar ... so that all the questions can be displayed in normal size
contentPane.add(new JScrollPane(centerPanel), BorderLayout.CENTER);
|
|

05-17-2008, 03:20 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
Thank you so much
Now I try to keep track of the test taker's answers, so I need to use a listener, I do this as follow, but the compiler throws me back an error which says:
"not a statement"
for the following line of code
ActionListener 1 = new ActionListener()
The modified method is as follow now
public void sampleTest ()
{
JFrame mainWindow = new JFrame("This is a test");
mainWindow.setSize(800, 600);
// Centering the window
Dimension frameSize = mainWindow.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
// Add a test name
Container contentPane = mainWindow.getContentPane();
JLabel testName = new JLabel("Sample Test", JLabel.CENTER);
contentPane.add(testName, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(questions.size(), 1));
// Read questions from the Question arrayList
for (int i = 0; i < questions.size(); i++)
{
Scanner sc = new Scanner (questions.get(i).getQuestion());
JTextField field1;
JTextField field2;
JTextField field3;
JTextField field4;
JTextField field5;
JTextField field6;
JTextField field7;
JTextField field8;
JTextField field9;
JTextField field10;
if (questions.get(i).getQuestionType().equals("TF"))
{
// Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
JPanel trueFalsePanel = new JPanel();
trueFalsePanel.setLayout(new GridLayout(3, 1));
String questionText = sc.nextLine();
// Add content to the text fields
field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
field1.setEditable(false);
field1.setEnabled(true);
trueFalsePanel.add(field1);
field2 = new JTextField ("" + questionText);
field2.setEditable(false);
field2.setEnabled(true);
trueFalsePanel.add(field2);
field3 = new JTextField ("Your answer");
field3.setEditable(true);
field3.setEnabled(true);
trueFalsePanel.add(field3);
centerPanel.add(trueFalsePanel); // Add the trueFalsePanel to the centerpanel, and then later add the centerPanel to the center of the JFrame
}
if (questions.get(i).getQuestionType().equals("MC"))
{
// Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
JPanel multipleChoicePanel = new JPanel();
multipleChoicePanel.setLayout(new GridLayout(7,1));
String questionText = sc.nextLine();
String answerChoiceA = sc.nextLine();
String answerChoiceB = sc.nextLine();
String answerChoiceC = sc.nextLine();
String answerChoiceD = sc.nextLine();
field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
field4.setEditable(false);
field4.setEnabled(true);
multipleChoicePanel.add(field4);
field5 = new JTextField ("" + questionText);
field5.setEditable(false);
field5.setEnabled(true);
multipleChoicePanel.add(field5);
field6 = new JTextField ("" + answerChoiceA);
field6.setEditable(false);
field6.setEnabled(true);
multipleChoicePanel.add(field6);
field7 = new JTextField ("" + answerChoiceB);
field7.setEditable(false);
field7.setEnabled(true);
multipleChoicePanel.add(field7);
field8 = new JTextField ("" + answerChoiceC);
field8.setEditable(false);
field8.setEnabled(true);
multipleChoicePanel.add(field8);
field9 = new JTextField ("" + answerChoiceD);
field9.setEditable(false);
field9.setEnabled(true);
multipleChoicePanel.add(field9);
field10 = new JTextField ("Your answer");
field10.setEditable(true);
field10.setEnabled(true);
multipleChoicePanel.add(field10);
centerPanel.add(multipleChoicePanel);
}
ActionListener 1 = new ActionListener() // The compiler says this is not a statement
{
public void actionPerformed(ActionEvent e)
{
if (questions.get(i).getQuestionType().equals("TF"))
{
String userAnswer = field3.getText();
if (userAnswer.equals("")
{
String userAnswer = null;
}
answer(i, userAnswer);
}
if (questions.get(i).getQuestionType().equals("MC"))
{
String uswerAnswer = field10.getText();
if (userAnswer.equals(""))
{
String userAnswer = null;
}
answer(i, userAnswer);
}
}
};
field3.addActionListener(1); // Can I use the same listener to listen to two different text fields? field3 and field10
field10.addActionListener(1);
}
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(new JScrollPane(centerPanel), BorderLayout.CENTER);
mainWindow.setVisible(true);
}
Thank you very much
|
|

05-17-2008, 07:16 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
C:\jexp>javac traversepoints.java
traversepoints.java:11: <identifier> expected
ActionListener 1 = new ActionListener() {
^
traversepoints.java:11: invalid method declaration; return type required
ActionListener 1 = new ActionListener() {
^
traversepoints.java:12: illegal start of expression
public void actionPerformed(ActionEvent e) {
^
3 errors
// change the identifier/variable name
ActionListener 1 = new ActionListener() // digit one
// to this
ActionListener l = new ActionListener() // lowerCase L
|
|

05-17-2008, 05:06 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
|
Hi, hardwired
I don't understand why I have to use this, it's a specified word in java, can I use it as a variable name?
And I also don't understand why I have to change the L of ActionListener to lowercase. In our book, it's uppercase.
I did the modification according to your instruction, but the compiler gives me back the same error message:
which reads the ActionListener this = new ActionListener() is
not a statement
I wonder that, it is that I have put the listener in a wrong place of the program or that I made a mistake in creating the listener leads to the error?
I tried to write the listener at both the end of the for loop and the end of each if loop, but every time the compiler gives me the same error message. I write the listener according to what our book does, it's exactly the same(except the digit 1), I cannot figure out the syntax error.
Can you tell me where I have made the mistake, please?
Thank you very much for your help!
|
|

05-17-2008, 07:22 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
Hi,
I run the program in NetBean and it gives me a different error message, the code is as follow now
public void sampleTest ()
{
JFrame mainWindow = new JFrame("This is a test");
mainWindow.setSize(800, 600);
// Centering the window
Dimension frameSize = mainWindow.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
// Add a test name
Container contentPane = mainWindow.getContentPane();
JLabel testName = new JLabel("Sample Test", JLabel.CENTER);
contentPane.add(testName, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(questions.size(), 1));
// Read questions from the Question arrayList
for (int i = 0; i < questions.size(); i++)
{
Scanner sc = new Scanner (questions.get(i).getQuestion());
JTextField field1;
JTextField field2;
JTextField field3;
JTextField field4;
JTextField field5;
JTextField field6;
JTextField field7;
JTextField field8;
JTextField field9;
JTextField field10;
if (questions.get(i).getQuestionType().equals("TF"))
{
// Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
JPanel trueFalsePanel = new JPanel();
trueFalsePanel.setLayout(new GridLayout(3, 1));
String questionText = sc.nextLine();
// Add content to the text fields
field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
field1.setEditable(false);
field1.setEnabled(true);
trueFalsePanel.add(field1);
field2 = new JTextField ("" + questionText);
field2.setEditable(false);
field2.setEnabled(true);
trueFalsePanel.add(field2);
field3 = new JTextField ("Your answer");
field3.setEditable(true);
field3.setEnabled(true);
trueFalsePanel.add(field3);
String userAnswer = field3.getText();
centerPanel.add(trueFalsePanel); // Add the trueFalsePanel to the centerpanel, and then later add the centerPanel to the center of the JFrame
ActionListener listener1 = new ActionListener(){
public void actionPerformed(ActionEvent e){
if (userAnswer.equals(""))// The compiler says userAnswer is local variable and is reached within inner class, needs to be declared as final {
userAnswer = null;
}
answer(i, userAnswer);
}
};
field3.addActionListener(listener1);
}
if (questions.get(i).getQuestionType().equals("MC"))
{
// Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
JPanel multipleChoicePanel = new JPanel();
multipleChoicePanel.setLayout(new GridLayout(7,1));
String questionText = sc.nextLine();
String answerChoiceA = sc.nextLine();
String answerChoiceB = sc.nextLine();
String answerChoiceC = sc.nextLine();
String answerChoiceD = sc.nextLine();
field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
field4.setEditable(false);
field4.setEnabled(true);
multipleChoicePanel.add(field4);
field5 = new JTextField ("" + questionText);
field5.setEditable(false);
field5.setEnabled(true);
multipleChoicePanel.add(field5);
field6 = new JTextField ("" + answerChoiceA);
field6.setEditable(false);
field6.setEnabled(true);
multipleChoicePanel.add(field6);
field7 = new JTextField ("" + answerChoiceB);
field7.setEditable(false);
field7.setEnabled(true);
multipleChoicePanel.add(field7);
field8 = new JTextField ("" + answerChoiceC);
field8.setEditable(false);
field8.setEnabled(true);
multipleChoicePanel.add(field8);
field9 = new JTextField ("" + answerChoiceD);
field9.setEditable(false);
field9.setEnabled(true);
multipleChoicePanel.add(field9);
field10 = new JTextField ("Your answer");
field10.setEditable(true);
field10.setEnabled(true);
multipleChoicePanel.add(field10);
centerPanel.add(multipleChoicePanel);
}
}
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(new JScrollPane(centerPanel), BorderLayout.CENTER);
mainWindow.setVisible(true);
}
The compiler says I need to declare field3 as final. But since its value changes as user types in new content, so I cannot declare it as final, can you tell me what I should do here, please?
Thank you very much!
|
|

05-17-2008, 11:29 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
Now I have solved this problem, but I have also encounter a new one
I try to display the questions one by one, and use the previous and forward buttons to move back and forward in the test, I want the program reads the user answer before showing the next question.
My program now shows all the questions in a single time, and althought I have added a listener to the answer text field, the program still keeps displaying all the questions in a single time. Should I add a button as a signal that tells the program when it is allowed to display the next question?
And I also want to know, if I want to establish a back and forward function for the GUI, should I modify the for loop in the GUI? Because I think with a for loop, I can only display a next question, but cannot display a previous question.
I am sorry for asking so many questions at a single time.
Thank you very much for your help!
The code is as follow now
public void sampleTest ()
{
JFrame mainWindow = new JFrame("This is a test");
mainWindow.setSize(800, 600);
// Centering the window
Dimension frameSize = mainWindow.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
// Add a test name
Container contentPane = mainWindow.getContentPane();
JLabel testTitle = new JLabel("Sample Test", JLabel.CENTER);
contentPane.add(testTitle, BorderLayout.NORTH);
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(questions.size(), 1));
// Read questions from the Question arrayList
for (i = 0; i < questions.size(); i++)
{
Scanner sc = new Scanner (questions.get(i).getQuestion());
if (questions.get(i).getQuestionType().equals("TF"))
{
// Create a new Panel to hold a true false question, and then add this panel to the centerPanel later
JPanel trueFalsePanel = new JPanel();
trueFalsePanel.setLayout(new GridLayout(3, 1));
String questionText = sc.nextLine();
// fill the text fields
field1 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
field1.setEditable(false);
field1.setEnabled(true);
trueFalsePanel.add(field1);
field2 = new JTextField ("" + questionText);
field2.setEditable(false);
field2.setEnabled(true);
trueFalsePanel.add(field2);
field3 = new JTextField ("Your answer");
field3.setEditable(true);
field3.setEnabled(true);
trueFalsePanel.add(field3);
centerPanel.add(trueFalsePanel); // Add the trueFalsePanel to the centerpanel, and then later add the centerPanel to the center of the JFrame
ActionListener listener1 = new ActionListener(){
public void actionPerformed(ActionEvent e){
userAnswer = field3.getText();
if (userAnswer.equals(""))
{
userAnswer = null;
}
answer(i, userAnswer);
}
};
field3.addActionListener(listener1);
}
if (questions.get(i).getQuestionType().equals("MC"))
{
// Create a new Panel to hold a multiple choice question, and then add this panel to the centerPanel later
JPanel multipleChoicePanel = new JPanel();
multipleChoicePanel.setLayout(new GridLayout(7,1));
String questionText = sc.nextLine();
String answerChoiceA = sc.nextLine();
String answerChoiceB = sc.nextLine();
String answerChoiceC = sc.nextLine();
String answerChoiceD = sc.nextLine();
field4 = new JTextField ("" + (i+1) + ". (" + questions.get(i).getPointsValue() + " points)");
field4.setEditable(false);
field4.setEnabled(true);
multipleChoicePanel.add(field4);
field5 = new JTextField ("" + questionText);
field5.setEditable(false);
field5.setEnabled(true);
multipleChoicePanel.add(field5);
field6 = new JTextField ("" + answerChoiceA);
field6.setEditable(false);
field6.setEnabled(true);
multipleChoicePanel.add(field6);
field7 = new JTextField ("" + answerChoiceB);
field7.setEditable(false);
field7.setEnabled(true);
multipleChoicePanel.add(field7);
field8 = new JTextField ("" + answerChoiceC);
field8.setEditable(false);
field8.setEnabled(true);
multipleChoicePanel.add(field8);
field9 = new JTextField ("" + answerChoiceD);
field9.setEditable(false);
field9.setEnabled(true);
multipleChoicePanel.add(field9);
field10 = new JTextField ("Your answer");
field10.setEditable(true);
field10.setEnabled(true);
multipleChoicePanel.add(field10);
centerPanel.add(multipleChoicePanel);
}
}
contentPane.add(centerPanel, BorderLayout.CENTER);
contentPane.add(new JScrollPane(centerPanel), BorderLayout.CENTER);
mainWindow.setVisible(true);
}
}
|
|

05-18-2008, 05:38 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
|
Check out the CardLayout as one option.
Another option is to set up your gui to show only one question (vis-a-vis adding all in panels at construction). Change the question according to user selection with the forward/back buttons.
|
|

05-18-2008, 09:33 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
|
hardwired wrote
Another option is to set up your gui to show only one question (vis-a-vis adding all in panels at construction). Change the question according to user selection with the forward/back buttons.
I don't understand, can you tell me what is vis-a-vis ?
Thank you very much
|
|

05-18-2008, 05:42 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,222
|
|
|
vis-a-vis: face-to-face.
One option as opposed to or versus another.
|
|

05-19-2008, 01:24 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 10
|
|
Hi, hardwired
I choose not to use CardLayout, because I don't understand it,
Now I try to use the following method to display the test
First, I add all the components to the mainWindow,
and then, I try to use the setText method to change the text in the question Text Field, does this method work?
I encounter a problem here. When I am declaring listeners, Eclipse tells me that the type new listener should implement ActionPerformed(ActionEvent e)
But all those four listeners are in the same loop, in the same level, I don't understand why there is a problem for the last listener of those four,
Can you explain it to me? Thank you very much!
The exact error message is
The type new ActionListener(){} must implement the inherited abstract method
ActionListener.actionPerformed(ActionEvent)
And the code
public void sampleTest ()
{
JFrame mainWindow = new JFrame("This is a test");
mainWindow.setSize(800,600);
// Centering the mainWindow
Dimension frameSize = mainWindow.getSize();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
mainWindow.setLocation((screenSize.width - frameSize.width)/2, (screenSize.height - frameSize.height)/2);
// Add a test title
Container contentPane = mainWindow.getContentPane();
JLabel testTitle = new JLabel("Sample Test", JLabel.CENTER);
contentPane.add(testTitle, BorderLayout.NORTH);
// This panel holds one question each time, it holds the questionPanel and answerPanel
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridLayout(2,1));
// This panel tells the test takers their final scores
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(3,1));
// This panel holds a question and will be added to centerPanel, grid (1,1)
JPanel questionPanel = new JPanel();
questionPanel.setLayout(new GridLayout(5,1));
// This panel allows the test takers to enter their answers, hit submit button, previous, and next buttons
JPanel answerPanel = new JPanel();
answerPanel.setLayout(new GridLayout(3,1));
// Set up a white text field first, and then use this text field to hold a question, use the setText() method
// in the for loop to switch to a new question as the user hits the previous or next button, so that
// the program can display only one question each time
questionField = new JTextField();
questionField.setEditable(false);
questionField.setEnabled(true);
questionPanel.add(questionField);
// Add answer choice A to the questionPanel, for true or false question, it's the true choice
choiceField1 = new JTextField();
choiceField1.setEditable(false);
choiceField1.setEnabled(true);
questionPanel.add(choiceField1);
// Add answer choice B, for true or false question, it's choice false
choiceField2 = new JTextField();
choiceField2.setEditable(false);
choiceField2.setEnabled(true);
questionPanel.add(choiceField2);
// Add answer choice C, for true or false question, it's blank
choiceField3 = new JTextField();
choiceField3.setEditable(false);
choiceField3.setEnabled(true);
questionPanel.add(choiceField3);
// Add answer choice D, for true or false question, it's blank
choiceField4 = new JTextField();
choiceField4.setEditable(false);
choiceField4.setEnabled(true);
questionPanel.add(choiceField4);
// Add the questionPanel to the centerPanel, grid(1,1)
centerPanel.add(questionPanel);
// Here the users type in their answers
answerField = new JTextField("Your answer");
answerField.setEditable(true);
answerField.setEnabled(true);
answerPanel.add(answerField);
// Create a submit button
submit = new JButton("Submit Answer");
answerPanel.add(submit);
// Use a new panel to hold the previous and forward buttons
JPanel pf = new JPanel();
pf.setLayout(new GridLayout(1,2));
// The previous button
previousButton = new JButton("Previous");
pf.add(previousButton);
// The forward button
forwardButton = new JButton("Next");
pf.add(forwardButton);
// Add the pfPanel to the answerPanel
answerPanel.add(pf);
// Add answerPanel to centerPanel
centerPanel.add(answerPanel);
// Add the centerPanel to main window
contentPane.add(centerPanel, BorderLayout.CENTER);
// Tell the test takers their grades
scoreField = new JTextField();
scoreField.setEditable(false);
scoreField.setEnabled(true);
southPanel.add(scoreField);
// A user can hit the finish button to end the test
finishButton = new JButton("Finish");
southPanel.add(finishButton);
contentPane.add(southPanel, BorderLayout.SOUTH);
// Set up a for loop or while loop to make the program display questions one by one
currentQ = 0;
while (currentQ < questions.size())
{
queText = questions.get(currentQ).getQuestion();
queType = questions.get(currentQ).getQuestionType();
Scanner sc = new Scanner(queText);
if (queType.equals("TF"))
{
// Read in question to the Text Fields
questionField.setText(sc.nextLine());
choiceField1.setText(sc.nextLine());
choiceField2.setText(sc.nextLine());
choiceField3.setText("");
choiceField4.setText("");
}
if (queType.equals("MC"))
{
questionField.setText(sc.nextLine());
choiceField1.setText(sc.nextLine());
choiceField2.setText(sc.nextLine());
choiceField3.setText(sc.nextLine());
choiceField4.setText(sc.nextLine());
}
// Create listeners for the answerField, previous, and next buttons
ActionListener answerListener = new ActionListener(){
public void actionPerformed(ActionEvent e1){
userAnswer = answerField.getText();
}
};
answerField.addActionListener(answerListener);
// Submit listener
ActionListener submitListener = new ActionListener(){
public void actionPerformed(ActionEvent e2){
answer(currentQ, userAnswer);
}
};
submit.addActionListener(submitListener);
// Previous button ActionListener
ActionListener previousListener = new ActionListener(){
public void actionPerformed(ActionEvent e3){
if (currentQ > 0)
{
currentQ--;
queText = questions.get(currentQ).getQuestion();
queType = questions.get(currentQ).getQuestionType();
Scanner preSc = new Scanner(queText);
if (queType.equals("TF"))
{
questionField.setText(preSc.nextLine());
choiceField1.setText(preSc.nextLine());
choiceField2.setText(preSc.nextLine());
choiceField3.setText("");
choiceField4.setText("");
answerField.setText("Your answer");
}
if (queType.equals("MC"))
{
questionField.setText(preSc.nextLine());
choiceField1.setText(preSc.nextLine());
choiceField2.setText(preSc.nextLine());
choiceField3.setText(preSc.nextLine());
choiceField4.setText(preSc.nextLine());
answerField.setText("Your answer");
}
}
}
};
previousButton.addActionListener(previousListener);
// Next button ActionListener
ActionListener forwardListener = new ActionListener(){
public void ActionPerformed(ActionEvent e4){
if (currentQ < questions.size())
{
currentQ++;
queText = questions.get(currentQ).getQuestion();
queType = questions.get(currentQ).getQuestionType();
Scanner forSc = new Scanner(queText);
if (queType.equals("TF"))
{
questionField.setText(forSc.nextLine());
c
| | |