Nieuw lid |
|
Ik denk dat ik een oplossing heb gevonden..:
Main.java
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class Main {
static boolean TagOpen = false;
static String Str = "";
static ColorPane pane = new ColorPane();
public static void main(String args[] ) {
String input = "<html><body>Hello World</body></html>";
loopString(input);
JFrame f = new JFrame("HTML editor");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new JScrollPane(pane));
f.setSize(600, 400);
f.setVisible(true);
}
public static void loopString(String txt) {
int length = txt.length();
for(int i = 0; i < length + 1; i++) {
if(i == 0) {
continue;
}
else {
ColorTags(txt.charAt(i-1));
}
}
}
public static void ColorTags(char c) {
String s = Character.toString(c);
if(s.equals("<")) {
TagOpen = true;
}
if(s.equals(">")) {
pane.append(Color.red, s);
TagOpen = false;
return;
}
if(TagOpen) {
pane.append(Color.red, s);
}
if(!TagOpen) {
pane.append(Color.black, s);
}
}
}
import java.awt.Color; import javax.swing.JFrame; import javax.swing.JScrollPane; public class Main { static boolean TagOpen = false; static ColorPane pane = new ColorPane(); public static void main (String args [] ) { String input = "<html><body>Hello World</body></html>"; loopString(input); f. setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); f.setSize(600, 400); f.setVisible(true); } public static void loopString (String txt ) { int length = txt.length(); for(int i = 0; i < length + 1; i++) { if(i == 0) { continue; } else { ColorTags(txt.charAt(i-1)); } } } public static void ColorTags(char c) { if(s.equals("<")) { TagOpen = true; } if(s.equals(">")) { pane. append(Color. red, s ); TagOpen = false; return; } if(TagOpen) { pane. append(Color. red, s ); } if(!TagOpen) { pane. append(Color. black, s ); } } }
ColorPane.java
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Color;
@SuppressWarnings("serial")
public class ColorPane extends JTextPane {
public void append(Color c, String s) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
int len = getDocument().getLength();
setCaretPosition(len);
setCharacterAttributes(aset, false);
replaceSelection(s);
}
}
import javax.swing.*; import javax.swing.text.*; import java.awt.Color; @SuppressWarnings("serial") int len = getDocument().getLength(); setCaretPosition(len); setCharacterAttributes(aset, false); replaceSelection(s); } }
|