【问题标题】:Line break attribute JEditorPane换行属性 JEditorPane
【发布时间】:2012-10-24 23:22:25
【问题描述】:

有人知道如何更改JEditorPane 的换行符属性吗?

我无法在我的 JTextPanes 文本中找到换行符(它既不是 \n 也不是 \r),所以我无法计算数字此文本的行数正确。我想更改 \n 的换行符属性。

【问题讨论】:

  • 编辑器的内容类型是什么??
  • 内容类型为text/plain

标签: java swing jeditorpane word-wrap


【解决方案1】:

这个例子对我有用...

public class TestEditorPane {

    public static void main(String[] args) {
        new TestEditorPane();
    }

    public TestEditorPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new EditorPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class EditorPane extends JPanel {

        private JEditorPane editor;

        public EditorPane() {

            setLayout(new GridBagLayout());
            editor = new JEditorPane();
            editor.setContentType("text/plain");

            JButton button = new JButton("Dump");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = editor.getText();
                    String[] parts = text.split(System.getProperty("line.separator"));
//                    String[] parts = text.split("\n\r");
                    for (String part : parts) {
                        if (part.trim().length() > 0) {
                            System.out.println(part);
                        }
                    }
                }
            });

            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.weighty = 1;
            gbc.fill = GridBagConstraints.BOTH;
            add(editor, gbc);

            gbc.gridx = 0;
            gbc.gridy++;
            gbc.weightx = 0;
            gbc.weighty = 0;
            gbc.fill = GridBagConstraints.NONE;
            add(button, gbc);
        }
    }
}

windows下,换行符好像是/n/r。我也使用了系统属性line.separator,它似乎对我有用。

【讨论】:

    【解决方案2】:

    使用javax.swing.text.Utilities.getRowStart()/getRowEnd() 计算行数。

    事实上,当文本被换行时,没有插入任何字符。请参阅 http://java-sl.com/wrap.html 以了解 wrap 的工作原理。

    【讨论】:

    • 我是这么想的,没有插入char。我很了解 java-sl 上的 wrap 教程,由同一位作者回答很酷,谢谢 :) Utilities 类非常适合我的需要。
    猜你喜欢
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 2013-08-15
    相关资源
    最近更新 更多