简体   繁体   English

JLabel Clock Java Swing

[英]Jlabel Clock java swing

I am making a program that displays the clock and zime zones for a country selected. 我正在制作一个显示所选国家/地区的时钟和zime区域的程序。 The time is displayed into a jLabel when the button is clicked. 单击按钮时,时间将显示在jLabel中。 The problem is that when I click once the time is displayed in the label ,but when I click second the time is rewritten to the existent jlabel date (text). 问题是,当我单击一次标签中显示了时间时,但是当我单击第二次时,时间被重写为现有的jlabel日期(文本)。

How can I display the date without to be rewritten. 如何显示日期而无需重写。 If click more times, the time won't be readable. 如果单击更多次,该时间将不可读。

Here Is my code: 这是我的代码:

public class Window extends JFrame {

    JLabel title, text,date1,label1;
    JTextField country;
    JButton search;

    private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a";

    public Window() {
        super("Genesys");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600,470);
        setVisible(true);
        setLayout(null);

        //------Labels-------
        title = new JLabel ("Genesys Time");
        add (title);

        text = new JLabel("Continent/Country:");
        add(text);

        //--------TextFileds------
        country = new JTextField();
        add (country);

        //----------Buttons-------
        search = new JButton("Cauta");
        add (search);

        title.setBounds(10,1,100,100);
        text.setBounds(10,100,150,20);
        country.setBounds(150,100,200,20);
        search.setBounds(10,150,100,20);

        search.addActionListener(new csearch()); 
    }

class csearch implements ActionListener {

    public void actionPerformed(ActionEvent event) {
        //dispose();
        //WindowTime wt = new WindowTime(null);

        date1 = new JLabel();
        date1.setVisible(false);

        SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
        Date date = new Date();
        //  String dateInString = "02-11-2016 12:06:55 AM";
        //    Date date = formatter.parse(dateInString);
        TimeZone tz = TimeZone.getDefault();

        // From TimeZone Asia/Singapore
        System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName());
        System.out.println("TimeZone : " + tz);
        System.out.println("Date (Ro - UTC+02:00) : " + formatter.format(date));

        // To TimeZone America/New_York
        SimpleDateFormat sdfAmerica = new SimpleDateFormat(DATE_FORMAT);
        SimpleDateFormat sdfParis = new SimpleDateFormat(DATE_FORMAT);
        DateTime dt = new DateTime(date);
        DateTimeZone dtZone = DateTimeZone.forID(country.getText());
        DateTime dtus = dt.withZone(dtZone);
        TimeZone tzInAmerica = dtZone.toTimeZone();
        Date dateInAmerica = dtus.toLocalDateTime().toDate(); //Convert to LocalDateTime first

        sdfAmerica.setTimeZone(tzInAmerica);

        date1.setText(String.valueOf(dateInAmerica));                
        add(date1);

        date1.setBounds(10,200,1000,40);

        }  
    }           
}         

Could anyone help me? 有人可以帮我吗?

If click more times, the time won't be readable 如果单击更多次,将无法读取时间

The problem is that you keep creating new JLabels and add them to the frame so the text of each label paints over top of one another. 问题在于,您将继续创建新的JLabel并将其添加到框架中,以便每个标签的文本彼此重叠。 Don't create a new JLabel every time you click the button. 不要在每次单击按钮时创建一个新的JLabel。

Instead create the label and add it to the frame when you create the frame. 而是创建标签,然后在创建框架时将其添加到框架。

Then you just use: 然后,您只需使用:

label.setText(...);

to change the text of the label. 更改标签的文本。

Also, you should NOT be using a null layout. 另外,您不应使用null布局。 Swing was designed to be used with layout managers. Swing旨在与布局管理器一起使用。 Read the section from the Swing tutorial on Layout Managers for more information and working examples. 阅读有关布局管理器的Swing教程中的部分,以获取更多信息和工作示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM