|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Messaggi: n/a
|
[Java]Problema conto alla rovescia
public class AutoKill extends JFrame {
private Container sfondo; private JTextArea area; public AutoKill(){ addWindowListener(new Ascoltatore()); setSize(400,400); setResizable(false); setTitle("AutoKill"); sfondo = getContentPane(); area = new JTextArea(""); area.setBounds(0,0,400,400); area.setEditable(false); sfondo.add(area); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); setVisible(true); } //AutoKill protected class Ascoltatore extends WindowAdapter { public void windowClosing(WindowEvent ev) { try { for(int i=5; i>0; i--) { Thread.sleep(1000); System.out.println(i); area.setText(""+i); } // for } catch (Exception e) {} } // windowClosing } // Ascoltatore public static void main(String[] args) { AutoKill a = new AutoKill(); } // main } // AutoKill Perchè System.out.println(i); compare ogni secondo sul prompt e area.setText(""+i); compare solo alla fine???? Come devo fare per sistemarlo? |
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2010
Città: Frosinone
Messaggi: 416
|
intanto dovresti usare l'edt per fare modifiche sugli oggetti swing, e poi dovresti usare un thread apposito per un'operazione di questo tipo
http://java.sun.com/products/jfc/tsc.../threads2.html |
|
|
|
|
|
#3 |
|
Messaggi: n/a
|
Mai usata SwingWorker.. è pure astratta.. bel casino. qualche consiglio?? aiutino??
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Apr 2010
Città: Frosinone
Messaggi: 416
|
Codice:
for(int i=5; i>0; i--) {
Thread.sleep(1000);
System.out.println(i);
area.setText(""+i);
JFrame f = (JFrame)ev.getComponent();
f.update(f.getGraphics());
} // for
|
|
|
|
|
|
#5 |
|
Messaggi: n/a
|
PERO' FUNIONAAAA!!
Grazie milleeeee!!! Ma praticamente crea un'altro JFrame con gli elementi dell'altro e lo aggiorna ogni volta? Giusto? |
|
|
|
#6 |
|
Senior Member
Iscritto dal: Apr 2010
Città: Frosinone
Messaggi: 416
|
no getComponent ritorna il componente che ha generato l'evento, quindi è proprio il riferimento al tuo JFrame, non crea niente
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Nov 2004
Messaggi: 691
|
Eccoti il programmino
Codice:
import java.awt.event.*;
import javax.swing.*;
import java.util.List;
public class AutoKill extends JFrame {
private JTextArea area;
private class Countdown extends SwingWorker<Void, Integer> {
@Override protected Void doInBackground() {
for(int i=5; i>0; i--) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {}
publish(i);
System.out.println(i);
}
return null;
}
@Override protected void process(List<Integer> listaDiInteri) {
Integer i = listaDiInteri.get(listaDiInteri.size() - 1);
area.setText(""+i);
}
}
public AutoKill(){
this.addWindowListener(new Ascoltatore());
this.setSize(400,400);
this.setResizable(false);
this.setTitle("AutoKill");
this.area = new JTextArea("");
this.area.setBounds(0,0,400,400);
this.area.setEditable(false);
this.getContentPane().add(area);
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setVisible(true);
}
protected class Ascoltatore extends WindowAdapter {
@Override public void windowClosing(WindowEvent ev) {
Countdown c = new Countdown();
c.execute();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new AutoKill();
}
});
}
}
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 16:11.



















