Affichage d'un résultat dans une interface
Bonsoir,
Ci-dessous 3 class : "Initialise" (main), "calcul" et "interface"
main affiche l'interface, puis lance un calcul
Pouvez-vous me dire comment afficher le résultat de la variable temp dans le champ text
Toutes vos suggestions sur ce premier test seront lues avec attention.
Alain
Code:
1 2 3 4 5 6 7 8 9 10
| public class Initialise {
public static void main(String[] args) {
new Interface();
Thread thread = new calcul();
thread.run();
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Interface extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
Interface() {
super("Interface");
setSize(new Dimension(400, 100));
JPanel pan = new JPanel();
FlowLayout bl = new FlowLayout(FlowLayout.CENTER);
pan.setLayout(bl);
JLabel lab = new JLabel("calcul");
JTextField text = new JTextField();
text.setPreferredSize(new Dimension(100, 20));
pan.add(lab);
pan.add(text);
setContentPane(pan);
setVisible(true);
// opération par défaut à la fermeture
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import java.util.Random;
import javax.swing.JLabel;
public class calcul extends Thread {
public void run() {
float temp = 10;
while (true) {
try {
int n = 2000;
System.out.println("ma tache \ntemporisation de " + n / 1000
+ " secondes");
Thread.sleep(n); // Attend n secondes
System.out.println("fin de la tache");
System.out.println("un résultat = " + tache(temp) + "\r");
JLabel lab = new JLabel("Bonjour le monde!!"); // créé un label
Thread.sleep(n); // Attend n secondes
} catch (InterruptedException exception) {
}
}
}
private static float tache(float temp) {
int signe;
// System.out.println("temp = " + temp);
// + ou - aléatoirement
signe = 1;
if (Math.random() < 0.5) {
signe = -1;
}
// variation entre 0.1 et 0.99
Random rand = new Random();
float variation = rand.nextFloat();
// Arrondir la valeur a 10^-2
temp = temp + (variation * signe);
temp *= 100.0;
temp = (float) Math.floor(temp + 0.5);
temp /= 100.0;
return temp;
}
} |