设置java的JTextPane字体加粗


package manage;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
/**
 * 测试点击按钮后,部分字体加粗
 * doc.setCharacterAttributes(0, 100, boldStyle, true); 该方法设置加粗字体数量
 * @author lance
 */
public class TextSamplerDemo {
	public static void main(String[] args) {
	    try {
	        UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
	    } catch (Exception evt) {}
	  
	    JFrame f = new JFrame("Styles Example 3");
	    
	    StyleContext sc = new StyleContext();
	    final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
	    JTextPane pane = new JTextPane(doc);
	    
	    Style defaultStyle = sc.getStyle(StyleContext.DEFAULT_STYLE);
	    final Style mainStyle = sc.addStyle("MainStyle", defaultStyle);
	    StyleConstants.setLeftIndent(mainStyle, 16);
	    StyleConstants.setRightIndent(mainStyle, 16);
	    StyleConstants.setFirstLineIndent(mainStyle, 16);
	    StyleConstants.setFontFamily(mainStyle, "serif");
	    StyleConstants.setFontSize(mainStyle, 12);
	    
	    final Style boldStyle = sc.addStyle("MainStyle", defaultStyle);
	    StyleConstants.setBold(boldStyle, true);

	    try {
	      SwingUtilities.invokeAndWait(new Runnable() {
	        public void run() {
	          try {
	            doc.setLogicalStyle(0, mainStyle);

	            doc.insertString(0, text, null);          
	          } catch (BadLocationException e) {
	          }
	          doc.dump(System.out);
	        }
	      });
	    } catch (Exception e) {
	      System.out.println("Exception when constructing document: " + e);
	      System.exit(1);
	    }   
	    
	    JButton button = new JButton("apply");
	    button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				//设置多少字体加粗
				doc.setCharacterAttributes(0, 100, boldStyle, true);
			}
	    	
	    });
	    
	    JScrollPane scrollPane = new JScrollPane(pane);
	    
	    JPanel panel = new JPanel();
	    panel.add(button, BorderLayout.NORTH);
	    panel.add(scrollPane, BorderLayout.CENTER);
	              
	    f.getContentPane().add(scrollPane, BorderLayout.CENTER);
	    f.getContentPane().add(button, BorderLayout.SOUTH);
	    f.setSize(400, 300);
	    f.setVisible(true);
	  }

	  public static final String text = 
	          "Attributes, Styles and Style Contexts\n" +
	          "The simple PlainDocument class that you saw in the previous " + 
	          "chapter is only capable of holding text. The more complex text " +
	          "components use a more sophisticated model that implements the " +
	          "StyledDocument interface. StyledDocument is a sub-interface of " +
	          "Document that contains methods for manipulating attributes that " +
	          "control the way in which the text in the document is displayed. " + "\n\r" + 
	          "The Swing text package contains a concrete implementation of " +
	          "StyledDocument called DefaultStyledDocument that is used as the " +
	          "default model for JTextPane and is also the base class from which " +
	          "more specific models, such as the HTMLDocument class that handles " +
	          "input in HTML format, can be created. In order to make use of " +
	          "DefaultStyledDocument and JTextPane, you need to understand how " +
	          "Swing represents and uses attributes.\n";
	

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设置字体颜色可以使用Java的Swing库中的JTextPane或JLabel组件。这些组件都提供了设置字体颜色的方法。 以下是使用JTextPane设置字体颜色的示例代码: ```java import javax.swing.*; import javax.swing.text.*; public class TextPaneDemo extends JFrame { public TextPaneDemo() { JTextPane textPane = new JTextPane(); StyledDocument doc = textPane.getStyledDocument(); Style style = textPane.addStyle("Color Style", null); StyleConstants.setForeground(style, Color.RED); try { doc.insertString(doc.getLength(), "Hello, World!", style); } catch (BadLocationException e) { e.printStackTrace(); } add(textPane); pack(); setVisible(true); } public static void main(String[] args) { new TextPaneDemo(); } } ``` 这个示例创建了一个JTextPane对象,并获取了它的StyledDocument对象。然后使用addStyle()方法创建了一个Style对象,命名为"Color Style"。接着使用StyleConstants类的setForeground()方法将字体颜色设置为红色。最后,使用insertString()方法将文本插入到JTextPane中。 使用JLabel设置字体颜色的示例代码如下: ```java import javax.swing.*; import java.awt.*; public class LabelDemo extends JFrame { public LabelDemo() { JLabel label = new JLabel("Hello, World!"); label.setForeground(Color.RED); add(label); pack(); setVisible(true); } public static void main(String[] args) { new LabelDemo(); } } ``` 这个示例创建了一个JLabel对象,并使用setForeground()方法将字体颜色设置为红色。然后将JLabel添加到JFrame中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值