Any help would be appreciated.
I have a working command line program (trivia question) and decided to try to put it in a Gui. I got the user menu to print, but it gets to line 162 and blows up (going line by line) where I'm thinking it should do getText then parse to an integer and go through the if statements. Our HW's are just command line, but I guess I like punishing myself, I've been working with it for a couple of hours looking at examples but I can't figure it out.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;
import javax.swing.*;
public class Program8Gui extends JPanel implements Serializable,
ContainerListener, ActionListener {
JTextArea display;
JPanel buttonPanel;
JButton gameAdminButton, playTriviaButton, clearButton;
Vector<JButton> buttonList;
static final String ADMIN = "Game Administration";
static final String PLAY = "Play Trivia";
static final String CLEAR = "Clear";
static final String newline = "\n";
public Program8Gui() {
super(new GridBagLayout());
GridBagLayout gridbag = (GridBagLayout) getLayout();
GridBagConstraints c = new GridBagConstraints();
// Initialize an empty list of buttons.
buttonList = new Vector<JButton>(10, 10);
// Create all the components.
gameAdminButton = new JButton("Options");
gameAdminButton.setActionCommand(ADMIN);
gameAdminButton.addActionListener(this);
playTriviaButton = new JButton("Play the Trivia Game");
playTriviaButton.setActionCommand(PLAY);
playTriviaButton.addActionListener(this);
buttonPanel = new JPanel(new GridLayout(1, 1));
buttonPanel.setPreferredSize(new Dimension(200, 75));
buttonPanel.addContainerListener(this);
display = new JTextArea(40, 30);
display.setEditable(true);
JScrollPane scrollPane = new JScrollPane(display);
scrollPane.setPreferredSize(new Dimension(200, 75));
clearButton = new JButton("Clear text area");
clearButton.setActionCommand(CLEAR);
clearButton.addActionListener(this);
c.fill = GridBagConstraints.BOTH; // Fill entire cell.
c.weighty = 1.0; // Button area and message area have equal height.
c.gridwidth = GridBagConstraints.REMAINDER; // end of row
gridbag.setConstraints(scrollPane, c);
add(scrollPane);
c.weighty = 0.0;
gridbag.setConstraints(clearButton, c);
add(clearButton);
c.weightx = 1.0; // Add/remove buttons have equal width.
c.gridwidth = 1; // NOT end of row
gridbag.setConstraints(gameAdminButton, c);
add(gameAdminButton);
c.gridwidth = GridBagConstraints.REMAINDER; // end of row
gridbag.setConstraints(playTriviaButton, c);
add(playTriviaButton);
c.weighty = 1.0; // Button area and message area have equal height.
gridbag.setConstraints(buttonPanel, c);
add(buttonPanel);
setPreferredSize(new Dimension(400, 400));
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
}
void displayMessage(String action, ContainerEvent e) {
display.setText(((JButton) e.getChild()).getText() + " was" + action
+ e.getContainer().getClass().getName() + newline);
display.setCaretPosition(display.getDocument().getLength());
}
/*
* This could have been implemented as two or three classes or objects, for
* clarity.
*/
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (ADMIN.equals(command)) {
// class GameAdmin implements Serializable{
final long serialVersionUID = 2;
Vector<Question> questionList;
int numQuestions = 0;
final String newline = "\n";
// public GameAdmin() {
// read from dat file if it exists
/**
* @param args
*/
Vector<Question> vector = new Vector<Question>();
questionList = vector;
File file = new File("questions.dat");
String fileName = "questions.dat";
if (!file.exists()) {
writeQuestionList(questionList, fileName);
}
Question q;
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(file));
try {
questionList = (Vector<Question>) in.readObject();
in.close();
int questionListSize = questionList.size();
display.setText("Question list size = "
+ questionList.size());
} catch (Exception e1) {
display.setText(e1.toString());
display.setText("No questions are present in the file");
}
} catch (FileNotFoundException e1) {
display.setText("No question file found.");
} catch (IOException e1) {
display.setText("IOException.");
}
// write questions to file
int guess = 0;
String tempQuestion = null;
String tempAnswer = null;
int tempValue = 0;
// Make menu
// while (guess != 4) {
// String string =
// "Menu"+newline+"Please Choose a number"+newline;
display.setText("Menu");
display.append(newline + "Please Choose a number" + newline
+ "1. Print Questions" + newline + "2. Add Question"
+ newline + "3. Delete Question" + newline
+ "4. End Program" + newline);
String s = display.getText();
guess = Integer.parseInt(s);
//Scanner scanner = new Scanner(System.in);
//guess = scanner.nextInt();
// print out questions
if (guess == 1) {
if (questionList.size() <= 0) {
display.append("The question list is empty");
} else {
for (int j = 0; j < questionList.size(); j++) {
Question que = questionList.get(j);
display.append("Question " + j + ". " + que.toString());
}
}
}
// add question
if (guess == 2) {
display.append("Enter a question");
tempQuestion = display.getText();
display.append(newline);
System.out.println("Enter the answer to the question");
tempAnswer = display.getText();
display.append(newline);
System.out.println("Enter the value 1 to 3");
s = display.getText();
tempValue = Integer.parseInt(s);
display.append(newline);
q = new Question(tempQuestion, tempAnswer, tempValue);
questionList.add(q);
numQuestions++;
}
// delete question
if (guess == 3) {
display.append("What question number do you want to delete");
s = display.getText();
display.append(newline);
int j = Integer.parseInt(s);
questionList.remove(j);
numQuestions--;
}
// end program
if (guess == 4) {
writeQuestionList(questionList, fileName);
display.append("End of program.");
display.append(newline);
System.exit(0);
}
}
// }
// }
// }
else if (PLAY.equals(command)) {
int lastIndex = buttonList.size() - 1;
try {
display.append("play the game to be added");
} catch (ArrayIndexOutOfBoundsException exc) {
}
} else if (CLEAR.equals(command)) {
display.setText("");
}
}
@Override
public void componentAdded(ContainerEvent e) {
// TODO Auto-generated method stub
displayMessage(" added to ", e);
}
@Override
public void componentRemoved(ContainerEvent e) {
// TODO Auto-generated method stub
displayMessage(" removed from ", e);
}
public String writeQuestionList(Vector<Question> questionList,
String fileName) {
try {
// File data = new File(fileName);
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("questions.dat"));
out.writeObject(questionList);
return ("Question list saved to file");
} catch (Exception e) {
return ("Error saving to " + fileName + ". Question not saved.\n" + e);
}
}
/**
* Create the GUI and show it.
*
*/
private static void createAndShowGUI() {
// Create and set up the window.
JFrame frame = new JFrame("ContainerEventDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create and set up the content pane.
JComponent newContentPane = new Program8Gui();
newContentPane.setOpaque(true); // content panes must be opaque
frame.setContentPane(newContentPane);
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}// end class program8Gui