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:右
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);
相关推荐
SWT JTextPane 可以把控件中需要的字符串的颜色修改成自己想要的颜色
JAVA-----JTextPane实例 代码
JTextPane使用,可以传图片,文字等
NULL 博文链接:https://icarusliu.iteye.com/blog/1412007
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组件(javax.swing.-) 面板:JPanel 标签化窗格:JTabbedPane 拆分窗格:JSplitPane 滚动窗格:JScrollPane 工具栏:JToolBar 桌面窗格:JDesktopPane 内部框架:JInternalFrame 分层窗格:JLayeredPane ...
演示、说明程序 博文链接:https://bps.iteye.com/blog/136627
java写的记事本程序带工具栏可以改变字体颜色,neatbeans 6.5以上可以直接运行,附带实验报告以及源码。实现了打开,保存,退出,复制,粘贴,剪切,改变字体颜色等基本功能。
使用java,swing 实现gps, 源码提供,简要如下: package gps; import java.awt.event.ActionListener; import javax.swing.*; import java.awt.event.*; import java.io.*; public class GpsFrame extends JFrame ...
NULL 博文链接:https://gh0769.iteye.com/blog/598839
本源码实现了 文本区里加图片和插入艺术字的功能 窗口使用Windows窗体风格
这个示例并不是Swing的完整演示,演示了一些常规组件的使用方法,也演示了一些组件的巧妙运用(YY一下,哈哈),如JTextArea的取消重做,用JLayeredPane实现动态的悬浮框,JTextPane演示等等。 我上传的是NetBean的...
NULL 博文链接:https://icarusliu.iteye.com/blog/2098986
课程设计期间做的Java的桌面端小程序,可以发送Emoji表情的聊天室,另外在文件夹中放置了自己的关键代码部分解析,关于emoji部分可查看笔者博客...
老师给的例子,实现了字体设置。使用了JTextPane,主要还是一个实现设置字体的例子
高仿QQ聊天 登录界面 import java.awt.BorderLayout; import java.awt.Color... // 文字颜色和背景颜色 /** * 一个空的构造(可当做换行使用) */ public FontAttrib() { } /** * 返回属性集 * ...