gpt4 book ai didi

java - 遍历文本文件中的行,显示 Jlabel 中的行

转载 作者:行者123 更新时间:2023-11-30 09:08:38 27 4
gpt4 key购买 nike

我有一个程序可以将 txt 文档的行设置为 Jlabel。单击 JButton 时,它应该显示 txt 文件中的下一行,

问题是程序显示第一行并正常运行,但从不显示另一行。它允许我继续点击按钮但永远不会切换线路......

这是我的代码。

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();
}
}
});
}
}

最佳答案

好的,穷人的调试器来拯救:

public static List<String> readLinesFromFile() {
List<String> lines = new ArrayList<String>();
try {
// !! scanner = new Scanner(new File("upload.txt"));
scanner = new Scanner(new File(UPLOAD_TXT));
if (scanner.hasNext()) {
lines.add(scanner.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}

System.out.println(lines); // ***** I added this
return lines;
}

而且它只读一行。然后我回头看看原因:您使用的是 if block 而不是 while 循环

改变这个:

     if (scanner.hasNext()) {
lines.add(scanner.nextLine());
}

为此:

     // also it should be hasNextLine() not hasNext()
while (scanner.hasNextLine()) {
lines.add(scanner.nextLine());
}

所以教训又是这样的:仅仅因为你的代码可以编译并不意味着它没有错误。使用简单的调试技术,如 println 语句(同样,“穷人的调试器”)通常是查找逻辑错误所需的全部。强大的 Eclipse 调试器可以帮助您找到更多。


顺便说一句,你会想要改变这个:

        if (i > lines.size()) {
i = 0;
}

为此:

        if (i >= lines.size()) {
i = 0;
}

避免数组越界异常。

您的静态 readFile(...) 方法和静态行变量也没有用。

关于java - 遍历文本文件中的行,显示 Jlabel 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23436972/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com