Java Syntax Highlighting nach Beispiel

Leroux

Erfahrenes Mitglied
Hi ich hab das Syntax Highlighting Beispiel :http://www.tutorials.de/forum/swing...ane-anpassbares-java-syntax-highlighting.html

benutzt um mein EditorPane zum Syntax Highlighting zu bewegen, doch irgendwie haut das nicht so ganz hin

Java:
package UI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import UI.Syntax.CodeDocument;

public class NewJFrame extends javax.swing.JFrame {
	private JMenuBar jMenuBar1;
	private JPanel jPanel1;
	private JButton jButton1;
	private JEditorPane jEditorPane1;
	private JScrollPane jScrollPane1;
	private JButton jButton3;
	private JButton jButton2;
	private JTextField jTextField1;
	private JMenuItem jMenuItem1;
	private JMenu jMenu1;

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}


	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				NewJFrame inst = new NewJFrame();
//				inst.setUndecorated(true);
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public NewJFrame() {
		super();
		initGUI();
	}
	
	private void initSyntax(){
		CodeDocument doc = new CodeDocument(); 
		Map<String,Color> keywords = new HashMap<String,Color>(); 
		Color comment = new Color(63,197,95);  
		Color javadoc = new Color(63,95,191);   
		Color annotation = new Color(100,100,100);  
		doc.setCommentColor(comment); 
		doc.setJavadocColor(javadoc);   
		doc.setAnnotationColor(annotation);  
		Color defColor = new Color(127,0,85);
		keywords.put("abstract",defColor);
		keywords.put("boolean",defColor);  
		keywords.put("break",defColor);  
		keywords.put("byte",defColor);  
		keywords.put("case",defColor);  
		keywords.put("catch",defColor); 
		keywords.put("char",defColor);  
		keywords.put("class",defColor);   
		keywords.put("continue",defColor); 
		keywords.put("default",defColor);  
		keywords.put("do",defColor); 
		keywords.put("double",defColor);    
		keywords.put("enum",defColor);   
		keywords.put("extends",defColor); 
		keywords.put("else",defColor);   
		keywords.put("false",defColor);  
		keywords.put("final",defColor);  
		keywords.put("finally",defColor);
		keywords.put("float",defColor);   
		keywords.put("for",defColor);    
		keywords.put("if",defColor);    
		keywords.put("implements",defColor); 
		keywords.put("import",defColor);  
		keywords.put("instanceof",defColor);  
		keywords.put("int",defColor);    
		keywords.put("interface",defColor);  
		keywords.put("long",defColor);    
		keywords.put("native",defColor);   
		keywords.put("new",defColor);  
		keywords.put("null",defColor);   
		keywords.put("package",defColor); 
		keywords.put("private",defColor);  
		keywords.put("protected",defColor);
		keywords.put("public",defColor);   
		keywords.put("return",defColor);
		keywords.put("short",defColor);  
		keywords.put("static",defColor);  
		keywords.put("super",defColor);   
		keywords.put("switch",defColor);  
		keywords.put("synchronized",defColor); 
		keywords.put("this",defColor);  
		keywords.put("throw",defColor);   
		keywords.put("throws",defColor);  
		keywords.put("transient",defColor);    
		keywords.put("true",defColor);   
		keywords.put("try",defColor);   
		keywords.put("void",defColor);  
		keywords.put("volatile",defColor);
		keywords.put("while",defColor); 
		doc.setKeywords(keywords);  
		jEditorPane1.setDocument(doc);
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				jPanel1 = new JPanel();
				getContentPane().add(jPanel1, BorderLayout.CENTER);
				jPanel1.setLayout(null);
				{
					jTextField1 = new JTextField();
					jPanel1.add(jTextField1);
					jTextField1.setBounds(12, 12, 273, 21);
				}
				{
					jButton1 = new JButton();
					jPanel1.add(jButton1);
					jButton1.setText("...");
					jButton1.setBounds(298, 12, 38, 21);
				}
				{
					jButton2 = new JButton();
					jPanel1.add(jButton2);
					jButton2.setText("Compiliern");
					jButton2.setBounds(12, 387, 368, 21);
				}
				{
					jButton3 = new JButton();
					jPanel1.add(jButton3);
					jButton3.setText("Help");
					jButton3.setBounds(347, 12, 33, 21);
				}
				{
					jScrollPane1 = new JScrollPane();
					jPanel1.add(jScrollPane1);
					jScrollPane1.setBounds(12, 45, 368, 336);
					{
						jEditorPane1 = new JEditorPane();
						initSyntax();
						jScrollPane1.setViewportView(jEditorPane1);
					}
				}
			}
			{
				jMenuBar1 = new JMenuBar();
				setJMenuBar(jMenuBar1);
				{
					jMenu1 = new JMenu();
					jMenuBar1.add(jMenu1);
					jMenu1.setText("Datei");
					{
						jMenuItem1 = new JMenuItem();
						jMenu1.add(jMenuItem1);
						jMenuItem1.setText("Exit");
						jMenuItem1.setBounds(0, 0, 44, 18);
					}
				}
			}

			pack();
			this.setSize(402, 503);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

//EDIT Fehler gefunden es muss ein JTextPane und kein JEditorPane sein....
 
Zuletzt bearbeitet:

Neue Beiträge

Zurück