设置JTextPane指定内容的颜色

package Assis;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class SetTextColor {
	public static void main(String[] args) {
		final JFrame f = new JFrame();
		final JButton b = new JButton("button");
		final JTextPane t = new JTextPane();

		b.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Color selectedColor = JColorChooser.showDialog(f, "zb", t.getSelectedTextColor());
				new StyledEditorKit.ForegroundAction("no-name", selectedColor).actionPerformed(e);
			}
		});

		f.getContentPane().setLayout(new BorderLayout());
		f.getContentPane().add(b, BorderLayout.NORTH);
		f.getContentPane().add(t, BorderLayout.CENTER);
		f.setSize(200, 200);
		f.setVisible(true);

	}
}

 

你可能感兴趣的:(Swing)