1

在认为我正在解决使文本(从文件中读取)出现在 JPanel 中的问题之后,我沮丧地回到了原点。

代码如下。结果只是一个空白屏幕 400x500 屏幕。使用 nextLine() + nextLine() 作为 displayText 命令的某些组合会导致文件中出现一个单词(该单词多次不同)。这让我想知道:我需要处理文本换行的代码吗?文本文件本身是分段的,因此,我认为 sf.displayText 应该说 sf.displayText(reader.next() + reader.nextline() + reader.nextline(),并尝试了其他组合,但这可能混淆while参数。还尝试了一个带有简单句子的不同文本文件,没有段落,但同样,什么也没有出现。

在网上查看后,我发现布局可能是一个问题,替代选项可能是 BufferedReader 或使用 JTextArea。浏览 Big Java 并没有提供任何我觉得可以接受的东西,因为关于扫描仪的所有讨论都针对整数,而我想要阅读的文件是散文。我还尝试在代码本身中放入一小段文本并取消其下方的所有其他内容,以查看是否可以将文本从代码传输到 JPanel:

StoryFrame sf = new StoryFrame();
sf.displayText("Life is beautiful"); 

但仍然没有任何结果。最终,我想将文件中的文本放入 JPanel,并让每个段落在前一个段落之后 5 秒出现。所以最终,我的问题是:

  • 为什么我的文字无法显示,或只显示一个字?是因为我没有指定布局吗?
  • 我需要考虑文字换行吗?
  • 我应该查看 JTextArea 而不是 JPanel,而使用 BufferedReader 而不是 Scanner?
  • 我是否正确使用了 Scanner 中的 nextLine 方法?
  • 我可以输入一个命令来读取文件并在 StoryFrame 的显示方法中显示该文件的文本(我认为这会使事情变得更容易)吗?

我知道很多,所以任何问题的任何答案都将不胜感激,谢谢。汤姆

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

public class StoryFrame extends JFrame {

private JLabel mylabel;
public StoryFrame() {

    setTitle("見張ってしながら...");
    setSize(400,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLocationRelativeTo(null);
    mylabel = new JLabel();
    this.add(mylabel);
    setVisible(true);
}


public void displayText(String text) {
    JLabel storyText = new JLabel();
    add(storyText);
}

}

演出介绍

import java.util.Scanner;
import java.io.File;
import java.io.IOException;

class ShowIntro {
 public static void main(String args[]) 
        throws IOException {

StoryFrame sf = new StoryFrame();
Scanner reader = new Scanner(new File("Try.txt"));

while (reader.hasNextLine()) {
    //String line = in.nextLine() Not sure whether this would contribute, I doubt it does though
    sf.displayText(reader.next()); 
            //sf.displayText(reader.next() + reader.nextLine() + reader.nextLine()); was also attempted.

    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {          }
}

 }
}
4

2 回答 2

3

它失败了,因为您从不调用方法来使用 displaytext 方法中的文本

public void displayText(String text) {
    mylabel.setText(text);
}
于 2012-11-30T16:21:29.090 回答
2

您还一次阅读一个单词的文件:

  sf.displayText(reader.next()); 

应该:

   sf.displayText(reader.nextLine());

如果你想读到下一个换行符。


尽管这不是在原始问题中满足以下一些评论,但这里是该程序的修改版本

package com.vincentramdhanie.kitchensink;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class StoryFrame extends JFrame {

    private JTextArea area;
    public StoryFrame() {

        setTitle("見張ってしながら...");
        setSize(400,500);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        area = new JTextArea(10, 30);
        area.setEditable(false); 
        area.setCursor(null); 
        area.setOpaque(false); 
        area.setFocusable(false);
        this.add(area);
        setVisible(true);
    }


    public void displayText(String text) {
        area.setText(text);
    }

    public static void main(String args[]) 
        throws IOException {

        StoryFrame sf = new StoryFrame();
        Scanner reader = new Scanner(new File("Try.txt"));

        while (reader.hasNextLine()) {                
             String line = reader.nextLine();
             sf.displayText(line); 
             try {
                //to skip blank lines. If the line has no non-space characters then do not sleep
               if(!line.trim().equals("")){
                  Thread.sleep(5000);
               }
             } catch (InterruptedException e) {          }
        }
     }
}
于 2012-11-30T16:32:31.343 回答