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

问题描述 投票:0回答:1

我试图每秒更新我的面板,但我没有让它工作。即使使用重新验证和重绘,我几乎尝试了所有东西,但它仍然无法正常工作。我究竟做错了什么?

刷新每秒和获取时间的代码确实完美,但我不能让它与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();
}


}

这就是我拥有JLabel的地方

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;
}
}
java swing repaint
1个回答
3
投票
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);

}

不要继续创建新组件!

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

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

timeLabel.setText(....);

在ActionListener代码中(确定新的日期/时间后)。

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

© www.soinside.com 2019 - 2024. All rights reserved.