我是Swing应用程序的新手。我需要在文本中添加firstLineIndent,但在将此值设置为负数后,段落的第一行似乎有点模糊。我无法识别StyleConstants.setFirstLineIndent(attr,-50)方法的行为。 如何纠正这个错误。
下面是我使用的代码链接:
http://java-sl.com/tip_hanging_first_line.html
谢谢...
答案 0 :(得分:1)
不确定为什么该示例扩展了JEditorPane。编辑器窗格用于HTML。我更喜欢使用JTextPane来设置样式文本。也不知道为什么使用自定义编辑器工具包。
以下代码适用于我:
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
//public class HangingIndent extends JEditorPane {
public class HangingIndent extends JTextPane {
public static void main(String[] args) {
JFrame frame = new JFrame("Negative (Hanging) first line indent");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final HangingIndent app = new HangingIndent();
// app.setEditorKit(new MyEditorKit());
app.setText("The paragraph with long text is necessary to show how " +
"hanging indent can be achieved. We should set not only the " +
"first line indent but the same left indent.");
StyledDocument doc=(StyledDocument)app.getDocument();
SimpleAttributeSet attrs=new SimpleAttributeSet();
StyleConstants.setFirstLineIndent(attrs, -50);
StyleConstants.setLeftIndent(attrs, 50);
doc.setParagraphAttributes(0,doc.getLength(),attrs, false);
JScrollPane scroll = new JScrollPane(app);
frame.getContentPane().add(scroll);
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public HangingIndent() {
super();
}
}