Jlabel显示新旧数字 [英] Jlabel showing both old and new numbers

查看:226
本文介绍了Jlabel显示新旧数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Jlabel不仅显示(tNow - tStart)%10000,而且显示过去的所有内容,我该如何修复?



import javax.swing。;
import java.awt。
;



public class Main {

  public static void main(String [] args){
JFrame frame = new JFrame(FrameDemo);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
//frame.setSize(500,500);
frame.setUndecorated(true);
frame.getContentPane()。setBackground(Color.GREEN);
final long tStart = System.currentTimeMillis();
long tNow = 0;
System.out.println(tStart);
while(1!= 0){
add_time(tNow,frame);
}
}

public static void add_time(long tStart,JFrame frame){
long tNow = System.currentTimeMillis();

if((tNow - tStart)%10000> = 5000){
frame.setVisible(true);
//System.out.println((tNow - tStart)%10000);
JLabel label = new JLabel(Test,JLabel.CENTER);
label.setText(String.valueOf((tNow - tStart)%10000));
label.setFont(new Font(Serif,Font.BOLD,240));
label.setSize(500,500);
frame.add(label);
//label.setText();
} else frame.setVisible(false);

}

}

解决方案

  frame.add(label); 

不要继续向框架添加组件。摆脱这种说法。



相反,你需要更新框架上的现有标签:



所以你需要进行一些更改:


  1. 创建JLabel并将其添加到main()方法中的框架。 / p>


  2. 然后需要更改 addTime(...)方法以将标签传递给方法帧的内容。然后你可以使用标签的setText()方法更新标签。


实际上这个设计很差码。您不应该为所有代码使用静态方法。下面是一个示例,说明如何更好地构建代码以及如何使用 Timer 更新标签上的文本:

  import java.awt。*; 
import java.awt.event。*;
import java.util。*;
import javax.swing。*;
import javax.swing.Timer;

公共类TimerTime扩展JPanel实现ActionListener
{
private JLabel timeLabel;
private int count = 0;

public TimerTime()
{
timeLabel = new JLabel(new Date()。toString());
add(timeLabel);

定时器定时器=新定时器(1000,这个);
timer.setInitialDelay(1);
timer.start();
}

@Override
public void actionPerformed(ActionEvent e)
{
//System.out.println(e.getSource());
timeLabel.setText(new Date()。toString());
// timeLabel.setText(String.valueOf(System.currentTimeMillis()));
count ++;

if(count == 10)
{
Timer timer =(Timer)e.getSource();
timer.stop();
}
}

private static void createAndShowUI()
{
JFrame frame = new JFrame(TimerTime);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TimerTime());
frame.setLocationByPlatform(true);
frame.pack();
frame.setVisible(true);
}

public static void main(String [] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}

我建议您更新代码以使用类似的东西而不是你当前的代码。


My Jlabel shows not only present (tNow - tStart) % 10000, but all the past ones, how do I fix that?

import javax.swing.; import java.awt.;

public class Main {

public static void main(String[] args) {
    JFrame frame = new JFrame("FrameDemo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    //frame.setSize(500, 500);
    frame.setUndecorated(true);
    frame.getContentPane().setBackground(Color.GREEN);
    final long tStart = System.currentTimeMillis();
    long tNow = 0;
    System.out.println(tStart);
    while (1 != 0) {
        add_time(tNow, frame);
    }
}

public static void add_time(long tStart, JFrame frame) {
    long tNow = System.currentTimeMillis();

    if ((tNow - tStart) % 10000 >= 5000) {
        frame.setVisible(true);
        //System.out.println((tNow - tStart) % 10000);
        JLabel label = new JLabel("Test", JLabel.CENTER);
        label.setText(String.valueOf((tNow - tStart) % 10000));
        label.setFont(new Font("Serif", Font.BOLD, 240));
        label.setSize(500, 500);
        frame.add(label);
        //label.setText("");
    } else frame.setVisible(false);

}

}

解决方案

frame.add(label);

Don't keep adding components to the frame. Get rid of that statement.

Instead you need to update the existing label on the frame:

So you need to make a couple of changes:

  1. Create the JLabel and add it to the frame in the main() method.

  2. Then the addTime(...) method needs to be changed to pass the label to the method intead of the frame. Then you can just use the setText() method of the label to update the label.

Actually this is poorly designed code. You should not be using static methods for all your code. Here is an example that shows how to better structure your code and how to use a Timer to update the text on a label:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class TimerTime extends JPanel implements ActionListener
{
    private JLabel timeLabel;
    private int count = 0;

    public TimerTime()
    {
        timeLabel = new JLabel( new Date().toString() );
        add( timeLabel );

        Timer timer = new Timer(1000, this);
        timer.setInitialDelay(1);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        //System.out.println(e.getSource());
        timeLabel.setText( new Date().toString() );
//      timeLabel.setText( String.valueOf(System.currentTimeMillis() ) );
        count++;

        if (count == 10)
        {
            Timer timer = (Timer)e.getSource();
            timer.stop();
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("TimerTime");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TimerTime() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

I suggest you update to code to use something like that instead of your current code.

这篇关于Jlabel显示新旧数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