Ciao abcd1000
wait() é un metodo che va associato a notify() e per implementarli occorre una gestione particolare.
siccome ogni applicazione di per se é un thread, potresti o estendere la classe Thread , ma credo che tu abbia già esteso JFrame , oppure implementare l'interfaccia Runnable.
Guarda ti faccio un regalino![]()
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Ritardo extends JFrame implements ActionListener, Runnable{
JTextField fl1 = new JTextField();
JTextArea ar1 = new JTextArea();
JButton bt1 = new JButton("ESEGUI");
Thread t;
public Ritardo(){
super("FRAME GENERICO");
fl1.setSize(80,20); fl1.setLocation(10,10);
ar1.setSize(400,200); ar1.setLocation(120,20);
bt1.setSize(80,20); bt1.setLocation(10,50);
JPanel pn = new JPanel();
pn.add(fl1); pn.add(ar1); pn.add(bt1);
setContentPane(pn);
bt1.addActionListener(this);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) { System.exit(0); }
});
Container c = getContentPane();
c.setLayout(null);
setSize(600,400);
show();
}
public void run() // ridefinisco run di Runnable
{
try{
ar1.append("ciao" + "\n");
t.sleep(2000);
ar1.append("abcd1000");
}
catch(InterruptedException i)
{
i.getMessage();
}
}
//-------------------GESTIONE EVENTO BOTTONE -----------------------------
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if (o == bt1){
avviaThread();
}
}
public void avviaThread()
{
t = new Thread(this);
t.start(); // questo richiama il metodo run()
}
public static void main (String arg []){
Ritardo f = new Ritardo();
}
}// fine frame

Rispondi quotando