java字体设置,包括大小,颜色,加粗,下划线,对齐,斜体的设置

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);
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第1章 财务分析报告 1.1 知识点 1.2 任务书 1.3 任务示范 1.4 知识拓展 计算机应用基础立体化教程-(12)全文共131页,当前为第1页。 1.1 知 识 点 图1-1 "页面设置"命令组 1.1.1 页面设置   "页面设置"命令组在"页面布局"选项卡中,包括纸张大小、纸张方向、页边距等的设置,如图1-1所示。    计算机应用基础立体化教程-(12)全文共131页,当前为第2页。   (1) 纸张大小设置所用的纸张大小,如办公用的A4纸;也可以选择自定义纸张大小,进行特殊纸张的大小设置,如请柬、信封等。  (2) 纸张方向:用于设置纸张的方向,有横向和纵向两种。  (3) 页边距:用于设置页面上、下、左、右的边距。Word自带了一些常用的边距,包括普通、窄、适中、宽。用户可选择使用Word自带的边距,也可自定义输入具体的边距值。 计算机应用基础立体化教程-(12)全文共131页,当前为第3页。   (4) 页面设置对话框:点击"页面布局"选项卡,再点击"页面设置"命令组右下角的小箭头,会出现如图1-2所示的"页面设置"对话框,可以设置上、下、左、右页边距及装订线的距离和位置;也可设置纸张方向,该设置可以应用于整篇文档或选中文字。  "页面设置"对话框中,还能设置版式和文档网格。具体应用在项目中展开。 计算机应用基础立体化教程-(12)全文共131页,当前为第4页。 图1-2 "页面设置"对话框 计算机应用基础立体化教程-(12)全文共131页,当前为第5页。 1.1.2 字体设置  "字体"命令组在"开始"选项卡中,如图1-3所示。其常规设置的内容有字体字号、字大小、黑体(B)、斜体(I)、加下划线(U)、上标、下标;文字效果有发光、加亮、底纹、带圈、加框、加拼音、清除格式、更改大小写。 计算机应用基础立体化教程-(12)全文共131页,当前为第6页。 图1-3 "字体"命令组 计算机应用基础立体化教程-(12)全文共131页,当前为第7页。   在"字体"命令组中,可以设置文字字体。系统的字体均安装在系统目录下的fonts文件夹中,用户可以根据需要自行安装所需要的字体。  (1) 字号指文字大小,默认是五号字。一般,中文字号最大的是初号,最小的是八号。另外,可以选择数字或者输入数字按回车键"Enter"来设置字号,数字越大,字号越大。  (2)   按钮可以根据一个预先设置大小字体号进行调整,如放大或缩小字号。 计算机应用基础立体化教程-(12)全文共131页,当前为第8页。   (3) 有些文档包含英文,其中涉及大小写,可以利用   按钮进行设置,如图1-4所示。通过下拉菜单,可以设置句首字母大写、全部小写、全部大写、每个单词首字母大写、切换大小写、半角和全角。 计算机应用基础立体化教程-(12)全文共131页,当前为第9页。 图1-4 更改大小写 计算机应用基础立体化教程-(12)全文共131页,当前为第10页。   (4)     为字形设置按钮,分别用于加粗倾斜和加下划线。单击下划线右侧向下箭头,可以在下拉菜单中选择下划线线型。  (5)     分别为设置删除线、下标和上标按钮。另外,设置下标还可用快捷键"Ctrl + +",设置上标可用"Ctrl + Shift + +"。  (6)     按钮中,第一个命令按钮能清除格式,可将所选文本格式清除;第二个命令按钮是为文字添加拼音,如图1-5所示;第三个命令按钮是为所选文字加边框。 计算机应用基础立体化教程-(12)全文共131页,当前为第11页。 图1-5 拼音指南 计算机应用基础立体化教程-(12)全文共131页,当前为第12页。   (7) 文字效果设置,用户可以为文字设置不同的效果,如图1-6所示。用户可以选择预设的一项效果,也可以自己调整轮廓、阴影、映像、发光来制作独特的文本效果。 图1-6 文字效果 计算机应用基础立体化教程-(12)全文共131页,当前为第13页。   (8)    按钮可以不同的颜色为背景来突出显示文字,在右侧下拉菜单中可以选择所需的不同颜色。取消颜色可以设置颜色。  (9)    为设置文字的颜色按钮;  为设置字符底纹按钮; 为设置带圈文字和带圈样式按钮,圈号可以选择,圈号文字也能设置,如图1-7所示。 计算机应用基础立体化教程-(12)全文共131页,当前为第14页。 图1-7 带圈字符 计算机应用基础立体化教程-(12)全文共131页,当前为第15页。 1.1.3 段落设置  "段落"命令组在"开始"选项卡中。段落设置的内容有左对齐、居中对齐、右对齐、两端对齐、分散对齐、行距、底纹、边框等,如图1-8所示。 图1-8 "段落"命令组 计算机应用基础立体化教程-(12)全文共131页,当前为第16

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值