Запись в текстовый файл через графический интерфейс Java - PullRequest
0 голосов
/ 26 июля 2011

Я создал графический интерфейс Java, который работает как вопросник из 6 вопросов. Для каждого из шести вопросов у меня есть 4 варианта: i, ii, iii, iv, чтобы пользователь мог выбрать, а затем введите один из четырех вариантов в текстовом поле, затем нажмите кнопку «ввод», чтобы перейти к Следующий вопрос. После ответа на все шесть вопросов он перейдет на конечную страницу.

Я хочу записать входное значение (в текстовом поле) каждого вопроса в текстовый файл. Поэтому после нажатия на кнопку «Enter», чтобы ответить на все шесть вопросов, я вижу что-то вроде «i ii iii i ii iii» в текстовом файле.

Есть ли способ сделать это?

Вот мой код:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class HTML extends Applet implements ActionListener
{
    private TextField question;
    private Button enter, start;
    int count = 0;
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    String text, input;

    private Label intro1, intro2;
    private Label qone1, qone2, qone3, qone4, qone5, qone6;
    private Label qtwo1, qtwo2, qtwo3, qtwo4, qtwo5, qtwo6;
    private Label qthree1, qthree2, qthree3, qthree4, qthree5, qthree6;
    private Label qfour1, qfour2, qfour3, qfour4, qfour5, qfour6;
    private Label qfive1, qfive2, qfive3, qfive4, qfive5, qfive6;
    private Label qsix1, qsix2, qsix3, qsix4, qsix5, qsix6;
    private Label finish1, finish2, finish3;

    public void init()
    {
        setLayout(null);
        start = new Button ("Start");
        question = new TextField(10);
        enter = new Button ("Enter");
        if (count == 0)
        {
            setBackground( Color.yellow);
            intro1 = new Label("Target Advertising", Label.CENTER);
            intro1.setFont(new Font("Times-Roman", Font.BOLD, 16));
            intro2 = new Label("Welcome to this questionnaire. In order to show the most appropriate advertisement, we would like to know more about your personal preferences.");
            add(intro1);
            add(intro2);
            intro1.setBounds(0,0,800,20);
            intro2.setBounds(15,20,800,20);
            add(start);
            start.setBounds(370,60,70,23);
            start.addActionListener(this);
        }
        if(count == 1)
        {
            setBackground( Color.yellow );
            qone1 = new Label("Question 1", Label.LEFT);
            qone1.setFont(new Font("Times-Roman", Font.BOLD, 16));
            qone2 = new Label("On average, How many hours do you spend on playing sports every week?");
            qone3 = new Label("i.0-2");
            qone4 = new Label("ii.3-6");
            qone5 = new Label("iii.7-10");
            qone6 = new Label("iv.10+");


            add(qone1);
            add(qone2);
            add(qone3);
            add(qone4);
            add(qone5);
            add(qone6);


            qone1.setBounds(15,0,800,20);
            qone2.setBounds(15,20,800,15);
            qone3.setBounds(15,60,800,15);
            qone4.setBounds(15,80,800,15);
            qone5.setBounds(15,100,800,15);
            qone6.setBounds(15,120,800,15);


            add(question);
            add(enter);
            question.setBounds(15,140,70,15);
            enter.setBounds(90,140,110,23);
            question.addActionListener(this);
            enter.addActionListener(this);
        }
        if (count == 2)
        {


            qtwo1 = new Label("Question 2", Label.LEFT);
            qtwo1.setFont(new Font("Times-Roman", Font.BOLD, 16));
            qtwo2 = new Label("On average, How many hours do you spend on qsixening to music every week?");
            qtwo3 = new Label("i. 0-4 ");
            qtwo4 = new Label("ii. 5-10");
            qtwo5 = new Label("iii. 11-20");
            qtwo6 = new Label("iv. 20+");


            add(qtwo1);
            add(qtwo2);
            add(qtwo3);
            add(qtwo4);
            add(qtwo5);
            add(qtwo6);


            qtwo1.setBounds(15,20,800,15);
            qtwo2.setBounds(15,40,800,15);
            qtwo3.setBounds(15,60,800,15);
            qtwo4.setBounds(15,80,800,15);
            qtwo5.setBounds(15,100,800,15);
            qtwo6.setBounds(15,120,800,15);


            add(question);
            add(enter);
            question.setBounds(15,140,70,15);
            enter.setBounds(90,140,110,23);
            question.addActionListener(this);
            enter.addActionListener(this);
        }
        if(count == 3)
        {


            qthree1 = new Label("Question 3", Label.LEFT);
            qthree1.setFont(new Font("Times-Roman", Font.BOLD, 16));
            qthree2 = new Label("On average, How many hours do you spend on using computers every week?");
            qthree3 = new Label("i.0-2");
            qthree4 = new Label("ii.3-10");
            qthree5 = new Label("iii.11-15");
            qthree6 = new Label("iv.20+");


            add(qthree1);
            add(qthree2);
            add(qthree3);
            add(qthree4);
            add(qthree5);
            add(qthree6);


            qthree1.setBounds(15,20,800,20);
            qthree2.setBounds(15,40,800,15);
            qthree3.setBounds(15,60,800,15);
            qthree4.setBounds(15,80,800,15);
            qthree5.setBounds(15,100,800,15);
            qthree6.setBounds(15,120,800,15);


            add(question);
            add(enter);
            question.setBounds(15,140,70,15);
            enter.setBounds(90,140,110,23);
            question.addActionListener(this);
            enter.addActionListener(this);
        }
        if(count == 4)
        {


            qfour1 = new Label("Question 4", Label.LEFT);
            qfour1.setFont(new Font("Times-Roman", Font.BOLD, 16));
            qfour2 = new Label("On average, How many hours do you spend on groceries every week?");
            qfour3 = new Label("i.0-2");
            qfour4 = new Label("ii.3-10");
            qfour5 = new Label("iii.11-15");
            qfour6 = new Label("iv.20+");


            add(qfour1);
            add(qfour2);
            add(qfour3);
            add(qfour4);
            add(qfour5);
            add(qfour6);


            qfour1.setBounds(15,20,800,15);
            qfour2.setBounds(15,40,800,15);
            qfour3.setBounds(15,60,800,15);
            qfour4.setBounds(15,80,800,15);
            qfour5.setBounds(15,100,800,15);
            qfour6.setBounds(15,120,800,15);

            add(question);
            add(enter);
            question.setBounds(15,140,70,15);
            enter.setBounds(90,140,110,23);
            question.addActionListener(this);
            enter.addActionListener(this);
        }
        if(count == 5)
        {


            qfive1 = new Label("Question 5", Label.LEFT);
            qfive1.setFont(new Font("Times-Roman", Font.BOLD, 16));
            qfive2 = new Label("On average, How many hours do you spend on watching TV every week?");
            qfive3 = new Label("i.0-2");
            qfive4 = new Label("ii.3-10");
            qfive5 = new Label("iii.11-15");
            qfive6 = new Label("iv.20+");


            add(qfive1);
            add(qfive2);
            add(qfive3);
            add(qfive4);
            add(qfive5);
            add(qfive6);


            qfive1.setBounds(15,20,800,15);
            qfive2.setBounds(15,40,800,15);
            qfive3.setBounds(15,60,800,15);
            qfive4.setBounds(15,80,800,15);
            qfive5.setBounds(15,100,800,15);
            qfive6.setBounds(15,120,800,15);


            add(question);
            add(enter);
            question.setBounds(15,140,70,15);
            enter.setBounds(90,140,110,23);
            question.addActionListener(this);
            enter.addActionListener(this);
        }
        if(count == 6)
        {


            qsix1 = new Label("Question 6", Label.LEFT);
            qsix1.setFont(new Font("Times-Roman", Font.BOLD, 16));
            qsix2 = new Label("On average, How many times do you spend with family every week?");
            qsix3 = new Label("i.0-2");
            qsix4 = new Label("ii.3-10");
            qsix5 = new Label("iii.11-15");
            qsix6 = new Label("iv.20+");


            add(qsix1);
            add(qsix2);
            add(qsix3);
            add(qsix4);
            add(qsix5);
            add(qsix6);


            qsix1.setBounds(15,20,800,15);
            qsix2.setBounds(15,40,800,15);
            qsix3.setBounds(15,60,800,15);
            qsix4.setBounds(15,80,800,15);
            qsix5.setBounds(15,100,800,15);
            qsix6.setBounds(15,120,800,15);


            add(question);
            add(enter);
            question.setBounds(15,140,70,15);
            enter.setBounds(90,140,110,23);
            question.addActionListener(this);
            enter.addActionListener(this);
        }


            finish1 = new Label("Thank You." , Label.CENTER);
            finish1.setFont(new Font("Times-Roman", Font.BOLD, 50));
            finish2 = new Label("Questionnaire Completed.", Label.CENTER);
            finish2.setFont(new Font("Times-Roman", Font.BOLD, 50));

            add(finish1);
            add(finish2);

            finish1.setBounds(0,200,800,60);
            finish2.setBounds(0,300,800,60);
        }
    }

    public void actionPerformed(ActionEvent ae)
    {
        String button = ae.getActionCommand();
        text = question.getText();
        b = 0;
        c = 0;

        if (count == 6)
        {
            input = text.toUpperCase();
            remove(enter);
            remove(question);
            question.setText("");
            remove(qsix1);
            remove(qsix2);
            remove(qsix3);
            remove(qsix4);
            remove(qsix5);
            remove(qsix6);


            if(input.equals("OL"))
            {
                b = 1;
                count = 7;
                init();
            }
            else
            {
                b = 2;
                count = 7;
                init();
            }
        }
        if (count == 5)
        {

            input = text.toUpperCase();
            remove(enter);
            remove(question);
            question.setText("");
            remove(qfive1);
            remove(qfive2);
            remove(qfive3);
            remove(qfive4);
            remove(qfive5);
            remove(qfive6);

            if(input.equals("BR"))
            {
                b = 1;
                count = 6;
                init();
            }
            else
            {
                b = 2;
                count = 6;
                init();
            }
        }
        if (count == 4)
        {
            input = text.toLowerCase();
            remove(enter);
            remove(question);
            question.setText("");
            remove(qfour1);
            remove(qfour2);
            remove(qfour3);
            remove(qfour4);
            remove(qfour5);
            remove(qfour6);


            }
            if(input.equals("no"))
            {
                b = 1;
                count = 5;
                init();
            }
            else
            {
                b = 2;
                count = 5;
                init();
            }
        }
        if (count == 3)
        {
            input = text.toLowerCase();
            remove(enter);
            remove(question);
            question.setText("");
            remove(qthree1);
            remove(qthree2);
            remove(qthree3);
            remove(qthree4);
            remove(qthree5);
            remove(qthree6);

            if(input.equals("black"))
            {
                b = 1;
                count = 4;
                init();
            }
            else
            {
                b = 2;
                count = 4;
                init();
            }
        }
        if (count == 2)
        {
            input = text.toLowerCase();
            remove(enter);
            remove(question);
            question.setText("");
            remove(qtwo1);
            remove(qtwo2);
            remove(qtwo3);
            remove(qtwo4);
            remove(qtwo5);
            remove(qtwo6);


            if(input.equals("yes"))
            {
                b = 1;
                count = 3;
                init();
            }
            else
            {
                b = 2;
                count = 3;
                init();
            }
        }
        if (count == 1)
        {
            input = text.toUpperCase();
            remove(enter);
            remove(question);
            question.setText("");
            remove(qone1);
            remove(qone2);
            remove(qone3);
            remove(qone4);
            remove(qone5);
            remove(qone6);

            if(input.equals("i"))
            {
                b = 1;
                count = 2;
                init();
            }
            else 
            {
                b = 1;
                count = 1;
                init();
            }
        }
        if (count == 0)
        {
            remove(intro1);
            remove(intro2);
            remove(start);
            count = 1;
            init();
        }
        this.validate();
    }
}

1 Ответ

1 голос
/ 26 июля 2011

Конечно, что-то вроде

OutputStream out = new FileOutputStream("/path/to/your/file");
out.write(answer.getBytes());
out.close();
...