设置字符串并使用java将.txt文件中的行循环到JLabel

时间:2014-05-02 17:31:39

标签: java string filereader

我有一个应该从upload.txt

读取行的程序

然后,点击JLabel

时,每行应发布到upload.txt的{​​{1}}行中JButton

我还没有运行该程序,因为我在这里收到错误file(textile); 该行是程序关闭前的主要方法。

我的问题是如何解决此错误并确保我的代码设置有效地实现我的目标?

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import javax.swing.*;

public class Driver {
    String s = "Welcome Students!";
    String b = "Start!";

    private JFrame f;
    private JPanel p;

    JFrame frame = new JFrame();
    JButton b1 = new JButton(b);
    JLabel jl = new JLabel(s);

    int i;
    private int clicked;

    public Driver() {
        gui();
    }

    public void gui() {
        f = new JFrame("Flash Card Program");
        p = new JPanel();

        f.setLayout(new GridLayout(2, 1));
        f.add(jl);
        f.add(p);

        p.setLayout(new GridLayout(2, 1));
        p.add(b1);

        jl.setHorizontalAlignment(JLabel.CENTER);

        // pack the frame for better cross platform support
        f.pack();

        // Make it visible
        f.setVisible(true);
        f.setSize(500, 400); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (b1.getText().equals("Click For Answer")) {
                    jl.setText(problems());
                    b = "Next Question";
                    b1.setText(b);
                } else {
                    jl.setText(problems());
                    b = "Click For Answer";
                    b1.setText(b);
                }
            }
        });

        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (clicked++ == 10) {
                    Object[] options = { "No, thanks", "Yes, please" };

                    int response = JOptionPane.showOptionDialog(frame,
                       "Would you like more math questions? ",
                       "Math Questions", JOptionPane.YES_NO_CANCEL_OPTION,
                       JOptionPane.QUESTION_MESSAGE, null, options,
                       options[1]);

                    if (response == 1)
                       clicked = 1; // reset
                    else
                       System.exit(0);
                }
            }
        });
    }

    public String problems() {
        File file = new File("upload.txt");

        try {
            Scanner sc = new Scanner(file);

            while (sc.hasNextLine()) {
                int i = sc.nextInt();
                System.out.println(i);
            }

            sc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return problems();
    }

    public static void main(String[] args) throws IOException {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Driver();
                Scanner textfile = null;

                try {
                    textfile = new Scanner(new File("upload.txt"));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }

                problems(textfile);
            }
        });
    }
}

0 个答案:

没有答案