具有Q& A序列化的多选测试GUI

时间:2012-11-17 02:20:59

标签: java swing user-interface persistence

我正在为Java编程中的学校项目工作。我需要设计一个GUI,它将接收问题和答案并将它们存储在一个文件中。它应该能够包含无限数量的问题。我们已经介绍了二进制I / O.

如何编写他们为文件提供的输入?我如何让他们从这个GUI添加多个问题?

package multiplechoice;
import java.awt.*;
import javax.swing.*;

public class MultipleChoice extends JFrame {
     public MultipleChoice() {
   /*
    * Setting Layout
    */
    setLayout(new GridLayout(10,10));

    /*
     * First Question
     */

     add(new JLabel("What is the category of the question?: "));
     JTextField category = new JTextField();
     add(category);

     add(new JLabel("Please enter the question you wish to ask: "));
     JTextField question = new JTextField();
     add(question);

     add(new JLabel("Please enter the correct answer: "));
     JTextField correctAnswer = new JTextField();
     add(correctAnswer);

     add(new JLabel("Please enter a reccomended answer to display: "));
     JTextField reccomendedAnswer = new JTextField(); 
     add(reccomendedAnswer);

     add(new JLabel("Please enter a choice for multiple choice option "
             + "A"));
     JTextField A = new JTextField();
     add(A);

     add(new JLabel("Please enter a choice for multiple choice option "
             + "B"));
     JTextField B = new JTextField();
     add(B);

     add(new JLabel("Please enter a choice for multiple choice option "
             + "C"));
     JTextField C = new JTextField();
     add(C);

     add(new JLabel("Please enter a choice for multiple choice option "
             + "D"));
     JTextField D = new JTextField();
     add(D);

     add(new JButton("Compile Questions"));
     add(new JButton("Next Question"));


 }

public static void main(String[] args) {
    /*
     * Creating JFrame to contain questions
     */
    FinalProject frame = new FinalProject();
   // FileOutputStream output = new FileOutputStream("Questions.dat");


    JPanel panel = new JPanel();

 //   button.setLayout();
 //   frame.add(panel);

    panel.setSize(100,100);

   // button.setPreferredSize(new Dimension(100,100)); 
    frame.setTitle("FinalProject");
    frame.setSize(600, 400);
    frame.setLocationRelativeTo(null); // Center the frame
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}
}

1 个答案:

答案 0 :(得分:2)

首先:

基本上有两种方法可以在java中写入文件。

  1. ObjectOutputStream(FileOutputStream("blah")) API
  2. PrintWriter("blah") API
  3. 第二

      

    三个或更多;使用for。

    在阅读JTextField A = new JTextField();JTextField B = new JTextField();

    时,我会想到这一点