gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 19:57:39 24 4
gpt4 key购买 nike

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

每秒刷新并获取时间的代码确实工作得很好,但我只能让它与 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 代码中(确定新日期/时间后)。

此外,Timer ActionListener 中不需要 if 语句。监听器仅添加到一个计时器,因此只有当计时器触发时才会执行代码。

关于java - JFrame/JPanel 不更新重绘或重新验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49038152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com