我正在使用带有JTextPane组件的Java Swing创建一个简单的Text编辑器。我添加了使文本变为粗体,斜体,下划线的代码,并且还添加了在JComboBox中获取系统字体的代码,以便可以更改JTextPane内容的字体。如果内容具有多种字体样式,则会根据光标位置显示相应的字体名称。
我有一个问题:如果内容具有多个字体样式,请按Ctrl + A组合键选择所有内容,并且将整个内容字体更改为相同的字体(这是第一行的字体样式) 。在按Ctrl + A之前:
按Ctrl + A后,第一行的字体样式-Calibri字体将应用于所有三行,如下图:
这是最简单的代码
import java.awt.BorderLayout;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.rtf.RTFEditorKit;
public class Editor2 {
private JTextPane editor;
private DefaultStyledDocument doc;
private DefaultComboBoxModel<String> fontFamilyComboBoxModel;
private JComboBox<String> fontSizeComboBox;
private JComboBox<String> fontFamilyComboBox;
private AttributeSet attrs;
private String fontFamilyStr;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Editor2().createAndShowGUI();
}
});
}
private void createAndShowGUI() {
editor = new JTextPane();
editor.setMargin(new Insets(5, 5, 5, 5));
RTFEditorKit rtf = new RTFEditorKit();
editor.setEditorKit(rtf);
editor.addCaretListener(new MyCaretListener());
JScrollPane editorScrollPane = new JScrollPane(editor);
doc = new DefaultStyledDocument();
initDocAttrs();
editor.setDocument(doc);
final String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
fontFamilyComboBoxModel = new DefaultComboBoxModel<>(fonts);
fontFamilyComboBox = new JComboBox<String>(fontFamilyComboBoxModel);
fontFamilyComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
String name = (String) fontFamilyComboBox.getSelectedItem();
new StyledEditorKit.FontFamilyAction("font-family-" + name, name).actionPerformed(ae);
editor.requestFocus();
}
});
final String[] fontSizes = { "Font Size", "10", "11", "12", "14", "16", "18", "20", "24", "28", "30", "34",
"40", "50" };
fontSizeComboBox = new JComboBox<String>(fontSizes);
fontSizeComboBox.setEditable(false);
JFrame frame = new JFrame("Text Editor");
frame.add(fontFamilyComboBox, BorderLayout.SOUTH);
frame.add(fontSizeComboBox, BorderLayout.NORTH);
frame.add(editorScrollPane, BorderLayout.CENTER);
frame.add(editorScrollPane);
frame.setSize(800, 400);
frame.setLocation(300, 150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
editor.requestFocusInWindow();
}
private void initDocAttrs() {
Style style = doc.addStyle("my_doc_style", null);
StyleConstants.setFontSize(style, 12);
StyleConstants.setFontFamily(style, "Arial");
doc.setParagraphAttributes(5, doc.getLength(), style, true);
}
private class MyCaretListener implements CaretListener {
@Override
public void caretUpdate(CaretEvent e) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
attrs = ((StyledEditorKit) editor.getEditorKit()).getInputAttributes();
System.out.println(attrs);
fontFamilyStr = (String) attrs.getAttribute(StyleConstants.FontFamily);
System.out.println("Font: " + fontFamilyStr);
fontFamilyComboBox.setSelectedItem(fontFamilyStr);
}
});
System.out.println("---");
}
}
}
为什么按Ctrl + A会更改所有字体样式?有人可以帮我吗?
答案 0 :(得分:2)
fontFamilyComboBox.setSelectedItem(fontFamilyStr);
在CaretListener
中,您正在更改选定的项目,这将导致ActionListener
调用组合框,这会导致您更改选定文本的字体。
一种解决方案是在更改所选项目之前从组合框中删除ActionListner:
comboBox.removeActionListener(...);
comboBox.setSelectedItem(fontFamilyStr);
comboBox.addActionListener(..)