在RSyntaxTextArea中输入的文本以奇怪的编码显示



在我的项目中,我想有一个内置的java源代码编辑器,具有自动完成和语法高亮显示,我选择了RSyntaxTextArea。我试图从项目网站上运行极简主义的例子,但只要我使用实际的2.5.6版本,输入的文本就会以奇怪的字符编码当我把这个库的版本降级到2.0.2时,它就可以工作了,但是这个版本不支持我需要的代码完成。

我代码:

public class Main3 extends JFrame
{
    public Main3() {
    setRootPane(new DemoRootPane());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setTitle("RSyntaxTextArea Demo Application");
    pack();
}

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                UIManager.setLookAndFeel(UIManager.
                        getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
            } catch (Exception e) {
                e.printStackTrace(); // Never happens
            }
            Toolkit.getDefaultToolkit().setDynamicLayout(true);
            new Main3().setVisible(true);
        }
    });
}

}


import org.fife.ui.rsyntaxtextarea.ErrorStrip;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.fife.ui.rtextarea.Gutter;
import org.fife.ui.rtextarea.RTextScrollPane;
import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class DemoRootPane extends JRootPane implements HyperlinkListener,
    SyntaxConstants
{
private RTextScrollPane scrollPane;
private RSyntaxTextArea textArea;

public DemoRootPane()
{
    textArea = createTextArea();
    setText("JavaExample.txt");
    textArea.setSyntaxEditingStyle(SYNTAX_STYLE_JAVA);
    scrollPane = new RTextScrollPane(textArea, true);
    Gutter gutter = scrollPane.getGutter();
    gutter.setBookmarkingEnabled(true);
    URL url = getClass().getClassLoader().getResource("bookmark.png");
//        gutter.setBookmarkIcon(new ImageIcon(url));
    getContentPane().add(scrollPane);
    ErrorStrip errorStrip = new ErrorStrip(textArea);
    getContentPane().add(errorStrip, BorderLayout.LINE_END);
    setJMenuBar(createMenuBar());
}

private void addSyntaxItem(String name, String res, String style,
                           ButtonGroup bg, JMenu menu)
{
    JRadioButtonMenuItem item = new JRadioButtonMenuItem(
            new ChangeSyntaxStyleAction(name, res, style));
    bg.add(item);
    menu.add(item);
}

private void addThemeItem(String name, String themeXml, ButtonGroup bg,
                          JMenu menu)
{
    JRadioButtonMenuItem item = new JRadioButtonMenuItem(
            new ThemeAction(name, themeXml));
    bg.add(item);
    menu.add(item);
}

private JMenuBar createMenuBar()
{
    JMenuBar mb = new JMenuBar();
    JMenu menu = new JMenu("Language");
    ButtonGroup bg = new ButtonGroup();
    addSyntaxItem("C", "CExample.txt", SYNTAX_STYLE_C, bg, menu);
    addSyntaxItem("Java", "JavaExample.txt", SYNTAX_STYLE_JAVA, bg, menu);
    addSyntaxItem("Perl", "PerlExample.txt", SYNTAX_STYLE_PERL, bg, menu);
    addSyntaxItem("Ruby", "RubyExample.txt", SYNTAX_STYLE_RUBY, bg, menu);
    addSyntaxItem("SQL", "SQLExample.txt", SYNTAX_STYLE_SQL, bg, menu);
    addSyntaxItem("XML", "XMLExample.txt", SYNTAX_STYLE_XML, bg, menu);
    menu.getItem(1).setSelected(true);
    mb.add(menu);
    menu = new JMenu("View");
    JCheckBoxMenuItem cbItem = new JCheckBoxMenuItem(new CodeFoldingAction());
    cbItem.setSelected(true);
    menu.add(cbItem);
    cbItem = new JCheckBoxMenuItem(new ViewLineHighlightAction());
    cbItem.setSelected(true);
    menu.add(cbItem);
    cbItem = new JCheckBoxMenuItem(new ViewLineNumbersAction());
    cbItem.setSelected(true);
    menu.add(cbItem);
    cbItem = new JCheckBoxMenuItem(new AnimateBracketMatchingAction());
    cbItem.setSelected(true);
    menu.add(cbItem);
    cbItem = new JCheckBoxMenuItem(new BookmarksAction());
    cbItem.setSelected(true);
    menu.add(cbItem);
    cbItem = new JCheckBoxMenuItem(new WordWrapAction());
    menu.add(cbItem);
    cbItem = new JCheckBoxMenuItem(new ToggleAntiAliasingAction());
    cbItem.setSelected(true);
    menu.add(cbItem);
    cbItem = new JCheckBoxMenuItem(new MarkOccurrencesAction());
    cbItem.setSelected(true);
    menu.add(cbItem);
    cbItem = new JCheckBoxMenuItem(new TabLinesAction());
    menu.add(cbItem);
    mb.add(menu);
    bg = new ButtonGroup();
    menu = new JMenu("Themes");
    addThemeItem("Default", "/default.xml", bg, menu);
    addThemeItem("Default (Alternate)", "/default-alt.xml", bg, menu);
    addThemeItem("Dark", "/dark.xml", bg, menu);
    addThemeItem("Eclipse", "/eclipse.xml", bg, menu);
    addThemeItem("IDEA", "/idea.xml", bg, menu);
    addThemeItem("Visual Studio", "/vs.xml", bg, menu);
    mb.add(menu);
    menu = new JMenu("Help");
    JMenuItem item = new JMenuItem(new AboutAction());
    menu.add(item);
    mb.add(menu);
    return mb;
}

/**
 * Creates the text area for this application.
 *
 * @return The text area.
 */
private RSyntaxTextArea createTextArea()
{
    RSyntaxTextArea textArea = new RSyntaxTextArea(25, 70);
    textArea.setTabSize(3);
    textArea.setCaretPosition(0);
    textArea.addHyperlinkListener(this);
    textArea.requestFocusInWindow();
    textArea.setMarkOccurrences(true);
    textArea.setCodeFoldingEnabled(true);
    textArea.setClearWhitespaceLinesEnabled(false);
    return textArea;
}

/**
 * Focuses the text area.
 */
void focusTextArea()
{
    textArea.requestFocusInWindow();
}

/**
 * Called when a hyperlink is clicked in the text area.
 *
 * @param e The event.
 */
public void hyperlinkUpdate(HyperlinkEvent e)
{
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED)
    {
        URL url = e.getURL();
        if (url == null)
        {
            UIManager.getLookAndFeel().provideErrorFeedback(null);
        }
        else
        {
            JOptionPane.showMessageDialog(this,
                    "URL clicked:n" + url.toString());
        }
    }
}

