【问题标题】:JFrame/JPanel does not update repaint or revalidateJFrame/JPanel 不更新重绘或重新验证
【发布时间】:2018-02-28 20:40:00
【问题描述】:

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

每秒刷新和获取时间的代码确实可以完美运行,但我无法让它与 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;
}
}

【问题讨论】:

    标签: java swing repaint


    【解决方案1】:
    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 触发时执行。

    【讨论】:

    • 不要忘记 repaint 方法递归调用自身...以及 null-layout 的使用
    猜你喜欢
    • 2020-03-31
    • 2013-05-07
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多