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";
}
分享到:
相关推荐
在Java的Swing库中,`JEditorPane`和`JTextPane`是两种非常重要的文本组件,它们允许用户编辑和显示富文本内容,包括不同的字体、颜色、字号、样式等。这两个组件都是`javax.swing.text`包的一部分,提供了丰富的...
例如,`\fontname`命令用于设置字体,`\b`表示加粗,`\i`表示斜体。 要创建一个简单的RTF文件,你可以使用`BufferedWriter`类来写入文本。以下是一个基本示例: ```java import java.io.*; public class ...
Java简单记事本是一款基于Java Swing开发的文本编辑器,主要功能包括文本的保存和打开,支持UTF-8编码,提供了基本的文本格式化选项,如设置字体、字号、加粗和斜体,以及常见的剪切、复制和粘贴操作。下面我们将...
另外,Java记事本中的高亮显示可能涉及到颜色样式和字体设置。在Java中,可以通过设置`StyleConstants`来改变文本的字体、颜色和样式。例如,开发者可能创建一个样式,将Java关键字设为蓝色加粗,然后用`Document`...
本文将深入分析一个Java文本编辑器的实现案例,该案例涉及多个Swing组件以及文本格式化的API,旨在为用户提供一个简单直观的方式来改变文本的字体大小与颜色。通过这段代码的学习,我们可以了解到如何使用Swing来...
5. **样式和格式**: 如何设置和更改文本的样式,包括字体、颜色、大小等。 6. **插入多媒体**: 如何通过HTML插入图片或嵌入视频,以及如何在JTextPane中实现这些功能。 7. **编程实践**: 创建和运行Java Swing应用的...
- **文本格式**:可以切换纯文本和富文本模式,富文本支持加粗、斜体、下划线等格式,通过`JTextPane`组件实现。 - **自动换行**:Java文本组件默认支持自动换行,当文本超出边界时,会自动换行显示。 3. **文件...