|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Nov 2004
Città: Napoli
Messaggi: 999
|
[Java] rallentare il processo...
salve, volevo una dritta....
ho bisogno di far lampeggiare una label... nel senso che ho una scritta , la quale vorrei far lampeggiare.... nella pratica ho sta label, ed avevo pensato di alternare la scritta con una stringa vuota , intervallata da un Thread.sleep(500) per esempio... ma non ho l'effetto voluto...
__________________
Intel Pentium IV 3,0 GHz, Asus P5SD2-X , 1.0 Gb ddr2, Radeon X550 , Maxtor 160Gb sata, Hitachi 100 gb pata,Piooner Dvr-109 ,Microsoft Windows XP Professional Service Pack 2 |
![]() |
![]() |
![]() |
#2 |
Member
Iscritto dal: Jan 2007
Messaggi: 70
|
Anche io avrei questa domanda
Esiste un metodo della classe object (e quindi ereditato da tutte le classi) che si chiama wait.. tuttavia non ho l'effeto desiderato... ![]() Se qualcuno potesse far luce su questa faccenda gliene sarei molto grato Ciao ![]() |
![]() |
![]() |
![]() |
#3 |
Member
Iscritto dal: Jan 2007
Messaggi: 70
|
Per esempio se io volessi che un oggetto qualsiasi prima di svolgere un metodo deve aspettare n secondi come devo fare?
![]() |
![]() |
![]() |
![]() |
#4 |
Senior Member
Iscritto dal: Dec 2002
Messaggi: 3359
|
Certo c'è la classe timer per fare questo genere di cose.
http://java.sun.com/docs/books/tutor...isc/timer.html |
![]() |
![]() |
![]() |
#5 |
Member
Iscritto dal: Jan 2007
Messaggi: 70
|
Grande proprio quello che cercavo!
Thanx ![]() |
![]() |
![]() |
![]() |
#6 |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
Una raccomandazione:
NON usare MAI il comando Thrad.sleep o wait o qualsiasi altra cosa che possa bloccare un thread fintanto che ci si trova all'interno del dispatcher thread dell'AWT. Così facendo infatti si blocca il thread che gestisce le code degli eventi e il ridisegno della GUI dell'AWT e si avranno certamente effetti indesiderati. Per ottenere il risultato corretto bisogna fare girare queste operazioni in un altro thread in modo che il thread dell'AWT possa fare correttamente il suo lavoro.
__________________
![]() |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Nov 2004
Città: Napoli
Messaggi: 999
|
e io come dovrei fare ???
devo fare una cosa del tipo class beep extends Thread contenuto del run ... MIACLASSE.label.setText("ciao"); sleep(500); MIACLASSE.label.setText(""); sleep(500); MIACLASSE.label.setText("ciao"); sleep(500); posso creare una piccola classe che estende Thread magari... e dalla classe della grafica lancio sto run().... potrebbe fuzionare ??
__________________
Intel Pentium IV 3,0 GHz, Asus P5SD2-X , 1.0 Gb ddr2, Radeon X550 , Maxtor 160Gb sata, Hitachi 100 gb pata,Piooner Dvr-109 ,Microsoft Windows XP Professional Service Pack 2 |
![]() |
![]() |
![]() |
#8 |
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
versione single-threaded:
Codice:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Prova implements ActionListener{ JLabel label; public Prova() { JFrame frame = new JFrame(); JPanel panel = new JPanel(); label = new JLabel("ciao"); panel.add(label); frame.getContentPane().add(panel); label.setText("ciao"); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); Timer timer = new Timer(500, this); timer.setInitialDelay(0); timer.start(); } public static void main(String[] args) { Prova p = new Prova(); } public void actionPerformed(ActionEvent arg0) { if(label.getText().equals("")) { label.setText("ciao"); } else { label.setText(""); } label.repaint(); } } Codice:
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Prova implements ActionListener{ JLabel label; ProvaThread provaThread; public Prova() { JFrame frame = new JFrame(); JPanel panel = new JPanel(); label = new JLabel("ciao"); panel.add(label); frame.getContentPane().add(panel); label.setText("ciao"); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); Timer timer = new Timer(500, this); timer.setInitialDelay(0); provaThread = new ProvaThread(this); timer.start(); } public static void main(String[] args) { Prova p = new Prova(); } public void actionPerformed(ActionEvent arg0) { provaThread.run(); } } class ProvaThread implements Runnable { JLabel label; public ProvaThread(Prova prova) { label = prova.label; } public void run() { if(label.getText().equals("")) { label.setText("ciao"); } else { label.setText(""); } label.repaint(); } }
__________________
![]() |
![]() |
![]() |
![]() |
#9 |
Senior Member
Iscritto dal: Nov 2004
Città: Napoli
Messaggi: 999
|
ti ringrazio, ho risolto facendo una nuova classe che estende thread...
sbagliavo xkè come dicevi tu, lanciavo lo sleep dall'interfaccia grafica... quindi un nuovo porcesso mi gestisce la label ed è tutto ok. ![]() ![]()
__________________
Intel Pentium IV 3,0 GHz, Asus P5SD2-X , 1.0 Gb ddr2, Radeon X550 , Maxtor 160Gb sata, Hitachi 100 gb pata,Piooner Dvr-109 ,Microsoft Windows XP Professional Service Pack 2 |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:30.