Jlabel时钟java摆动

Jlabel时钟java摆动,java,swing,Java,Swing,我正在制作一个程序,显示选定国家的时钟和zime区域。 单击按钮时,时间将显示在jLabel中。 问题是,当我单击一次时,时间会显示在标签中,但当我单击第二次时,时间会被重写为现有的jlabel日期(文本) 如何在不重写的情况下显示日期。 如果单击更多次,时间将不可读 这是我的密码: public class Window extends JFrame { JLabel title, text,date1,label1; JTextField country; JBut

我正在制作一个程序,显示选定国家的时钟和zime区域。 单击按钮时,时间将显示在jLabel中。 问题是,当我单击一次时,时间会显示在标签中,但当我单击第二次时,时间会被重写为现有的jlabel日期(文本)

如何在不重写的情况下显示日期。 如果单击更多次,时间将不可读

这是我的密码:

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);

        }  
    }           
}         
有人能帮我吗

如果单击更多次,时间将不可读

问题是您一直在创建新的jlabel并将它们添加到框架中,以便每个标签的文本都覆盖在另一个标签上。不要每次单击按钮时都创建新的JLabel

而是在创建框架时创建标签并将其添加到框架中

然后你只需使用:

label.setText(...);
更改标签的文本

此外,您不应该使用空布局。Swing设计用于布局管理器。有关更多信息和工作示例,请阅读上Swing教程的部分

如果单击更多次,时间将不可读

问题是您一直在创建新的jlabel并将它们添加到框架中,以便每个标签的文本都覆盖在另一个标签上。不要每次单击按钮时都创建新的JLabel

而是在创建框架时创建标签并将其添加到框架中

然后你只需使用:

label.setText(...);
更改标签的文本


此外,您不应该使用空布局。Swing设计用于布局管理器。阅读Swing教程中的部分,了解更多信息和工作示例。

谢谢,它正在工作。我不知道我怎么会犯这样的错误。谢谢,它正在工作。我不知道我怎么会犯这样的错误。