Java - Actualizar JLabel desde Clase

 
Vista:
sin imagen de perfil

Actualizar JLabel desde Clase

Publicado por Alex (1 intervención) el 25/08/2015 08:29:18
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
---------- // MAIN CLASS
 
        package gui;
 
        public class Gui {
 
            public static void main(String[] args) {
                Myframe a = new Myframe();
                a.My_frame();
                prints b = new prints();
                b.Counter();
            }
 
        }
 
    -------- // JFRAME
 
          package gui;
 
            import javax.swing.JFrame;
            import javax.swing.JLabel;
            import javax.swing.SwingUtilities;
 
            public class Myframe extends JFrame {
 
                 public JLabel Label;
 
                Myframe(){}
 
 
               public void My_frame(){
 
                 prints m = new prints();
                 JFrame screen = new JFrame("Test");
                 screen.setSize(1024, 768);
                 screen.setExtendedState(JFrame.MAXIMIZED_BOTH);
                 screen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 Label = new JLabel("Orginal Text", JLabel.LEFT);
                 screen.add(Label);
                 screen.setVisible(true);
                 screen.add(Label);
 
                }
 
                public void c_text(String text) {
 
 
                        Label.setText(text);
                        System.out.print(text);
 
 
                    }
 
            }
 
    ------ // LOOP
 
 
        package gui;
 
        import java.awt.GridBagLayout;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
 
        public class prints extends JFrame{
 
 
 
         prints() { }
 
            public void Counter(){
 
            int x = 20;
            for(int i =0; i < 21; i++){
 
                 System.out.println(i);
 
                 Myframe f = new Myframe();
                 String n = Integer.toString(i);
                 f.c_text(n);
 
            }
 
            }
        }


Prácticamente lo que quiero lograr es que el label se actualice con el resultado, una vez que esta iniciado el jframe no puedo acceder al elemento, si creo un nuevo jframe ( My_frame() ) y le paso el valor desde el loop a Label me crea ventanas nuevas y el valor si se actualiza pero es lo que no quiero crear miles de ventanas... no se que estoy haciendo mal.

Muchas gracias de antemano.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 349
Bronce
Ha mantenido su posición en Java (en relación al último mes)
Gráfica de Java

Actualizar JLabel desde Clase

Publicado por Andrés Mella Romero (340 intervenciones) el 27/08/2015 18:56:23
1
2
3
4
5
6
7
8
9
package javaapplication15;
 
public class JavaApplication15 {
 
    public static void main(String[] args) throws InterruptedException {
        MyFrame a = new MyFrame();
    }
 
}

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
package javaapplication15;
 
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
 
public class MyFrame extends JFrame {
 
    public JLabel Label;
 
    public MyFrame() throws InterruptedException {
        super("Test");
        setSize(1024, 768);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Label = new JLabel("Orginal Text", JLabel.LEFT);
        add(Label);
        setVisible(true);
        add(Label);
        counter();
    }
 
    private void updateProgress(final int i) {
 
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Label.setText(Integer.toString(i));
            }
        });
 
    }
 
    private void counter() throws InterruptedException {
 
        for (int i = 0; i < 21; i++) {
            updateProgress(i);
            Thread.sleep(1000);
        }
    }
 
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar