简体   繁体   English

JTextArea在新行上不显示文件内容

[英]JTextArea Does Not Display File Contents On New Lines

I'm having an issue using a JTextArea within a scrollpane. 我在滚动窗格中使用JTextArea时遇到问题。 I can read from a textfile, but the contents are all showing up on one line. 我可以从一个文本文件中读取内容,但是所有内容都显示在一行上。 I have tried various methods using append and trying to break the line, but with no success. 我已经尝试过使用append尝试各种方法并试图打破界限,但是没有成功。

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class SplashScreen extends JFrame implements ActionListener
{

    JButton mobile;
    JButton browser;
    JLabel welc;
    JPanel home;
    File file = new File("C:\\Users\\Jimbob\\Desktop\\DisWorkspace\\TrustWizard\\welcometwo.txt");
    BufferedReader reader = null;
    int lines = 10;

    public String read()
    {
        String savetext = "";

        try
        {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            while((text = reader.readLine()) != null)
            {
                savetext += text;
            }
        }
        catch(IOException jim)
        {
            jim.printStackTrace();
        }
        return savetext;
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource().equals(mobile))
        {
            MobileHome openB = new MobileHome();
            this.dispose();
        }
        if(e.getSource().equals(browser))
        {
            BrowserHome openB = new BrowserHome();
            this.dispose();
        }
    }

    public SplashScreen()
    {
        super("Trust Wizard");

        JTextArea homeText = new JTextArea(25, 30);
        homeText.setText(read());

        JScrollPane homeScroll = new JScrollPane(homeText);

        welc = new JLabel("Welcome To The Trust Wizard");
        home = new JPanel();
        mobile = new JButton("Mobile Wizard");
        browser = new JButton("Browser Wizard");

        home.add(welc);
        home.add(homeScroll);
        home.add(mobile);
        home.add(browser);

        ImageIcon img = new ImageIcon("hand.jpg");
        setIconImage(img.getImage());

        mobile.addActionListener(this);
        browser.addActionListener(this);

        getContentPane().add(home);
        home.repaint();
        setSize(450, 530);
        setVisible(true);

    }

    public static void main(String args[])
    {
        SplashScreen test = new SplashScreen();

    }
}

Don't write your own method to load data into a JTextArea. 不要编写自己的方法将数据加载到JTextArea中。

//homeText.setText(read());

Instead, just use the read() method provided by the JTextArea API: 相反,只需使用JTextArea API提供的read()方法:

FileReader reader = new FileReader( your file name here );
BufferedReader br = new BufferedReader(reader);
homeText.read( br, null );
br.close();

Instead of appending just text , try appending text + "\\n" 相反,只是附加的text ,试着在后面加上text + "\\n"

public String read() {
    BufferedReader reader = null;
    StringBuilder builder = new StringBuilder();
    try {
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        String text = null;
        while((text = reader.readLine()) != null) {
            builder.append(text + "\n");
        }
    } catch(IOException jim) {
        jim.printStackTrace();
    } finally {
        try {
            if (reader != null) reader.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    return builder.toString();
}

Also added a finally block to close the BufferedReader (and with it the rest of the stream) 还添加了一个finally块以关闭BufferedReader (以及其余的流)

homeText.setLineWrap(true); should do the trick for you. 应该为您解决问题。

Put that line right after the where you create the homeText variable and then your text will wrap to the size of your JTextArea . homeText一行放在创建homeText变量的位置之后,然后文本将自动homeText JTextArea的大小。

Also put a StringBuilder in your while loop instead of using String concatenation which has a lot of overhead. 还要在您的while循环中放置一个StringBuilderwhile不要使用具有大量开销的String串联。

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

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