为什么这个java代码不起作用?

时间:2014-06-25 14:11:39

标签: java

我想学习java。我试图制作一个显示txt文件中所有文本的程序,当它读取一个特定的数字但它不起作用时就会停止,我不知道出了什么问题。

这就是我现在所拥有的:

package textLezer;

import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FileData implements Runnable {

public static final int WIDTH = 320;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 3;
public final String NAME = "Text lezer";

public JFrame frame;
public static JLabel label = new JLabel();
public static JPanel panel = new JPanel();

private Thread thread;

public boolean running = false;

public void start() {
    running = true;

    thread = new Thread(this, NAME + "_main");
    thread.start();
}

public void stop() {
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

public void run() {
    String file_name = "text/test.txt";
    try {
        ReadFile file = new ReadFile(file_name);
        String[] aryLines = file.OpenFile();

        for (int i = 0; i < aryLines.length; i++) {
            if (aryLines[i].equals("5")) {
                return;
            } else {
                label.setText("<html>" + label.getText() + " <br> "
                        + aryLines[i] + "</html>");
                System.out.println(aryLines[i]);
            }
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}
}

启动器文件:

package textLezer;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JFrame;

public class Launcher {

private static FileData data = new FileData();

public static void main(String[] args) {

    FileData.panel.setLayout(new FlowLayout());
    FileData.panel.add(FileData.label);

    data.frame = new JFrame();

    data.frame.setTitle(data.NAME);
    data.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    data.frame.setLayout(new BorderLayout());
    data.frame.setSize(800, 400);
    data.frame.add(FileData.panel, BorderLayout.CENTER);
    data.frame.setResizable(false);
    data.frame.setLocationRelativeTo(null);
    data.frame.setVisible(true);

    data.start();
}
}

和txt文件:

1
2
3
4
5
6
7
8

1 个答案:

答案 0 :(得分:0)

代码的问题是你在for循环中使用return;语句。您实际需要的是break;声明

另外,为多行标签创建html的方式也是错误的。它不会工作,因为生成的最终html将是

<html><html><html><html> <br> 1</html> <br> 2</html> <br> 3</html> <br> 4</html>

所以你应该优化你的连接逻辑。

public void run() {
    String file_name = "text/test.txt";
    try {
        ReadFile file = new ReadFile(file_name);
        String[] aryLines = file.OpenFile();

        StringBuilder innerText = new StringBuilder();
        for (int i = 0; i < aryLines.length; i++) {
            if (aryLines[i].equals("5")) {
                break;
            } else {
                innerText.append(aryLines[i]).append("<br/>");
            }
        }
        label.setText("<html>" + innerText.toString() + "</html>");
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}