- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有格式化文本的 JtextPane。我需要复制完整的样式和属性从此文本将其传输到另一个 JtextPane。有示例或代码片段吗看看它是如何工作的?
好的,这是我找到的代码,我做了一些更改:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class Main {
private JTextPane textPane1;
private JTextPane textPane2;
private Document doc1;
private Document doc2;
private JFrame frame1;
private JFrame frame2;
private MutableAttributeSet black;
private MutableAttributeSet red;
private AttributeSet attribute;
public Main() {
textPane1 = new JTextPane();
black = new SimpleAttributeSet();
red = new SimpleAttributeSet();
StyleConstants.setForeground(black, Color.black);
StyleConstants.setForeground(red, Color.red);
textPane1.setEditorKit(new StyledEditorKit());
doc1 = textPane1.getDocument();
append1("This is a Test!\n");
//set color = red
attribute = red;
append1("Hello world! Hello Stackoverflow\n");
//set color = black
attribute = black;
append1("the text is black again\n");
StyledDocument styledDocument = textPane1.getStyledDocument();
textPane2 = new JTextPane();
textPane2.setEditorKit(new StyledEditorKit());
doc2 = textPane2.getDocument();
String text = textPane1.getText();
append2(text);
//transfer format data of text in frame1 to frame2
int docLength = doc1.getLength();
Element element;
AttributeSet attribSet;
for(int i=0;i<docLength;i++) {
element = styledDocument.getCharacterElement(i);
attribSet = element.getAttributes();
StyleConstants.setForeground(red, Color.red);
}
createFrames();
}
private void append1(String text){
try {
doc1.insertString(doc1.getLength(), text, attribute);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
private void append2(String text) {
try {
doc2.insertString(doc2.getLength(), text, attribute);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
private void createFrames() {
frame1 = new JFrame("frame 1");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(400, 300);
frame1.setLocationRelativeTo(null);
frame1.getContentPane().add(new JScrollPane(textPane1), BorderLayout.CENTER);
frame1.setVisible(true);
frame2 = new JFrame("frame 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(400, 300);
frame2.setLocation(300,0);
frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER);
frame2.setVisible(true);
}
public static void main(String args[]) {
new Main();
}
}
我还想将格式样式和属性传输到第二个框架。
最佳答案
我已经解决了。
导入java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class Main {
private JTextPane textPane1;
private JTextPane textPane2;
private Document doc1;
private Document doc2;
private JFrame frame1;
private JFrame frame2;
private MutableAttributeSet black;
private MutableAttributeSet red;
private AttributeSet attribute;
public Main() throws BadLocationException {
textPane1 = new JTextPane();
black = new SimpleAttributeSet();
red = new SimpleAttributeSet();
StyleConstants.setForeground(black, Color.black);
StyleConstants.setForeground(red, Color.red);
textPane1.setEditorKit(new StyledEditorKit());
doc1 = textPane1.getDocument();
append1("This is a Test!\n");
//set color = red
attribute = red;
append1("Hello world! Hello Stackoverflow\n");
//set color = black
attribute = black;
append1("the text is black again\n");
//IMPORTANT PART: attribute of each character from the styled text will
//be transfered to the second textpanel
StyledDocument styledDocument = textPane1.getStyledDocument();
Element element;
textPane2 = new JTextPane();
textPane2.setEditorKit(new StyledEditorKit());
doc2 = textPane2.getDocument();
for(int i=0; i<styledDocument.getLength();i++) {
element = styledDocument.getCharacterElement(i);
AttributeSet attributeNew = element.getAttributes();
System.out.println(i);
append2(styledDocument.getText(i, 1), attributeNew);
}
createFrames();
}
private void append1(String text){
try {
doc1.insertString(doc1.getLength(), text, attribute);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
private void append2(String text, AttributeSet attributeNew) {
try {
doc2.insertString(doc2.getLength(), text, attributeNew);
} catch (BadLocationException ex) {
ex.printStackTrace();
}
}
private void createFrames() {
frame1 = new JFrame("frame 1");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(400, 300);
frame1.setLocationRelativeTo(null);
frame1.getContentPane().add(new JScrollPane(textPane1), BorderLayout.CENTER);
frame1.setVisible(true);
frame2 = new JFrame("frame 2");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setSize(400, 300);
frame2.setLocation(300,0);
frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER);
frame2.setVisible(true);
}
public static void main(String args[]) throws BadLocationException {
new Main();
}
}
关于java - 如何从 JtextPane 获取样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24163370/
我正在序列化一个 JTextPane 的 Document,以便将其样式化的文本保存到数据库中。我有一个 caretListener 附加到 JTextPane 我想知道序列化这个 Document
我目前正在尝试制作 Log4j 2登录 JTextPane .它应该像 Netbeans IDE 控制台中的 STDERR 或 STDOUT(包括文本样式 - 颜色)。 我知道我需要创建一个 appe
我在 JScrollPane 中有一个 JTextPane(水平策略从不,垂直策略始终)。我编写了一种在此 JTextPane 中附加文本的方法。现在有一个按钮,单击该按钮时,操作监听器正在运行,执行
我有一个 JTextPane (1),旁边还有另一个 (2)。我已经同步它们,如果在(2)中输入一行,则在(1)中输入一行,但是当我插入图像(24px)时,(2)会自动调整行长度,但(1)当然不会调整
当我使用 JTextPane 并在 HTML 编辑器模式下键入一些文本时 JTextPane textPane = new JTextPane(); textPane.setContentType("
我在 java swing 中有一个应用程序包含 JTextPane 和 HTMLDocument。假设我将 Pane 的文本设置为:
有没有办法读取 JTextPane 的内容逐行?很像 BufferedReader? 最佳答案 Element root = textPane.getDocument().getDefaultRoot
这是我的问题。我正在编写一个具有语法突出显示的编辑器。没什么特别的,但它可以完成工作。问题是我正在实现错误识别,当我想添加样式来下划线时,我覆盖了我以前的样式。这是一个屏幕截图: 我正在做这样的事情来
所以我想写一个简单的编辑器。我想将所有字符之间的所有字符都涂成灰色“字符。它的片段是: class MainPanel extends JPanel { private int WIDTH = 800
我正在开发一个应用程序,当我从列表中选择一个值(文件)时,它应该在不同形式的 jTextPane 中打开。我正在使用两个面板,一个是显示我的列表的主面板,一个是 ExcelSheet,当我单击列表值时
我创建了一个使用 JTextPane 的 Swing 界面。 JTextPane 使用自定义颜色突出显示: textPane.getHighlighter().addHighlight(startPo
有没有办法使 JTextPane 中的文本看起来与控制台输出的文本相似?我的意思是,基本上,每个字符如何具有相同的宽度,以便 ASCII 艺术或间距缩进等内容可以正常工作。 例如,目前,如果我输入“F
我没有得到垂直滚动条。滚动JTextPane。我正在使用 JPanel 显示 JScrollPane 内部的 JTextPane。这是代码。请查看。谢谢。 import java.awt.*; imp
尝试使用此代码,但它不能准确地改变颜色,请注意“停止”一词。当您键入单词时就会发生这种情况。 https://stackoverflow.com/a/28773736/7694892 最佳答案 在我看
我正在尝试用 JScrollPane 包装 JTextPane,并且我需要背景透明。 这是我的代码: public CharPanel(){ super.setLayout(null);
我使用两个语句添加了图像和文本。但在 JTextPane 中,它仅显示文本。我的代码如下 - jTextPane1.insertIcon(new ImageIcon("t.png")); jTextP
我有两个具有静态大小的 JTextPanes,我想像文本处理器中的两个页面一样连接它们。如果我在第一个 JTextPane(页面)中写入一些内容,并且对于一个 JTextPane 来说太长,那么它会溢
我有一个 JTextPane,其模型是 DefaultStyledDocument。我注意到,如果显示文本,然后使用 setCharacterAttributes 将一行上的每个字符更改为更大的字体,
我有来自 Netbeans 设计器部分的 JTextPane。我想在其上添加列和行。但是,JTextPane 的属性窗口中没有添加列或行的选项。还有其他方法可以做到这一点吗? 最佳答案 JTextPa
我有一个文本需要格式化,文本的第一个单词需要加粗、大字体并居中。 为了进行这种格式化,我使用了 TextSamplerDemo.java 中的解决方案在 JTextComponents 的 oracl
我是一名优秀的程序员,十分优秀!