java - 如何在放置光标或选择 JTextPane 中的文本时更改 JCombobox 中相应的字体名称?
问题描述
我正在 Java Swing 中创建一个文本编辑器应用程序。我正在使用 JTextPane,并且添加了代码来获取 JComboBox 中的所有系统字体和一些字体大小。
我在 jtextpane 中输入了文本 - “Hello World”,并将“Hello”字样的字体更改为“Arial”,字号为 10,“World”为“Calibri”,字号为 12。
我的预期场景:如果我选择单词“Hello”或将光标放在单词“Hello”上,字体 JCombobox 中的字体名称应自动更改为“Arial”,字体大小 Jcombobox 应自动更改为 10,如与单词“World”相同,Jcombobox 中的值应更改为“Calibri”和“12”。我怎样才能做到这一点?提前致谢。
解决方案
这基本上解决了如何选择与所选文本或文本中的光标位置对应的组合框项的问题。例如,我只选择了字体大小。同样的技术也可以应用于字体系列。
该示例是一个使用文本编辑器的文本编辑器JTextPane
,其文档类型为DefaultStyledDocument
. 有一个JComboBox
字体大小列表(16 到 50)。可以在编辑器中选择一段文本,然后从组合框中选择字体大小以将文本设置为该字体大小。这是通过ItemListener
添加到JComboBox
. 侦听器具有将编辑器文档的属性设置为新选择的字体大小的代码 - 设置为选定的文本。
编辑器允许将多种字体大小应用于文本的不同部分,如下图所示。
要求是当插入符号(或光标)放置在文本或选定文本的一部分中时 - 需要在字体大小中设置相应JComboBox
的字体大小。为此,将 aCaretListener
添加到JTextPane
.
这个监听器的逻辑主要定位插入符号的位置,获取该位置的文档文本的属性,并提取字体大小属性。此属性的字体大小在字体大小组合框中设置。
示例代码:
public class Editor2 {
private JTextPane editor;
private DefaultStyledDocument doc;
private JComboBox<String> fontSizeComboBox;
private boolean caretControlFlag;
private static final int DEFAULT_FONT_SIZE = 18;
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));
editor.addCaretListener(new MyCaretListener());
JScrollPane editorScrollPane = new JScrollPane(editor);
doc = new DefaultStyledDocument();
initDocAttrs();
editor.setDocument(doc);
final String [] fontSizes = {"Font Size", "16", "18",
"20", "24", "28", "30", "34", "40", "50"};
fontSizeComboBox = new JComboBox<String>(fontSizes);
fontSizeComboBox.setEditable(false);
fontSizeComboBox.addItemListener(new FontSizeItemListener());
JFrame frame = new JFrame("Text Editor");
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, 18);
StyleConstants.setFontFamily(style, "SansSerif");
doc.setParagraphAttributes(0, doc.getLength(), style, true);
}
private class FontSizeItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
if ((e.getStateChange() != ItemEvent.SELECTED) ||
(fontSizeComboBox.getSelectedIndex() == 0)) {
return;
}
String fontSizeStr = (String) e.getItem();
int newFontSize = 0;
try {
newFontSize = Integer.parseInt(fontSizeStr);
}
catch (NumberFormatException ex) {
return;
}
if (caretControlFlag) {
caretControlFlag = false;
return;
}
setFontAttrs(newFontSize);
editor.requestFocusInWindow();
}
private void setFontAttrs(int newFontSize) {
SimpleAttributeSet attrs = new SimpleAttributeSet();
Style docStyle = doc.getStyle("my_doc_style");
int size = StyleConstants.getFontSize(docStyle);
StyleConstants.setFontSize(attrs, newFontSize);
String attrName = "mysize" + Integer.toString(newFontSize);
attrs.addAttribute(attrName, attrName);
int startPos = editor.getSelectionStart();
String selectedText = editor.getSelectedText();
if (selectedText == null || selectedText.trim().isEmpty()) {
return;
}
int length = selectedText.length();
doc.setCharacterAttributes(startPos, length, attrs, false);
editor.setDocument(doc);
}
}
private class MyCaretListener implements CaretListener {
@Override
public void caretUpdate(CaretEvent e) {
caretControlFlag = true;
int dot = e.getDot();
Element ele = doc.getCharacterElement(dot);
AttributeSet attrs = ele.getAttributes();
String fontSizeStr = "18";
for (Enumeration en = attrs.getAttributeNames(); en.hasMoreElements();) {
String attrName = en.nextElement().toString();
if (attrName.contains("mysize")) {
fontSizeStr = attrName.substring(6);
}
}
fontSizeComboBox.setSelectedItem(fontSizeStr);
caretControlFlag = false;
}
}
}
推荐阅读
- api - Outlook Add-in 365 - 此平台不支持 API
- reactjs - 高阶组件可以更改子组件以使其可编辑吗?
- c++ - 声明自动函数会导致未定义的错误
- javascript - 创建带有阈值线的胜利图
- java - 为什么 .contains(1) 在 HashSet 上调用时返回 false
包含1l? - wordpress - 是否可以链接到 add_theme_support( 'editor-color-palette' ) 中的数组文件;
- python - 如何循环浏览已定义的元素列表
- json - 在 Terraform 变量中使用 JSON 格式的字符串
- json - Spark导入拆分JSON有效负载
- node.js - 如何按其中一个键的值对对象进行排序