java多线程    Java入门    vsftp    ftp    linux配置    centos    FRP教程    HBase    Html5缓存    webp    zabbix    分布式    neo4j图数据库    

java编写文本编辑器字体加粗,上颜色功能

最近修改以前一个超级网站编辑器,之前仅仅有格式化字符的功能,最近需要一个能给字体加粗的功能。

发现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();
	}

}


This entry was posted in JAVA and tagged , , , , , . Bookmark the permalink.
月小升QQ 2651044202, 技术交流QQ群 178491360
首发地址:月小升博客https://java-er.com/blog/java-awt-bold/
无特殊说明,文章均为月小升原创,欢迎转载,转载请注明本文地址,谢谢
您的评论是我写作的动力.

5 Responses to java编写文本编辑器字体加粗,上颜色功能

  1. 四物汤 says:

    这可比html+css复杂多了!!呵呵~!来支持一下!

  2. 叶雨梧桐 says:

    升哥 为什么我设置字体无法实现啊,这是代码 谢谢
    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);}
    }

Leave a Reply