简体   繁体   English

如何在JEditorPane中编辑文本字体

[英]How to edit text font in JEditorPane

I have a JEditorPane where it is loaded a txt file. 我有一个JEditorPane ,在其中加载了txt文件。 I want to have a button that changes the font size. 我想要一个可以更改字体大小的按钮。 I have found about StyledEditorKit but i am not able to use it. 我发现了有关StyledEditorKit但我无法使用它。 I am very new to java and its kind of difficult for me. 我对Java非常陌生,这对我来说有点困难。

     public JFrame init() {
    JFrame frame = new JFrame("Pagination");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JEditorPane editor = new JEditorPane();
    editor.setEditorKit(this);

    editor.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent e) {
            if (!isPageBreakInsertion ) {
                ( (StyledEditorKit) editor.getEditorKit()).getInputAttributes().removeAttribute(PAGE_BREAK_ATTR_NAME);
            }
        }
    });



    JMenu fontMenu = new JMenu("Font Size");
    for (int i = 48; i >= 8; i -= 10) {
      JMenuItem menuItem = new JMenuItem("" + i);
      // add an action
      menuItem
          .addActionListener(new StyledEditorKit.FontSizeAction(
              "myaction-" + i, i));
      fontMenu.add(menuItem);
    }
    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fontMenu);

    frame.setJMenuBar(menuBar);
    this.setHeader(createHeader());
    this.setFooter(createFooter());
    PageFormat pf = new PageFormat();
    pf.setPaper(new Paper());

    final PaginationPrinter pp = new PaginationPrinter(pf, editor);
    JScrollPane scroll = new JScrollPane(editor);

    frame.getContentPane().add(scroll);
    JToolBar tb=new JToolBar();
    JButton bPrint = new JButton("Print to default printer");
    bPrint.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            print(editor, pp);
        }
    });
    JButton bInsertPageBreak = new JButton("Insert page break");
    bInsertPageBreak.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            insertPageBreak(editor);
        }
    });

    File file = new File("uuu.txt");

    try {
        editor.setPage(file.toURI().toURL());
    } catch (MalformedURLException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    StyledDocument doc = (StyledDocument) editor.getDocument();

    // Create a style object and then set the style attributes
    Style style = doc.addStyle("StyleName", null);

    StyleConstants.setFontSize(style, 30);



    tb.add(bPrint);
    tb.add(bInsertPageBreak);
    frame.getContentPane().add(tb, BorderLayout.NORTH);
    frame.setBounds(new Rectangle(0, 23, 665, 789));

    return frame;
}

This is my code so far. 到目前为止,这是我的代码。 I have searched a lot for it but i cant find something that gives me a solution. 我已经搜寻了很多东西,但是找不到能给我解决方案的东西。 Also, i dont know exactly how to use the StyledEditorKit . 另外,我StyledEditorKit知道如何使用StyledEditorKit Thank you. 谢谢。

I have to keep the following code 我必须保留以下代码

final JEditorPane editor = new JEditorPane();
editor.setEditorKit(this);

because it creates the layout that i need, which is an A4 paper and sets the limits. 因为它会创建我需要的布局,这是A4纸并设置限制。 Also i have to add that my class extends StyledEditorKit. 另外,我还必须补充一点,我的课程扩展了StyledEditorKit。

I have a JEditorPane where it is loaded a txt file. 我有一个JEditorPane,在其中加载了txt文件。

For plain text you should use a JTextPane . 对于纯文本,应使用JTextPane JEditorPane is for HTML. JEdi​​torPane用于HTML。

Check out the section from the Swing tutorial on Text Component Features for a working example that allows you to change the font, color etc. of selected text in a JTextPane. 请查看Swing教程中有关文本组件功能的部分,获取一个有效的示例,该示例使您可以更改JTextPane中所选文本的字体,颜色等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM