繁星coding
这基本上解决了如何选择与所选文本或文本中的光标位置相对应的组合框项的问题。例如,我只选择了字体大小。同样的技术也可以应用于字体系列。该示例是使用的文本编辑器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; } }}