简体   繁体   English

JPanel的文字问题

[英]Text problems with JPanel

After thinking I was on course to solving a problem with making text (read from a file) appear in a JPanel, I`m frustratingly back to square one. 考虑到我正在解决要使文本(从文件中读取)出现在JPanel中的问题之后,我沮丧地回到了第一位。

The code is below. 代码如下。 The result is just a blank screen 400x500 screen. 结果只是一个空白屏幕400x500屏幕。 Some combinations of using nextLine() + nextLine() as displayText commands result in one word coming up from the file (the word has been different multiple times). 使用nextLine()+ nextLine()作为displayText命令的某些组合会导致一个单词从文件中出现(该单词已多次不同)。 This makes me wonder: do I need code that deals with text wrapping? 这让我感到奇怪:我是否需要处理文本换行的代码? The textfile itself is in paragraphs, and as such, I thought that sf.displayText should say sf.displayText(reader.next() + reader.nextline() + reader.nextline(), and have tried other combinations, but this may be confusing the while parameters. Have also tried a different textfile with simple sentences, no paragraphs, but again, nothing comes up. 文本文件本身在段落中,因此,我认为sf.displayText应该说sf.displayText(reader.next()+ reader.nextline()+ reader.nextline(),并尝试了其他组合,但这可能另外,也尝试过使用简单的句子,没有段落的其他文本文件,但是还是没有任何结果。

Having looked online, I have found that layouts may be an issue, and that alternative options may be BufferedReader or using JTextArea. 上网查看后,我发现布局可能是个问题,替代选项可能是BufferedReader或使用JTextArea。 Browsing through Big Java didn`t provide anything I felt I could take, as all discussion on the scanner went towards integers, whereas the file I want read is prose. 在Big Java上浏览并没有提供我能接受的任何东西,因为有关扫描程序的所有讨论都转向整数,而我要读取的文件是散文。 I also tried putting a small piece of text in the code itself and cancelling out everything else below it to see if I could transfer text from the code to the JPanel: 我还尝试在代码本身中放入一小段文本,然后取消其下方的所有其他内容,以查看是否可以将文本从代码传输到JPanel:

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

but still nothing came up. 但仍然没有任何结果。 Ultimately, I want to put text from a file into a JPanel, and have each paragraph come up 5 seconds after the one before. 最终,我想将文件中的文本放入JPanel中,并让每个段落比之前的段落晚5秒。 So ultimately, my questions are: 所以最终,我的问题是:

  • Why does my text fail to show up, or only display one word? 为什么我的文本无法显示,或仅显示一个单词? Is it because I don`t specify a layout? 是因为我没有指定布局?
  • Do I need to think about text wrapping? 我需要考虑文字换行吗?
  • Should I look into JTextArea instead of JPanel, and BufferedReader instead of Scanner? 我应该查看JTextArea而不是JPanel,还是BufferedReader而不是Scanner吗?
  • Have I been using the nextLine method from the Scanner correctly? 我是否一直在正确使用扫描仪中的nextLine方法?
  • Can I put a command to read a file and display that file`s text in the display method of StoryFrame (I think this would make things a lot easier)? 我可以在StoryFrame的显示方法中放置一个命令来读取文件并显示该文件的文本(我认为这会使事情变得容易得多)吗?

I know it`sa lot, so any answers to any of the questions would be greatly appreciated, thank you. 我知道很多,因此,对任何问题的任何答案将不胜感激,谢谢。 Tom 汤姆

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

}

ShowIntro 显示介绍

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) {          }
}

 }
}

it fails because you never call a method to use the text in your displaytext method 它失败,因为您从不调用方法在displaytext方法中使用文本

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

You are also reading the file one word at a time: 您还一次读取一个单词的文件:

  sf.displayText(reader.next()); 

should be: 应该:

   sf.displayText(reader.nextLine());

if you want to read upto the next newline character. 如果您想阅读下一个换行符。


Even though this was not in the original question to satisfy some of the comments below here is a modified version of the program 即使这不是最初的问题,也不能满足下面的一些评论,但这里是该程序的修改版本

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) {          }
        }
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM