Java 如何在JTextPane中正确填充文本?
我正在使用Java 如何在JTextPane中正确填充文本?,java,swing,jtextpane,Java,Swing,Jtextpane,我正在使用jtepane显示一些文本,其中包括需要突出显示的部分。如何正确填充空格 import javax.swing.*; import javax.swing.text.*; import java.awt.*; public class test2 { public static void main(String[] args) { final JTextPane textPane = new JTextPane(); textPane.setC
jtepane
显示一些文本,其中包括需要突出显示的部分。如何正确填充空格
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
public class test2 {
public static void main(String[] args) {
final JTextPane textPane = new JTextPane();
textPane.setContentType("text/html");
textPane.setText("<html>" + text.replaceAll("\\n", "<br/>").replaceAll(" ", " ").replaceAll("\\t", " ") + "</html>");
StyledDocument doc = textPane.getStyledDocument();
SimpleAttributeSet sas = new SimpleAttributeSet();
StyleConstants.setForeground(sas, Color.blue);
doc.setCharacterAttributes(0, text.length(), sas, false);
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("Test");
frame.setPreferredSize(new Dimension(900, 800));
frame.getContentPane().add(textPane);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
static String text = "\n" +
"\n" +
" pop12 2014-01-12 just a padded text\n" +
" line 2\n" +
" line 3 \n";
}
import javax.swing.*;
导入javax.swing.text.*;
导入java.awt.*;
公共类test2{
公共静态void main(字符串[]args){
最终JTextPane textPane=新JTextPane();
setContentType(“text/html”);
textPane.setText(“+text.replaceAll”(\\n“,“
”).replaceAll(“,”).replaceAll(\\t“,”)+);
StyledDocument doc=textPane.getStyledDocument();
SimpleAttributeSet sas=新的SimpleAttributeSet();
设置前景(sas,Color.blue);
doc.setCharacterAttributes(0,text.length(),sas,false);
invokeLater(新的Runnable(){
公开募捐{
JFrame=新JFrame(“测试”);
框架。设置首选尺寸(新尺寸(900800));
frame.getContentPane().add(textPane);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
静态字符串text=“\n”+
“\n”+
“pop12 2014-01-12只是一个填充文本\n”+
“第2行\n”+
“第3行\n”;
}
这表明:
您可以尝试定义默认的制表位大小,如下所述
或者,您可以通过setParagrahAttributes()为段落添加自定义选项卡集(选项卡数组)设置自定义选项卡集的示例:
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
public class TextPaneTabs
{
public static void setTabs( JTextPane textPane, int charactersPerTab)
{
FontMetrics fm = textPane.getFontMetrics( textPane.getFont() );
int charWidth = fm.charWidth( 'w' );
int tabWidth = charWidth * charactersPerTab;
// int tabWidth = 100;
TabStop[] tabs = new TabStop[5];
for (int j = 0; j < tabs.length; j++)
{
int tab = j + 1;
tabs[j] = new TabStop( tab * tabWidth );
}
TabSet tabSet = new TabSet(tabs);
SimpleAttributeSet attributes = new SimpleAttributeSet();
StyleConstants.setTabSet(attributes, tabSet);
int length = textPane.getDocument().getLength();
textPane.getStyledDocument().setParagraphAttributes(0, length, attributes, false);
}
private static void createAndShowUI()
{
JTextPane textPane = new JTextPane();
textPane.setText("12345678\n\t1\t2\t3aaaaa\t4\t5\t6\t7\t8\n\t1\t2\t3\t4\t5\t6\t7\t8\n\t\t12345678");
JScrollPane scrollPane = new JScrollPane( textPane );
scrollPane.setPreferredSize( new Dimension(700, 100 ) );
// Change the tab size to 4 characters
setTabs( textPane, 8 );
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( scrollPane );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共类textpanetab
{
公共静态无效设置选项卡(JTextPane textPane,int charactersPerTab)
{
FontMetrics fm=textPane.getFontMetrics(textPane.getFont());
int charWidth=fm.charWidth('w');
int tabWidth=charWidth*charactersPerTab;
//int tabWidth=100;
TabStop[]tabs=新TabStop[5];
对于(int j=0;j
如何使用setParagrahAttributes()
?doc.setParagrahAttributes(0,text.length(),sas,false);和StyleConstants.setTabSet