/**
 * Sets the content in the text area to that in the specified resource.
 *
 * @param resource The resource to load.
 */
private void setText(String resource)
{
    ClassLoader cl = getClass().getClassLoader();
    BufferedReader r = null;
    try
    {
        r = new BufferedReader(new InputStreamReader(
                cl.getResourceAsStream(resource), "UTF-8"));
        textArea.read(r, null);
        r.close();
        textArea.setCaretPosition(0);
        textArea.discardAllEdits();
    }
    catch (RuntimeException re)
    {
        throw re; // FindBugs
    }
    catch (Exception e)
    { // Never happens
        textArea.setText("Type here to see syntax highlighting");
    }
}

private class AboutAction extends AbstractAction
{
    public AboutAction()
    {
        putValue(NAME, "About RSyntaxTextArea...");
    }
    public void actionPerformed(ActionEvent e)
    {
        JOptionPane.showMessageDialog(DemoRootPane.this,
                "<html><b>RSyntaxTextArea</b> - A Swing syntax highlighting text component" +
                        "<br>Version 2.0.7" +
                        "<br>Licensed under a modified BSD license",
                "About RSyntaxTextArea",
                JOptionPane.INFORMATION_MESSAGE);
    }
}

private class AnimateBracketMatchingAction extends AbstractAction
{
    public AnimateBracketMatchingAction()
    {
        putValue(NAME, "Animate Bracket Matching");
    }
    public void actionPerformed(ActionEvent e)
    {
        textArea.setAnimateBracketMatching(
                !textArea.getAnimateBracketMatching());
    }
}

