从txt文件到字符串java的行存储

从txt文件到字符串java的行存储,java,Java,我的程序读取一个txt文件,并在单击Jbutton时显示Jlabel中的每一行 当程序运行并单击Jbutton时,JLabel中不会显示任何内容 我相信这是因为我没有在readFile方法中正确调用字符串行 import java.awt.EventQueue; import java.awt.GridLayout; import java.awt.event.*; import java.io.File; import java.io.FileNotFoundException; import

我的程序读取一个txt文件,并在单击Jbutton时显示Jlabel中的每一行

当程序运行并单击Jbutton时,JLabel中不会显示任何内容

我相信这是因为我没有在readFile方法中正确调用字符串行

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

import javax.swing.*;

public class Driver {

        List<String> lines;

        static String line = "";

        static Scanner scanner = new Scanner(System.in);

        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() {
                lines = readLinesFromFile();
                i = 0;
                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) {
                        jl.setText(lines.get(i));
                        i++;
                        if ( i > lines.size() ) {
                            i = 0;
                        }
                        if (b1.getText().equals("Click For Answer")) {
                            b1.setText("Next Question");
                        } else {
                            b1.setText("Click For Answer");
                        }
                    }
                });


            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 static List<String> readLinesFromFile() {
             List<String> lines = new ArrayList<String>();
            try {
                scanner = new Scanner(new File("upload.txt"));
                if (scanner.hasNext()){
                    lines.add(scanner.nextLine());
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return lines;
        }

        private static void readFile(File file) throws FileNotFoundException{
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
            line = scanner.nextLine();
            }
            scanner.close();
        }


        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        new Driver();
                        readFile(new File("upload.txt"));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
}
导入java.awt.EventQueue;
导入java.awt.GridLayout;
导入java.awt.event.*;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.*;
导入javax.swing.*;
公务舱司机{
列出行;
静态字符串行=”;
静态扫描仪=新扫描仪(System.in);
String s=“欢迎学生!”;
字符串b=“开始!”;
私有jf框架;
私人JPanel p;
JFrame=新JFrame();
JButton b1=新JButton(b);
JLabel jl=新的JLabel;
int i;
私有int;
公共司机(){
gui();
}
公共用户界面(){
lines=readLinesFromFile();
i=0;
f=新JFrame(“闪存卡程序”);
p=新的JPanel();
f、 setLayout(新的GridLayout(2,1));
f、 添加(jl);
f、 加(p);
p、 setLayout(新的GridLayout(2,1));
p、 添加(b1);
jl.设置水平对齐(JLabel.中心);
//包装框架以获得更好的跨平台支撑
f、 包装();
//让它可见
f、 setVisible(真);
f、 setSize(500400);//默认大小为0,0
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
jl.setText(lines.get(i));
i++;
如果(i>lines.size()){
i=0;
}
if(b1.getText().equals(“单击以获取答案”)){
b1.setText(“下一个问题”);
}否则{
b1.setText(“单击以获取答案”);
}
}
});
b1.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
如果(单击+==10){
Object[]选项={“不,谢谢”,“是的,请”};
int response=JOptionPane.showOptionDialog(帧,
“你想要更多的数学问题吗?”,
“数学问题”,作业窗格。是\否\取消\选项,
JOptionPane.QUESTION_消息,空,选项,
期权[1]);
如果(响应==1)
单击=1;//重置
其他的
系统出口(0);
}
}
}); 
}
公共静态列表readLinesFromFile(){
列表行=新的ArrayList();
试一试{
扫描仪=新扫描仪(新文件(“upload.txt”);
if(scanner.hasNext()){
line.add(scanner.nextLine());
}
}catch(filenotfounde异常){
e、 printStackTrace();
}
回流线;
}
私有静态void readFile(File File)引发FileNotFoundException{
扫描仪=新扫描仪(文件);
while(scanner.hasNextLine()){
line=scanner.nextLine();
}
scanner.close();
}
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
新驱动程序();
readFile(新文件(“upload.txt”);
}catch(filenotfounde异常){
e、 printStackTrace();
}
}
});
}
}

如果要使用文本文件中的一行设置jl字段的标签,则需要执行以下操作:

  b1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            jl.setText(lines.get(i));
            i++;
            if ( i > lines.size() ) {
                i = 0;
            }
            if (b1.getText().equals("Click For Answer")) {
                b1.setText("Next Question");
            } else {
                b1.setText("Click For Answer");
            }
        }
    });
  • 在驱动程序类中添加列表字段:

    public class Driver {
       List<String> lines;
       //other fields that you need
    }
    
  • 添加以下方法:

    public static List<String> readLinesFromFile() {
        List<String> lines = new ArrayList<String>();
        try {
            Scanner scanner = new Scanner(new File("upload.txt"));
            if (scanner.hasNext()){
                lines.add(scanner.nextLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return lines;
    }
    
  • 此外,使用此实现,您不再需要line变量,因为您将标签的文本设置为lines字段中的一个值


    注意:如果文件中的行数高于实际行数,则会重置为0。如果您不想这样做,只需更改ActionListener中用于检查大小的If块

    您得到的错误是什么?至少据我所知,这不是真正的错误相关,但既然您在problems方法中再次加载文件,为什么不创建一个通用扫描程序来传递问题。如@zgc7009所述,无需加载文件两次。否则,你就在正确的轨道上了。我想我写的东西会帮助你的。您可能还需要修复缩进,以提高可读性。如果要从
    main
    这样运行它,您还需要将
    问题设置为静态。错误是由于我在main中运行问题的方式造成的。当我将其更改为静态时,它消失了,但是程序编译没有错误,但没有加载到gui。谢谢。我编辑并添加了我的完整程序,以便您可以看到我正在尝试做什么。我需要将线路设置为Jlabel。我使用jl.setText(line)来实现这一点;但是readFile eclipse方法说我没有使用line变量。谢谢你的帮助,你能看一下吗
    
      b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jl.setText(lines.get(i));
                i++;
                if ( i > lines.size() ) {
                    i = 0;
                }
                if (b1.getText().equals("Click For Answer")) {
                    b1.setText("Next Question");
                } else {
                    b1.setText("Click For Answer");
                }
            }
        });