猿问

如何在放置光标或在 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”。我怎样才能做到这一点?提前致谢。


撒科打诨
浏览 248回答 1
1回答

繁星coding

这基本上解决了如何选择与所选文本或文本中的光标位置相对应的组合框项的问题。例如,我只选择了字体大小。同样的技术也可以应用于字体系列。该示例是使用的文本编辑器JTextPane,其文档类型为DefaultStyledDocument.&nbsp;有一个JComboBox字体大小列表(16 到 50)。可以在编辑器中选择一段文本,然后从组合框中选择一种字体大小以将文本设置为该字体大小。这是使用ItemListener添加到JComboBox.&nbsp;侦听器具有将编辑器文档的属性设置为新选择的字体大小 - 所选文本的代码。编辑器允许将多种字体大小应用于文本的不同部分,如下图所示。要求是当插入符号(或光标)放置在文本或选定文本的一部分时 - 需要在字体大小中设置相应JComboBox的字体大小。为此,将 aCaretListener添加到JTextPane.这个监听器的逻辑主要是定位插入符号位置,获取该位置的文档文本的属性并提取字体大小属性。此属性的字体大小在字体大小组合框中设置。示例代码:public class Editor2 {&nbsp; &nbsp; private JTextPane editor;&nbsp; &nbsp; private DefaultStyledDocument doc;&nbsp; &nbsp; private JComboBox<String> fontSizeComboBox;&nbsp; &nbsp; private boolean caretControlFlag;&nbsp; &nbsp; private static final int DEFAULT_FONT_SIZE = 18;&nbsp; &nbsp; public static void main(String [] args) {&nbsp; &nbsp; &nbsp; &nbsp; SwingUtilities.invokeLater(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Editor2().createAndShowGUI();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; private void createAndShowGUI() {&nbsp; &nbsp; &nbsp; &nbsp; editor = new JTextPane();&nbsp; &nbsp; &nbsp; &nbsp; editor.setMargin(new Insets(5, 5, 5, 5));&nbsp; &nbsp; &nbsp; &nbsp; editor.addCaretListener(new MyCaretListener());&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; JScrollPane editorScrollPane = new JScrollPane(editor);&nbsp; &nbsp; &nbsp; &nbsp; doc = new DefaultStyledDocument();&nbsp; &nbsp; &nbsp; &nbsp; initDocAttrs();&nbsp; &nbsp; &nbsp; &nbsp; editor.setDocument(doc);&nbsp; &nbsp; &nbsp; &nbsp; final String [] fontSizes = {"Font Size", "16", "18",&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "20", "24", "28", "30", "34", "40", "50"};&nbsp; &nbsp; &nbsp; &nbsp; fontSizeComboBox = new JComboBox<String>(fontSizes);&nbsp; &nbsp; &nbsp; &nbsp; fontSizeComboBox.setEditable(false);&nbsp; &nbsp; &nbsp; &nbsp; fontSizeComboBox.addItemListener(new FontSizeItemListener());&nbsp; &nbsp; &nbsp; &nbsp; JFrame frame = new JFrame("Text Editor");&nbsp; &nbsp; &nbsp; &nbsp; frame.add(fontSizeComboBox, BorderLayout.NORTH);&nbsp; &nbsp; &nbsp; &nbsp; frame.add(editorScrollPane, BorderLayout.CENTER);&nbsp; &nbsp; &nbsp; &nbsp; frame.add(editorScrollPane);&nbsp; &nbsp; &nbsp; &nbsp; frame.setSize(800, 400);&nbsp; &nbsp; &nbsp; &nbsp; frame.setLocation(300, 150);&nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp; editor.requestFocusInWindow();&nbsp; &nbsp; }&nbsp; &nbsp; private void initDocAttrs() {&nbsp; &nbsp; &nbsp; &nbsp; Style style = doc.addStyle("my_doc_style", null);&nbsp; &nbsp; &nbsp; &nbsp; StyleConstants.setFontSize(style, 18);&nbsp; &nbsp; &nbsp; &nbsp; StyleConstants.setFontFamily(style, "SansSerif");&nbsp; &nbsp; &nbsp; &nbsp; doc.setParagraphAttributes(0, doc.getLength(), style, true);&nbsp; &nbsp; }&nbsp; &nbsp; private class FontSizeItemListener implements ItemListener {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void itemStateChanged(ItemEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ((e.getStateChange() != ItemEvent.SELECTED) ||&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (fontSizeComboBox.getSelectedIndex() == 0)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fontSizeStr = (String) e.getItem();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int newFontSize = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newFontSize = Integer.parseInt(fontSizeStr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (NumberFormatException ex) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (caretControlFlag) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; caretControlFlag = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setFontAttrs(newFontSize);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.requestFocusInWindow();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; private void setFontAttrs(int newFontSize) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SimpleAttributeSet attrs = new SimpleAttributeSet();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Style docStyle = doc.getStyle("my_doc_style");&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int size = StyleConstants.getFontSize(docStyle);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StyleConstants.setFontSize(attrs, newFontSize);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String attrName = "mysize" + Integer.toString(newFontSize);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; attrs.addAttribute(attrName, attrName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int startPos = editor.getSelectionStart();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String selectedText = editor.getSelectedText();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (selectedText == null || selectedText.trim().isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int length = selectedText.length();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doc.setCharacterAttributes(startPos, length, attrs, false);&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editor.setDocument(doc);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private class MyCaretListener implements CaretListener {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void caretUpdate(CaretEvent e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; caretControlFlag = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int dot = e.getDot();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Element ele = doc.getCharacterElement(dot);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AttributeSet attrs = ele.getAttributes();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String fontSizeStr = "18";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Enumeration en = attrs.getAttributeNames(); en.hasMoreElements();) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String attrName = en.nextElement().toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (attrName.contains("mysize")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fontSizeStr = attrName.substring(6);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fontSizeComboBox.setSelectedItem(fontSizeStr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; caretControlFlag = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答