H
Hunger
Gast
Ich habe ein JApplet, mit zahlreichen buttons etc (eine art wysiwyg editor für mein forum soll es werden.)
aber wenn ich das applet starte, dann sind alle objekte unsichtbar. erst wenn ich mit der maus über sie fahre sehe ich sie.
danke für eure hilfe.
Code:
aber wenn ich das applet starte, dann sind alle objekte unsichtbar. erst wenn ich mit der maus über sie fahre sehe ich sie.
danke für eure hilfe.
Code:
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.event.*;
import java.net.*;
public class Editor extends JApplet {
String text;
JEditorPane pane;
int ohneTags;
int mitTags;
boolean first=true;
public void start(){
resize(620,500);
}
public void paint(Graphics g){
initToolbar();
text ="<font style='font-family:arial,sans-serif'>"+getParameter( "text" );
pane = new JEditorPane();
pane.setContentType("text/html");
pane.setText(text);
JScrollPane scrollPane = new JScrollPane(pane);
scrollPane.setVisible(true);
scrollPane.setBounds(10,10,500,300);
add(scrollPane);
pane.setVisible(true);
pane.setBounds(0,0,250,100);
scrollPane.setWheelScrollingEnabled(true);
pane.setBackground(new Color(220,220,220));
//resize(620,500);
}
private void initToolbar() {
JToolBar toolBar = new JToolBar();
JButton speichernButton = new JButton("[#]");
speichernButton.addActionListener(speichern);
toolBar.add(speichernButton);
JButton fettButton = new JButton("F");
fettButton.addActionListener(fett);
toolBar.add(fettButton);
JButton kursivButton = new JButton("K");
kursivButton.addActionListener(kursiv);
toolBar.add(kursivButton);
JButton rotButton = new JButton("Rot");
rotButton.addActionListener(rot);
toolBar.add(rotButton);
getContentPane().add(toolBar, BorderLayout.NORTH);
repaint();
}
ActionListener fett= new ActionListener() {
public void actionPerformed( ActionEvent e ) {
addTag("b");
}
};
ActionListener kursiv= new ActionListener() {
public void actionPerformed( ActionEvent e ) {
addTag("i");
}
};
ActionListener rot= new ActionListener() {
public void actionPerformed( ActionEvent e ) {
addTag("font color=#ff0000");
}
};
ActionListener speichern= new ActionListener() {
public void actionPerformed( ActionEvent e ) {
try {
speichern(pane.getText(),new URL("http://www.manueldewald.de.ms"));
}catch(MalformedURLException e2) {
System.out.println("Error " + e2 );
}
}
};
public int gibEchtePosition(String text, int position){
boolean inTag=false;
ohneTags=0;
mitTags=0;
while(ohneTags<position){
char zeichen=text.charAt(mitTags);
//System.out.println("Zeichen an Stelle "+mitTags+" : "+zeichen);
if(zeichen=='<'){
inTag=true;
}
if(!inTag){
ohneTags++;
}
if(zeichen=='>'){
inTag=false;
}
mitTags++;
}
//System.out.println("mittags: "+mitTags);
//System.out.println("ohnetags: "+ohneTags);
int echtePosition=mitTags-1;
return echtePosition;
}
public void addTag(String tag){
text=pane.getText().replaceAll("<html>\n <head>\n \n </head>\n <body>\n ","");
String textneu="";
if(pane.getSelectedText() != null){
if(!offenerTag(tag,text.substring(0,gibEchtePosition(text,pane.getSelectionStart())))){
textneu=text.substring(0,gibEchtePosition(text,pane.getSelectionStart()))
+ "<" + tag + ">"
+ pane.getSelectedText()
+ "</" + tag + ">"
+ text.substring(gibEchtePosition(text,pane.getSelectionEnd()));
;
}else{
textneu=text.substring(0,gibEchtePosition(text,pane.getSelectionStart()))
+ "</" + tag + ">"
+ pane.getSelectedText()
+ text.substring(gibEchtePosition(text,pane.getSelectionEnd()));
;
}
}else{
if(!offenerTag(tag,text.substring(0,gibEchtePosition(text,pane.getSelectionStart())))){
textneu=text.substring(0,gibEchtePosition(text,pane.getSelectionStart())) + "<" + tag + ">"
+ text.substring(gibEchtePosition(text,pane.getSelectionStart()));
}else{
textneu=text.substring(0,gibEchtePosition(text,pane.getSelectionStart())) + "</" + tag + ">"
+ text.substring(gibEchtePosition(text,pane.getSelectionStart()));
}
}
pane.setText(textneu);
}
public boolean offenerTag(String tag,String text){
boolean weiterSuchen=true;
String alterText=text;
int offenTag=0;
int schliessenTag=0;
while(weiterSuchen){
String neuerText=alterText.replaceFirst("<"+tag+">","");
if(!alterText.equals(neuerText)){
weiterSuchen=true;
offenTag++;
}else{
weiterSuchen=false;
}
alterText=neuerText;
//System.out.println(neuerText);
}
weiterSuchen=true;
while(weiterSuchen){
String neuerText=alterText.replaceFirst("</"+tag+">","");
if(!alterText.equals(neuerText)){
weiterSuchen=true;
schliessenTag++;
}else{
weiterSuchen=false;
}
alterText=neuerText;
//System.out.println(neuerText);
}
boolean offenerTag;
if(offenTag>schliessenTag){
offenerTag=true;
}else{
offenerTag=false;
}
return offenerTag;
}
public void speichern(String text, URL url1){
getAppletContext().showDocument(url1);
}
}