最近在完成作业时需要一个个性化的编辑面板,以下是基于JTextPaned的一个实现。
运行效果如下:
以下是全部代码:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.UIManager;
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 MyTextPane extends JPanel implements ActionListener {
private JPanel north;
private JScrollPane scrollPane;
private JTextPane textPane;
private JLabel jLabel2;
private JCheckBox jCheckBox1;
private JButton jButton1;
private JLabel jLabel3;
private JComboBox sizeBox;
private JComboBox fontBox;
private JLabel jLabel1;
private JRadioButton jRadioButton1;
private JCheckBox jCheckBox2;
private Color color = Color.black;
private String font = "宋体";
private boolean isBold = false;
private boolean isUnderlined = false;
private boolean isItalic = false;
private int size = 10;
public MyTextPane() {
initGUI();
}
private void