我试图每秒更新一次我的面板,但我不能让它工作。即使使用重新验证和重新绘制,我几乎尝试了所有内容,但它仍然无法正常工作。我做错了什么?
刷新每一秒并获得时间的代码确实可以正常工作,但我只能让它与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;
}
}
答案 0 :(得分: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触发时才会执行代码。