0

我正在为学校的 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);
}
}
4

1 回答 1

2

第一的:

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

  1. ObjectOutputStream(FileOutputStream("blah")) API
  2. PrintWriter("blah") API

第二:

三个或更多;使用一个for。

这是我在阅读JTextField A = new JTextField();和阅读时想到的JTextField B = new JTextField();

于 2012-11-17T02:31:32.780 回答