java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置
2017-09-22 20:11
761 查看
本文转自:http://www.blogjava.net/Swing/archive/2007/12/26/128965.html
下面的代码告诉我们该怎么在文本编辑器中设置字体大小,颜色,加粗,下划线等许多便捷操作~
感谢原作者提供的代码。
下面的代码告诉我们该怎么在文本编辑器中设置字体大小,颜色,加粗,下划线等许多便捷操作~
感谢原作者提供的代码。
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.EditorKit; import javax.swing.text.MutableAttributeSet; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.StyledDocument; import javax.swing.text.StyledEditorKit; public class TestFonts extends javax.swing.JFrame implements ActionListener { private static final long serialVersionUID = 1L; 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) { TestFonts inst = new TestFonts(); inst.setVisible(true); } public TestFonts() { 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 doc = getStyledDocument(editor); doc.setCharacterAttributes(p0, p1 - p0, attr, replace); } StyledEditorKit k = getStyledEditorKit(editor); MutableAttributeSet inputAttributes = k.getInputAttributes(); if (replace) { inputAttributes.removeAttributes(inputAttributes); } inputAttributes.addAttributes(attr); } protected static final StyledDocument getStyledDocument(JEditorPane e) { Document d = e.getDocument(); if (d instanceof StyledDocument) { return (StyledDocument) d; } throw 4000 new IllegalArgumentException("document must be StyledDocument"); } protected static final StyledEditorKit getStyledEditorKit(JEditorPane e) { EditorKit k = e.getEditorKit(); if (k instanceof StyledEditorKit) { return (StyledEditorKit) k; } throw new IllegalArgumentException("EditorKit must be StyledEditorKit"); } public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (obj == font) { JEditorPane editor = jep; setFontSize(editor, 20); } if (obj == color) { JEditorPane editor = jep; setForeground(editor, Color.red); } } }
相关文章推荐
- java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置
- java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置
- java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置
- 文本编辑器中设置字体大小,颜色,加粗,下划线等许多便捷操作~
- Latex设置字体大小,加粗,加下划线,变斜体_孩纸气_新浪博客
- UILable属性详解,设置居上对齐,居中对齐,居下对齐,获取斜体字,字体、大小、单位、颜色
- Java中怎么设置JLabel的字体样式,大小,颜色?
- 设置UIButton的文字显示位置、字体的大小、字体的颜色、加粗
- Android资源之String (三): html标签,语法(原生支持) 设置字体大小/颜色/下划线等
- css基础 给p标签中的内容 加粗,斜体,下划线,字体大小
- css基础 给p标签中的内容 加粗,斜体,下划线,字体大小
- 设置TabHost中的Tab标签大小,字体的颜色,下划线的长度,使Tab之间可以有间隔
- ios设置字体-黑体,加粗等 修改textField的placeholder的字体颜色、大小
- 设置UIButton的文字显示位置、字体的大小、字体的颜色、加粗(特别是字体颜色设置的那句代码。。挺有用)
- HTML常用字体样式设置(加粗、下划线、斜体)
- Android中TextView通过SpannableString设置字体、大小、颜色、样式、超级链接
- placeholder设置字体大小与颜色
- CSDN-markdown 文字样式设置(字体, 大小, 颜色, 高亮底色)
- [Flex]Flex编程注意之修改CategoryAxis的字体样式(大小、颜色、粗斜体等)
- dephi(pascal)中修改Label字体的样式(加粗,斜体,下划线)