最近修改以前一个超级网站编辑器,之前仅仅有格式化字符的功能,最近需要一个能给字体加粗的功能。
发现java编写awt 窗口还是蛮费劲的。
特留在此,方便朋友们相互学习。
实现功能:
对选取的一段文字进行加粗
(上颜色自己发挥,原理一样。)
要点
1.不能用JTextArea
2.采用JTextPane
3.获取选中的文本算法
package com.javaer.examples.awt;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.WindowConstants;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
public class RichEdit extends JFrame {
JTextPane textPane;
JButton bold;
public RichEdit(){
textPane = new JTextPane();
textPane.setBounds(0, 0, 300, 300);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
setSize(new Dimension(500,500));
}
public void gui() {
Container c = getContentPane();
c.setLayout(null);
bold = new JButton(" 粗体 ");
bold.setBounds(new Rectangle(400, 120, 100, 42));
c.add(bold);
//触发粗体
bold.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Document doc = textPane.getDocument();
int start = textPane.getSelectionStart();
int end = textPane.getSelectionEnd();
String str = textPane.getSelectedText();
try {
doc.remove(start, end-start);
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setBold(attrSet, true);
StyleConstants.setFontFamily(attrSet, "微软雅黑");
StyleConstants.setFontSize(attrSet, 18);
StyleConstants.setForeground(attrSet, Color.black); // 颜色
try {
doc.insertString(start, str, attrSet);
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
c.add(textPane);
textPane.setFont(new Font("微软雅黑", Font.PLAIN, 16));
// this.getContentPane().add(textPane, BorderLayout.CENTER);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
RichEdit re = new RichEdit();
re.gui();
}
}
You must be logged in to post a comment.
这可比html+css复杂多了!!呵呵~!来支持一下!
升哥 为什么我设置字体无法实现啊,这是代码 谢谢
import java.awt.*;
public class z
{ public static void main(String[]args){
Frame f1=new Frame(“字体”);
f1.setLayout(new GridLayout(9,1));
f1.setSize(350,350);
f1.setLocation(0,0);
f1.setVisible(true);
Label la1;
la1= new Label(“哈哈哈”);
Font a = new Font(“华文彩云”,0,20);
la1.setFont(a);
f1.add(la1);}
}
采用JTextPane
要点
1.不能用JTextArea
2.采用JTextPane
你那么直接设置是没有用的哦。
谢升哥,搞定
别客气哈。