1
Why can’t I update my labelHora
in the Frame?
Using a print after the line labelHora.setText(x);
I realized that the content of lip changes, but on the screen continues the pattern, which in the case is "zzz".
package ultilidade;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class FrameInicio extends JFrame {
private JPanel contentPane;
public static JLabel labelHora;
public static FrameInicio frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new FrameInicio();
DataEHora hora = new DataEHora();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FrameInicio() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
labelHora = new JLabel("zzz");
contentPane.add(labelHora, BorderLayout.NORTH);
}
}
This class builds Jframe.
package ultilidade;
import java.text.SimpleDateFormat;
import java.util.*;
public class DataEHora extends FrameInicio{
Date hora;
SimpleDateFormat sdf;
public static int i = 0;
Timer timer;
public DataEHora(){
timer = new Timer();
timer.schedule(new RemindTask(), 0, 5*1000);
}
public String mostraHora(){
hora = new Date();
sdf = new SimpleDateFormat("HH:mm");
return sdf.format(hora);
}
class RemindTask extends TimerTask{
@Override
public void run() {
//frame.metodo();
//DataEHora hora = new DataEHora();
String x = mostraHora();
labelHora.setText(x);
frame.repaint();
}
}
/*public static void main(String[] args) {
DataEHora d = new DataEHora();
}*/
/*public static void main(String[] args) {
DataEHora deh = new DataEHora();
deh.atualizaHora();
} */
}
That would update.
Are you using
null layout
?– Jéf Bueno
In this case is the border. My doubt arose in a project that I am developing, so the code does not get too extensive I created these two classes.
– Bruno Brito
when you run this run() and when you run the code that creates Jlabel?
– PauloHDSousa