JFrame/JPanel 不更新重绘或重新验证

JFrame/JPanel does not update repaint or revalidate

我试图每秒更新一次我的面板,但我没有让它工作。我几乎尝试了所有方法,甚至使用重新验证和重新绘制,但它仍然不起作用。我做错了什么?

用于每秒刷新和获取时间的代码工作得很好,但我只是无法让它与 GUI 一起工作。只出现过一次,之后就不再更新了This is how it looks like

这是我的 JFrame 代码..

public class MainFrame extends JFrame{
WeatherWidget weatherWidget = new WeatherWidget();
DateTimeWidget dateTimeWidget = new DateTimeWidget();

Timer weatherRefreshTimer = new Timer(1000*60, new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == weatherRefreshTimer){
           // weatherWidget.retreatWeatherInformation();
            weatherWidget.updateWeatherUI();
            repaint();
            System.out.println("Updated weather");
        }
    }
});


Timer dateTimeRrefreshTimer = new Timer(1000, new ActionListener() {

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == dateTimeRrefreshTimer){

            dateTimeWidget.retreatDateTimeInformation();
            dateTimeWidget.updateDateTimeUI();

            repaint();
            System.out.println("Updated Time and Date");
        }
    }
});

public MainFrame(){
    this.setSize(540, 950);
    this.getContentPane().setBackground(new Color(0,0,0));
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.getContentPane().setLayout(null);
    this.setResizable(false);

    addWeatherComponents();
    addDateTimeComponents();

    this.setVisible(true);

    weatherRefreshTimer.start();
    dateTimeRrefreshTimer.start();
}

public void addWeatherComponents(){
    this.add(weatherWidget.getWeerIconLabel());
    this.add(weatherWidget.getTemperatureLabel());
    this.add(weatherWidget.getPlaatsLabel());
}

public void addDateTimeComponents(){
    this.add(dateTimeWidget.getTimeLabel());
}

public void repaint(){
    this.getContentPane().revalidate();
    this.getContentPane().repaint();
}


}

这是我的 JLabels

public class DateTimeWidget {
private String time;
private String date;

private int hour;
private int minutes;
private int seconds;
private int day;
private int month;
private int year;

private JLabel timeLabel;
private JLabel dateLabel;

public DateTimeWidget(){
    retreatDateTimeInformation();
    updateDateTimeUI();
}

public void updateDateTimeUI(){
    timeLabel = new JLabel();
    timeLabel.setText(time);
    timeLabel.setBounds(350, 10, 200, 30);
    timeLabel.setForeground(new Color(255,255,255));
    timeLabel.setHorizontalAlignment(SwingConstants.LEFT);
    timeLabel.setBackground(new Color(Transparency.TRANSLUCENT));
    timeLabel.setFont(new Font("Tahoma", Font.BOLD, 30));
    timeLabel.setOpaque(false);

}

public void retreatDateTimeInformation(){
    LocalDateTime now = LocalDateTime.now();
    hour = now.getHour();
    minutes = now.getMinute();
    seconds = now.getSecond();
    day = now.getDayOfMonth();
    month = now.getMonthValue();
    year = now.getYear();

    time =    Integer.toString(hour)+":"+Integer.toString(minutes)+":"+Integer.toString(seconds);

    System.out.println(time);

}

public JLabel getTimeLabel() {
    return timeLabel;
}
}
public void updateDateTimeUI(){
    timeLabel = new JLabel();
    timeLabel.setText(time);
    timeLabel.setBounds(350, 10, 200, 30);
    timeLabel.setForeground(new Color(255,255,255));
    timeLabel.setHorizontalAlignment(SwingConstants.LEFT);
    timeLabel.setBackground(new Color(Transparency.TRANSLUCENT));
    timeLabel.setFont(new Font("Tahoma", Font.BOLD, 30));
    timeLabel.setOpaque(false);

}

不要继续创建新组件!!!

标签应创建一次并添加到框架中一次。

然后当定时器触发更改数据时,您只需调用:

timeLabel.setText(....);

在ActionListener代码中(在你确定新的date/time之后)。

此外,Timer ActionListener 中不需要 if 语句。侦听器只添加到一个定时器,因此代码只会在定时器触发时执行。