private class BookmarksAction extends AbstractAction
{
    public BookmarksAction()
    {
        putValue(NAME, "Bookmarks");
    }
    public void actionPerformed(ActionEvent e)
    {
        scrollPane.setIconRowHeaderEnabled(
                !scrollPane.isIconRowHeaderEnabled());
    }
}

private class ChangeSyntaxStyleAction extends AbstractAction
{
    private String res;
    private String style;
    public ChangeSyntaxStyleAction(String name, String res, String style)
    {
        putValue(NAME, name);
        this.res = res;
        this.style = style;
    }
    public void actionPerformed(ActionEvent e)
    {
        setText(res);
        textArea.setCaretPosition(0);
        textArea.setSyntaxEditingStyle(style);
    }
}

private class CodeFoldingAction extends AbstractAction
{
    public CodeFoldingAction()
    {
        putValue(NAME, "Code Folding");
    }
    public void actionPerformed(ActionEvent e)
    {
        textArea.setCodeFoldingEnabled(!textArea.isCodeFoldingEnabled());
    }
}

private class MarkOccurrencesAction extends AbstractAction
{
    public MarkOccurrencesAction()
    {
        putValue(NAME, "Mark Occurrences");
    }
    public void actionPerformed(ActionEvent e)
    {
        textArea.setMarkOccurrences(!textArea.getMarkOccurrences());
    }
}

private class TabLinesAction extends AbstractAction
{
    private boolean selected;
    public TabLinesAction()
    {
        putValue(NAME, "Tab Lines");
    }
    public void actionPerformed(ActionEvent e)
    {
        selected = !selected;
        textArea.setPaintTabLines(selected);
    }
}

private class ThemeAction extends AbstractAction
{
    private String xml;
    public ThemeAction(String name, String xml)
    {
        putValue(NAME, name);
        this.xml = xml;
    }
    public void actionPerformed(ActionEvent e)
    {
        InputStream in = getClass().getResourceAsStream(xml);
        try
        {
            Theme theme = Theme.load(in);
            theme.apply(textArea);
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
    }
}

private class ToggleAntiAliasingAction extends AbstractAction
{
    public ToggleAntiAliasingAction()
    {
        putValue(NAME, "Anti-Aliasing");
    }
    public void actionPerformed(ActionEvent e)
    {
        textArea.setAntiAliasingEnabled(!textArea.getAntiAliasingEnabled());
    }
}

private class ViewLineHighlightAction extends AbstractAction
{
    public ViewLineHighlightAction()
    {
        putValue(NAME, "Current Line Highlight");
    }
    public void actionPerformed(ActionEvent e)
    {
        textArea.setHighlightCurrentLine(
                !textArea.getHighlightCurrentLine());
    }
}

private class ViewLineNumbersAction extends AbstractAction
{
    public ViewLineNumbersAction()
    {
        putValue(NAME, "Line Numbers");
    }
    public void actionPerformed(ActionEvent e)
    {
        scrollPane.setLineNumbersEnabled(
                !scrollPane.getLineNumbersEnabled());
    }
}

private class WordWrapAction extends AbstractAction
{
    public WordWrapAction()
    {
        putValue(NAME, "Word Wrap");
    }
    public void actionPerformed(ActionEvent e)
    {
        textArea.setLineWrap(!textArea.getLineWrap());
    }
}
}

这个问题可以通过在createTextArea()

中为RSyntaxTextArea设置一个具体的字体来解决

textArea.setFont(new Font("Monospaced", Font.PLAIN, 12));

相关内容

  • 没有找到相关文章

最新更新