<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    hello world

    隨筆 - 2, 文章 - 63, 評論 - 0, 引用 - 0
    數據加載中……

    設置字體的一篇文章

    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 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 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 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);
    ??}
    ?}

    }
    其他操作如下:
    1、對字體的操作
    MutableAttributeSet attr = new SimpleAttributeSet();
    ????StyleConstants.setFontFamily(attr, family);
    ????setCharacterAttributes(editor, attr, false);
    family為字體
    2、對字體大小的操作
    MutableAttributeSet attr = new SimpleAttributeSet();
    ????StyleConstants.setFontSize(attr, size);
    ????setCharacterAttributes(editor, attr, false);
    size為字號
    3、是否是粗體的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
    ???MutableAttributeSet attr = kit.getInputAttributes();
    ???boolean bold = (StyleConstants.isBold(attr)) ? false : true;
    ???SimpleAttributeSet sas = new SimpleAttributeSet();
    ???StyleConstants.setBold(sas, bold);
    ???setCharacterAttributes(editor, sas, false);
    4、是否是斜體的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
    ???MutableAttributeSet attr = kit.getInputAttributes();
    ???boolean italic = (StyleConstants.isItalic(attr)) ? false : true;
    ???SimpleAttributeSet sas = new SimpleAttributeSet();
    ???StyleConstants.setItalic(sas, italic);
    ???setCharacterAttributes(editor, sas, false);
    5、是否有下劃線的操作
    StyledEditorKit kit = getStyledEditorKit(editor);
    ???MutableAttributeSet attr = kit.getInputAttributes();
    ???boolean underline = (StyleConstants.isUnderline(attr)) ? false
    ?????: true;
    ???SimpleAttributeSet sas = new SimpleAttributeSet();
    ???StyleConstants.setUnderline(sas, underline);
    ???setCharacterAttributes(editor, sas, false);
    6、左中右對齊的處理
    MutableAttributeSet attr = new SimpleAttributeSet();
    ???StyleConstants.setAlignment(attr, a);
    ???setParagraphAttributes(editor, attr, false);
    public static final void setParagraphAttributes(JEditorPane editor,
    ???AttributeSet attr, boolean replace) {
    ??int p0 = editor.getSelectionStart();
    ??int p1 = editor.getSelectionEnd();
    ??StyledDocument doc = getStyledDocument(editor);
    ??doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
    ?}
    a:0:左,1:中,2:右

    7、文本字體顏色的設置
    MutableAttributeSet attr = new SimpleAttributeSet();
    ????StyleConstants.setForeground(attr, fg);
    ????setCharacterAttributes(editor, attr, false);
    fg:為color
    8、文本背景顏色的設置
    MutableAttributeSet attr = new SimpleAttributeSet();
    ????StyleConstants.setBackground(attr, bg);
    ????setCharacterAttributes(editor, attr, false);

    posted on 2009-05-06 14:01 聽風 閱讀(211) 評論(0)  編輯  收藏 所屬分類: JAVA

    主站蜘蛛池模板: 成全高清在线观看免费| 免费一级全黄少妇性色生活片| 免费观看一区二区三区| 亚洲精品456播放| 日本特黄特色AAA大片免费| www.亚洲色图.com| 又长又大又粗又硬3p免费视频| 国产小视频在线观看免费| 黄色a三级三级三级免费看| 亚洲精品人成无码中文毛片| 污视频网站免费在线观看| 亚洲精品A在线观看| 巨胸狂喷奶水视频www网站免费| 国产亚洲一区二区三区在线不卡 | 亚洲欧洲日本精品| 91频在线观看免费大全| 亚洲av日韩综合一区久热| 国产在线观看免费完整版中文版| 麻豆安全免费网址入口| 亚洲国产精品无码久久久蜜芽 | 国产精品69白浆在线观看免费 | 免费国产黄网站在线观看动图| 亚洲欧洲精品成人久久曰影片| 中国一级全黄的免费观看| 亚洲人成在线影院| 丁香花免费高清视频完整版| 亚洲精品GV天堂无码男同| 亚洲AV蜜桃永久无码精品| 99久久免费国产特黄| 日木av无码专区亚洲av毛片| 午夜色a大片在线观看免费| jizz免费观看视频| 337p日本欧洲亚洲大胆精品555588 | 亚洲av无码国产精品色在线看不卡| 9久热精品免费观看视频| 亚洲人成电影在线天堂| 四虎永久在线精品免费网址 | 尤物视频在线免费观看| 久久亚洲国产精品| 免费看片免费播放| av永久免费网站在线观看 |