java 字体设置

本文介绍了一个Java程序,该程序使用Swing实现了一个文本编辑器的字体设置功能,包括字体大小、颜色、加粗、斜体、下划线及对齐方式等,并提供了详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个Java字体设置程序,包括大小,颜色,加粗,下划线,对齐,斜体的设置,很全!!! 收藏

SwingJTextPane中字体颜色的设置

转自: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 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);

 

 

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/tdy218/archive/2008/05/05/2390991.aspx

<think>好的,我现在需要帮助用户解决在IntelliJ IDEA中设置Java代码字体的问题。首先,我得回忆一下之前用户提供的引用内容,看看有没有相关的设置方法。引用[1]、[2]、[4]都提到了字体设置的相关步骤,应该可以综合这些信息来给出答案。 用户的问题很明确,是想知道如何在IDEA中设置Java代码的字体。我需要分步骤说明,确保用户能够一步步跟着操作。根据引用[1]和[2],设置字体大小的路径是File -> Settings -> Editor -> Font。这里不仅可以调整字体大小,还能选择字体类型。引用[4]也提到在Appearance & Behavior里设置系统字体,但用户可能更需要的是代码编辑区的字体设置,所以重点应该放在Editor -> Font部分。 另外,引用[2]还提到可以通过Ctrl+鼠标滚轮来缩放字体,这个功能对用户来说可能也很实用,应该作为补充方法介绍。需要注意的是,区分开固定字体大小设置和临时缩放的不同,避免用户混淆。 还要检查是否有其他相关设置,比如备用字体(Fallback font)和行距(Line spacing),这些虽然不是主要需求,但提出来可以让回答更全面。同时,提醒用户恢复默认值的选项,以防设置出错时可以快速还原。 需要确保步骤清晰,使用中文,并且每个步骤都对应正确的菜单路径。可能用户对IDEA的界面不熟悉,所以路径描述要准确,比如使用“Editor -> Font”而不是直接说Font菜单。同时,快捷键部分要明确,比如Ctrl+Alt+S打开设置,这样用户操作起来更方便。 最后,生成相关问题的时候,要考虑到用户可能后续会遇到的设置问题,比如调整整体界面字体、快捷键修改等,这样相关问题才能覆盖更多潜在需求。需要确保问题与当前主题相关,并且对用户有帮助。</think>### IntelliJ IDEA中设置Java代码字体的方法 #### 固定字体设置(推荐) 1. **打开设置界面** 使用快捷键 $Ctrl+Alt+S$ 或通过菜单栏 $File \rightarrow Settings$ 进入设置 2. **定位字体设置** 左侧导航栏选择 $Editor \rightarrow Font$ $$路径公式:Editor \supset Font$$ 3. **自定义字体属性** - $Font$:选择字体类型(如JetBrains Mono、Consolas) - $Size$:设置字号(建议12-16) - $Line\ spacing$:调整行距(默认1.0) 4. **应用设置** 点击 $Apply$ 预览效果,确认后点 $OK$[^2][^4] ```mermaid graph TD A[打开IDEA] --> B[Ctrl+Alt+S] B --> C[Editor → Font] C --> D[选择字体] C --> E[设置字号] C --> F[调整行距] D --> G[Apply] E --> G F --> G G --> H[确认效果] ``` #### 快捷键动态缩放(辅助功能) 在 $Editor \rightarrow General$ 中勾选: $Change\ font\ size\ (Zoom)\ with\ Ctrl+Mouse\ Wheel$[^2] 启用后可通过 $Ctrl+鼠标滚轮$ 实时缩放代码显示(不影响实际字号) #### 配置注意事项 1. 中英文字体适配:推荐使用等宽字体(如更纱黑体) 2. 备用字体设置:在 $Fallback\ font$ 中指定中文字体 3. 恢复默认值:点击 $Restore\ Defaults$ 可撤销修改[^2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值