JSyntaxPane 是一个带语法高亮支持的代码编辑 UI 控件,扩展了 JEditorKit。
使用方法:
import java.awt.BorderLayout; import java.awt.Container; import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.WindowConstants; import jsyntaxpane.DefaultSyntaxKit; public class SyntaxTester { public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new SyntaxTester().setVisible(true); } }); } public SyntaxTester() { JFrame f = new JFrame(TestIssue47.class.getName()); final Container c = f.getContentPane(); c.setLayout(new BorderLayout()); DefaultSyntaxKit.initKit(); final JEditorPane codeEditor = new JEditorPane(); JScrollPane scrPane = new JScrollPane(codeEditor); c.add(scrPane, BorderLayout.CENTER); c.doLayout(); codeEditor.setContentType("text/java"); codeEditor.setText("public static void main(String[] args) {\n}"); f.setSize(800, 600); f.setVisible(true); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } }