Swing对JTextPane中字体颜色的设置
时间:2011-06-21 BlogJava zht
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.text.AttributeSet;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.Edito***t;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEdito***t;
public class NewJFrame extends javax.swing.JFrame implements ActionListener {
private JPanel jp1;
private JButton color;
private JTextPane jep;
private JScrollPane jsp;
private JButton font;
/**
* Auto-generated main method to display this JFrame
*/
public static void main(String[] args) {
NewJFrame inst = new NewJFrame();
inst.setVisible(true);
}
public NewJFrame() {
super();
initGUI();
}
private void initGUI() {
try {
BorderLayout thisLayout = new BorderLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
{
jp1 = new JPanel();
getContentPane().add(jp1, BorderLayout.NORTH);
{
font = new JButton();
font.addActionListener(this);
jp1.add(font);
font.setText("font");
}
{
color = new JButton();
jp1.add(color);
color.addActionListener(this);
color.setText("color");
}
}
{
jsp = new JScrollPane();
getContentPane().add(jsp, BorderLayout.CENTER);
{
jep = new JTextPane();
jsp.setViewportView(jep);
jep.setDocument(new DefaultStyledDocument());
}
}
pack();
setSize(400, 300);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setFontSize(JEditorPane editor, int size) {
if (editor != null) {
if ((size > 0) && (size < 512)) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setFontSize(attr, size);
setCharacterAttributes(editor, attr, false);
} else {
UIManager.getLookAndFeel().provideErrorFeedback(editor);
}
}
}
public static void setForeground(JEditorPane editor, Color fg) {
if (editor != null) {
if (fg != null) {
MutableAttributeSet attr = new SimpleAttributeSet();
StyleConstants.setForeground(attr, fg);
setCharacterAttributes(editor, attr, false);
} else {
UIManager.getLookAndFeel().provideErrorFeedback(editor);
}
}
}
public static final void setCharacterAttributes(JEditorPane editor,
AttributeSet attr, boolean replace) {
int p0 = editor.getSelectionStart();
int p1 = editor.getSelectionEnd();
if (p0 != p1) {
StyledDocument do
|