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-----JTextPane实例 代码
JTextPane使用,可以传图片,文字等
老师给的例子,实现了字体设置。使用了JTextPane,主要还是一个实现设置字体的例子
SWT JTextPane 可以把控件中需要的字符串的颜色修改成自己想要的颜色
演示、说明程序 博文链接:https://bps.iteye.com/blog/136627
NULL 博文链接:https://icarusliu.iteye.com/blog/1412007
我还使用了一些其他的Java类和方法,例如JFileChooser、JTextPane、JMenuBar等等。 我希望这个简单的记事本可以帮助其他学习Java编程的人,特别是那些想要学习GUI编程的人。如果您有任何建议或需要帮助,请随时与我...
java写的记事本程序带工具栏可以改变字体颜色,neatbeans 6.5以上可以直接运行,附带实验报告以及源码。实现了打开,保存,退出,复制,粘贴,剪切,改变字体颜色等基本功能。
本源码实现了 文本区里加图片和插入艺术字的功能 窗口使用Windows窗体风格
NULL 博文链接:https://icarusliu.iteye.com/blog/2098986
NULL 博文链接:https://gh0769.iteye.com/blog/598839
第1章 Java概述 1 1.1 Java语言的发展简史 2 1.2 Java的竞争对手及各自优势 4 1.2.1 C#简介和优势 4 1.2.2 Ruby简介和优势 4 1.2.3 Python的简介和优势 5 1.3 Java程序运行机制 5 1.3.1 高级语言的运行机制 6...
Java制作的网页浏览器和源代码查看器,仿IE的网页浏览功能,不支持HTML5,窗口采用上下分屏显示的方式,上面显示网页浏览器,下面显示该网页的源代码,JTextField jtfAddress; //输入html文件地址或网址 ...
课程设计期间做的Java的桌面端小程序,可以发送Emoji表情的聊天室,另外在文件夹中放置了自己的关键代码部分解析,关于emoji部分可查看笔者博客...
import javax.swing.JTextPane; import javax.swing.UIManager; import javax.swing.WindowConstants; import javax.swing.text.AttributeSet; import javax.swing.text.DefaultStyledDocument; import javax....
Java代码编写的简单网页浏览器,仿IE基础功能,只能解析标准的HTML文件,现在流行的HTML5格式不能解析。使用方法:可直接输入文件名或网络地址,但必需事先连入网络。 JTextField jtfAddress; //输入html文件地址...
18 实例8 利用JNI实现窗口特效 20 实例9 文件选择对话框 24 实例10 改变字体 26 实例11 应用JTree 28 实例12 应用JTable 30 实例13 记事本 32 实例14 使用JTextPane 36 实例15 实现多文档...
使用java,swing 实现gps, 源码提供,简要如下: package gps; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.event.*; import java.io.*;... private JTextPane jTextPan