1

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

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

4

2 回答 2

3

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

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

于 2012-10-25T05:55:02.553 回答
3

这个例子对我有用......

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,它似乎对我有用。

于 2012-10-25T02:01:01.697 回答