Un applet revoltoso.
Publicado por Daniel (10 intervenciones) el 05/08/2007 18:03:52
El siguiente código es un applet que muestra un textarea con un texto por defecto, y un botón. El usuario puede seleccionar una parte del texto del textarea, y teniéndola seleccionada, al pulsar sobre el botón, se sustituirá el texto seleccionado por la frase "Hola desde java". La cosa funciona cuando selecciono parte del texto por defecto, pero si añado texto extra al textarea (cuando se está ejecutando el applet), y selecciono una parte de este texto, entonces hace el reemplazo anormalmente. El resultado no es el deseado. Espero puedan ayudarme:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class AREADETEXTO1 extends Applet implements ActionListener {
TextArea areaTexto;
Button boton;
public void init() {
areaTexto = new TextArea("Ya es la hora.",5,20,TextArea.SCROLLBARS_BOTH);
add(areaTexto);
boton = new Button("Haga clic aquí");
add(boton);
boton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boton) {
areaTexto.replaceRange("Hola desde Java",areaTexto.getSelectionStart(),areaTexto.getSelectionEnd());
}
}
}
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class AREADETEXTO1 extends Applet implements ActionListener {
TextArea areaTexto;
Button boton;
public void init() {
areaTexto = new TextArea("Ya es la hora.",5,20,TextArea.SCROLLBARS_BOTH);
add(areaTexto);
boton = new Button("Haga clic aquí");
add(boton);
boton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == boton) {
areaTexto.replaceRange("Hola desde Java",areaTexto.getSelectionStart(),areaTexto.getSelectionEnd());
}
}
}
Valora esta pregunta


0