【问题标题】:Set Font Size of selected text in JEditorPane Using JComboBox使用 JComboBox 在 JEditorPane 中设置选定文本的字体大小
【发布时间】:2012-07-01 18:31:24
【问题描述】:

如何在JEditorPane(窗格)中设置选定文本的字体大小使用JComboBox?

我以前用过:

toolbar.add(new StyledEditorKit.FontSizeAction("12", 12));

但你不能简单地拥有数百个按钮。

【问题讨论】:

  • 嗯...我想出了如何从 JComboBox 中获取一个 int...有什么方法可以使用 INT 设置所选文本的字体?
  • 找到一个不错的演示:stackoverflow.com/questions/939109/…

标签: java swing cross-platform font-size jeditorpane


【解决方案1】:

我不知道执行此操作的规范方法,但在实验中,这是可行的:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Action;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.Document;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyledEditorKit;

public class EditorPaneFun extends JPanel {
   private static final Integer[] ITEMS = { 9, 10, 11, 12, 14, 16, 18, 20, 24,
         32 };
   private JEditorPane editorPane = new JEditorPane();
   private JComboBox<Integer> fontBox = new JComboBox<Integer>(ITEMS);
   private StyledDocument doc = new DefaultStyledDocument();
   private StyledEditorKit styledEditorKit = new StyledEditorKit();

   public EditorPaneFun() {
      editorPane.setDocument(doc);
      editorPane.setEditorKit(styledEditorKit);
      JScrollPane scrollpane = new JScrollPane(editorPane);
      scrollpane.setPreferredSize(new Dimension(500, 400));
      JPanel comboPanel = new JPanel();
      comboPanel.add(fontBox);

      setLayout(new BorderLayout());
      add(scrollpane, BorderLayout.CENTER);
      add(comboPanel, BorderLayout.SOUTH);

      Document doc = editorPane.getDocument();
      for (int i = 0; i < 20; i++) {
         int offset = doc.getLength();
         String str = "This is line number: " + i + "\n";
         try {
            doc.insertString(offset, str, null);
         } catch (BadLocationException e) {
            e.printStackTrace();
         }
      }

      fontBox.addActionListener(new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            int size = (Integer) fontBox.getSelectedItem();
            Action fontAction = new StyledEditorKit.FontSizeAction(String
                  .valueOf(size), size);
            fontAction.actionPerformed(e);
         }
      });
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("EditorPaneFun");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new EditorPaneFun());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

请注意,这仅在 Document 是 DefaultStyledDocument 并且 EditorKit 是 StyledEditorKit 时才有效。

【讨论】:

  • 不,这不好。最好使用setCharacterAttributes(...)...等等。
  • 这个问题是你不能选择文本并这样做,所以字体大小没有改变......
  • 在这里找到答案:stackoverflow.com/questions/939109/…
  • @user1332495:设置属性没有错误,但Action更灵活。如果实例已经存在,您可以在多个上下文中使用它或转发事件,如here所示。
  • 啊,谢谢!我使用了不同的方法。然而,我的电脑崩溃了,我失去了一切,所以我又看了看这个。这实际上效果更好/更简单。谢谢!
猜你喜欢
  • 2012-06-29
  • 2012-06-29
  • 2013-09-13
  • 2011-06-28
  • 1970-01-01
  • 2014-04-15
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多