java 如何创建和使用 JTextPane

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31928306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:22:20  来源:igfitidea点击:

How to create and use a JTextPane

javaswingjtextareajtextpane

提问by Theo

I would like to create a program in Java that it is done this way: What I want

我想用 Java 创建一个程序,它是这样完成的: 我想要的是

A window containing a button and a component (JTextArea, JTextPane, etc.) that can't be modified in which appear some strings based on the execution of some work. As seen from the drawing, if the job is successful the text will be black, if there were errors will be marked red.

包含一个按钮和一个无法修改的组件(JTextArea、JTextPane 等)的窗口,其中会出现一些基于执行某些工作的字符串。从图中可以看出,如果作业成功,文本将显示为黑色,如果有错误将标记为红色。

I managed to do everything correctly using a JTextArea and a JButton but I found that you can't change the color of the strings line by line.

我设法使用 JTextArea 和 JButton 正确完成所有操作,但我发现您无法逐行更改字符串的颜色。

I read that should use a JTextPane but I haven't been able to use it. Here's the code I wrote:

我读到应该使用 JTextPane 但我一直无法使用它。这是我写的代码:

public class Example {

   public Example() {
        JFrame frame = new JFrame();
        JTextPane pane = new JTextPane();
        JButton button = new JButton("Button");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setPreferredSize(new Dimension(200, 200));
        frame.add(pane);
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }
}

When I run the program, this is what is created:

当我运行程序时,这就是创建的内容:

enter image description here

在此处输入图片说明

Where the TextPane?

TextPane 在哪里?

Also, before I added the text in JTextArea using append(), I haven't found a similar method to the JTextPane there? How do you use it? How do I change the color of a single line?

另外,在我使用 append() 在 JTextArea 中添加文本之前,我还没有在那里找到与 JTextPane 类似的方法?你如何使用它?如何更改单条线的颜色?

I have read and seen and tried various examples found on the internet but I could not finish anything... There are worked examples similar to this?

我已经阅读、看到并尝试了在互联网上找到的各种示例,但我无法完成任何事情......有与此类似的工作示例吗?

I apologize for the "generic" request...

我为“通用”请求道歉......

Thanks

谢谢

回答by Constantin

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Example {

    public Example() {

        JFrame frame = new JFrame();
        JTextPane pane = new JTextPane();;
        JButton button = new JButton("Button");

        addColoredText(pane, "Red Text\n", Color.RED);
        addColoredText(pane, "Blue Text\n", Color.BLUE);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setPreferredSize(new Dimension(200, 200));
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.WEST);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        new Example();
    }

    public void addColoredText(JTextPane pane, String text, Color color) {
        StyledDocument doc = pane.getStyledDocument();

        Style style = pane.addStyle("Color Style", null);
        StyleConstants.setForeground(style, color);
        try {
            doc.insertString(doc.getLength(), text, style);
        } 
        catch (BadLocationException e) {
            e.printStackTrace();
        }           
    }
}

Try this example

试试这个例子

回答by Theo

This is a better start:

这是一个更好的开始:

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

public class test  {

    public test() throws BadLocationException {

        JFrame frame = new JFrame();
        DefaultStyledDocument document = new DefaultStyledDocument();
        JTextPane pane = new JTextPane(document);
        JPanel mainPanel = new JPanel();
        JButton button = new JButton("Button");
        button.setPreferredSize(new Dimension(100, 40));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.setPreferredSize(new Dimension(200, 200));
        mainPanel.add(button);
        frame.getContentPane().add(pane, BorderLayout.CENTER);
        frame.getContentPane().add(mainPanel, BorderLayout.WEST);
        StyleContext context = new StyleContext();
        // build a style
        Style style = context.addStyle("test", null);
        // set some style properties
        StyleConstants.setForeground(style, Color.BLACK);
        document.insertString(0, "Four: success \n", style);
        StyleConstants.setForeground(style, Color.RED);
        document.insertString(0, "Three: error \n", style);
        document.insertString(0, "Two: error \n", style);

        StyleConstants.setForeground(style, Color.BLACK);
        // add some data to the document
        document.insertString(0, "One: success \n", style);


        //  StyleConstants.setForeground(style, Color.blue);

        frame.pack();
        frame.setVisible(true);

    }

    public static void main(String[] args) throws BadLocationException {
        new test();
    }
}

It is quite basic, but should get you started. You can create different methods when you have to add text and cascade through the colours.

这是非常基本的,但应该让你开始。当您必须添加文本和级联颜色时,您可以创建不同的方法。

回答by Pilotito_dev

I would recommend you to check out JavaFX, it has a much nicer UI. However, you are already using JPanel and JButton. You could use a JLabel to display the desired text, and use JLabel.setTextFill(Color.WHITESMOKE)to set the color of the text.

我建议您查看 JavaFX,它有一个更好的用户界面。但是,您已经在使用 JPanel 和 JButton。您可以使用 JLabel 来显示所需的文本,并用于JLabel.setTextFill(Color.WHITESMOKE)设置文本的颜色。