如何创建要在Java Swing组件中显示的格式化文本(JTextPane,JEditorPane ...) [英] How to create formatted text to show in a Java Swing Component (JTextPane, JEditorPane...)
问题描述
我在任何地方都进行了搜索和研究,找到了一个没有结果的答案。所以我再次请求帮助。
I've searched and researched in everywhere I could to find an answer with no result. So I ask for help once more.
我想在桌面java swing应用程序中显示格式化文本。这个文本将在一些变量对象的基础上编程生成,并且不可编辑。
I want to show formatted text in a desktop java swing application. This text would be programactly generated in base of some variable objects and wouldn't be editable.
我不知道是否最好使用JTextPane或JEditorPane,或者什么。问题是我找不到任何解释如何使用它们的手册或教程。我是否必须创建HTMLDocument才能插入文本?如何创建它?...
I don't know if is best to use JTextPane, or JEditorPane, or what. The matter is that I don't find anywhere some manual or tutorial that explain how to use them. Do I have to create an HTMLDocument to insert the text? How do I create it?...
在这种情况下,是显示文本的正确方法吗?或者我可以使用表格或标签或类似的东西。
Is is the right way to show text in this case?, or may I go using tables or labels or something like that.
我需要你的一些建议,如果有一些地方我可以学习如何去做,请告诉我。
I need some advice from you please, if there is some where I could learn how to do it, tell me please.
推荐答案
下面看一下这个代码示例,如果你想再修改一下这个例子,可以添加Font作为参数,并在指定位置使用适当的参数。在 JTextField
中写一些内容,然后按 ENTER
几次。
Here have a look at this code example, if you want to modify this a bit more, add Font too as an argument and use that appropriate argument at the specified location. Write something in the JTextField
and press ENTER
several times.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class TextPaneTest extends JFrame {
private JPanel topPanel;
private JTextPane tPane;
private JTextField tfield;
private int counter;
private Color[] colours = {
Color.RED,
Color.BLUE,
Color.DARK_GRAY,
Color.PINK,
Color.BLACK,
Color.MAGENTA,
Color.YELLOW,
Color.ORANGE
};
public TextPaneTest() {
counter = 0;
}
private void createAndDisplayGUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
EmptyBorder eb = new EmptyBorder(new Insets(10, 10, 10, 10));
tPane = new JTextPane();
tPane.setBorder(eb);
tPane.setMargin(new Insets(5, 5, 5, 5));
JScrollPane scroller = new JScrollPane();
scroller.setViewportView(tPane);
tfield = new JTextField();
tfield.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
counter++;
if (counter == 8)
counter = 0;
String text = tfield.getText() + "\n";
appendToPane(tPane, text, colours[counter]);
tfield.selectAll();
}
});
getContentPane().add(scroller, BorderLayout.CENTER);
getContentPane().add(tfield, BorderLayout.PAGE_END);
setSize(200, 100);
setVisible(true);
tfield.requestFocusInWindow();
}
private void appendToPane(JTextPane tp, String msg, Color c) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
int len = tp.getDocument().getLength();
tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TextPaneTest().createAndDisplayGUI();
}
});
}
}
这篇关于如何创建要在Java Swing组件中显示的格式化文本(JTextPane,JEditorPane ...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!