简体   繁体   English

JTextArea中的选项卡输出行为

[英]Tab output behavior within a JTextArea

I try to be as punctilious as possible in researching before I ask a question, especially one so simple, so bear with me. 在问一个问题之前,我会尽量在研究中做到点点滴滴,尤其是一个如此简单的问题,所以请耐心等待。

I have a String that's tab delimited, and when I output it to a JTextArea in Java, I get behavior that looks like this: 我有一个制表符分隔的字符串,当我将其输出到Java中的JTextArea时,会出现如下所示的行为:

FirstName   LastName   PhoneNumber         BirthDate

Linewrap is turned off with horizontal scrolling enabled. 在启用水平滚动的情况下,关闭了自动换行功能。 After toiling over the documentation and SA, I'm missing something obvious as to why it's exhibiting this behavior. 在整理了文档和SA之后,我一直缺少关于它为什么表现这种行为的明显信息。

As @kleopatra comments, this is not unexpected. 正如@kleopatra的评论,这并不意外。 As alternatives, consider JTable or How to Use HTML in Swing Components . 作为替代方案,请考虑使用JTable如何在Swing组件中使用HTML

Addendum: I previously overlooked setTabSize() . 附录:我以前忽略了setTabSize()

在此处输入图片说明

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/** @see https://stackoverflow.com/questions/7509429 */
public class TextAreaTabs extends JPanel {

    public TextAreaTabs() {
        JTextArea jta = new JTextArea("FirstName\tLastName\tPhoneNumber\tBirthDate");
        jta.setTabSize(10);
        this.add(jta);
    }

    private void display() {
        JFrame f = new JFrame("TextAreaTabs");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TextAreaTabs().display();
            }
        });
    }
}

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

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