多个时钟一次 - Java

时间:2014-04-06 19:06:20

标签: java

我知道类似的问题在javascript中被问及并回答但是我在Java中有一项任务,涉及显示单个/多个位置的时间(从列表中选择)。

我有一定数量的内置,如下面的代码所示。但是,当我选择多个位置时,我陷入困境,唯一的“滴答”时钟是所选的最后一个时钟,其他时钟只是在它们被创建时冻结。

有人可以查看下面的代码,看看他们是否可以给我一些指导?

由于

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;

public class AssignmentV1 implements ActionListener/* ,ListSelectionListener */
{
    private String[] cities;
    private JButton addBtn;
    private JButton minusBtn;
    private JList cityList;
    private JPanel right;
    private JPanel displayArea;
    private JPanel[] clockDisplay;
    private long currentTime;
    private long cityTime;
    private int hourDiff;
    private String cityName;
    private String timeInFormat;
    private JLabel time;
    private JPanel topRight;
    private ArrayList<String> cityNamesList;
    private ArrayList<Long> cityTimes;

    public AssignmentV1() {
        JFrame appWin = new JFrame("World Time");
        appWin.setSize(400, 400);
        appWin.setLocationRelativeTo(null);
        appWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Font preferredFont = new Font("OCR A EXTENDED", Font.BOLD, 50);

        Container contentPane = appWin.getContentPane();

        displayArea = new JPanel();
        displayArea.setLayout(new GridLayout(1, 2));

        JPanel left = new JPanel();
        left.setLayout(new GridLayout(2, 1));

        JPanel topLeft = new JPanel();
        topLeft.setLayout(new FlowLayout(FlowLayout.CENTER));
        // adding of +/- buttons to display
        addBtn = new JButton();
        addBtn.setText("+");
        addBtn.addActionListener(this);
        topLeft.add(addBtn);
        minusBtn = new JButton();
        minusBtn.setText("-");
        minusBtn.addActionListener(this);
        topLeft.add(minusBtn);
        left.add(topLeft);

        // adding of list of cities to a scrollpane to add to display
        JPanel bottomLeft = new JPanel();

        setUpCities();
        cityList = new JList(cities);
        cityList.setSelectedIndex(0);
        cityList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        JScrollPane scrollableList = new JScrollPane(cityList);
        bottomLeft.add(scrollableList);
        left.add(bottomLeft);

        right = new JPanel();
        topRight = new JPanel();
        clockDisplay = new JPanel[1];
        topRight.setLayout(new GridLayout(clockDisplay.length, 1));
        for (int i = 0; i < clockDisplay.length; i++) {
            clockDisplay[i] = new JPanel();
            cityName = cities[0].substring(0, cities[0].indexOf(","));
            hourDiff = Integer.parseInt(cities[0].substring(cities[0].indexOf(",") + 1));
            currentTime = System.currentTimeMillis();
            cityTime = System.currentTimeMillis() + (1000 * 60 * 60 * hourDiff);
            clockDisplay[i].setBorder(BorderFactory.createTitledBorder(cityName));
            time = new JLabel();
            time.setOpaque(true);
            timeInFormat = textAsTime(cityTime);
            time.setText(timeInFormat);
            clockDisplay[i].add(time);
            topRight.add(clockDisplay[i]);
        }
        right.add(topRight);

        displayArea.add(left);
        displayArea.add(right);
        contentPane.add(displayArea);
        Timer t = new Timer(1000, this);
        t.start();
        appWin.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
        cityTime = tick(cityTime);
        timeInFormat = textAsTime(cityTime);
        time.setText(timeInFormat);

        cityNamesList = new ArrayList<String>();
        cityTimes = new ArrayList<Long>();

        Object[] obj = cityList.getSelectedValues();
        Object source = e.getSource();
        if (source == addBtn) {
            topRight.removeAll();
            right.setLayout(new GridLayout(obj.length, 1));
            clockDisplay = new JPanel[obj.length];
            topRight.setLayout(new GridLayout(obj.length, 1));
            ArrayList<String> cityNamesList = new ArrayList<String>();
            ArrayList<Long> cityTimes = new ArrayList<Long>();
            for (int i = 0; i < obj.length; i++) {
                String temp = (String) obj[i];
                clockDisplay[i] = new JPanel();
                cityName = temp.substring(0, temp.indexOf(","));
                cityNamesList.add(cityName);
                hourDiff = Integer.parseInt(temp.substring(temp.indexOf(",") + 1));
                currentTime = System.currentTimeMillis();
                cityTime = System.currentTimeMillis() + (1000 * 60 * 60 * hourDiff);
                cityTimes.add(cityTime);
                tick(cityTime);
                clockDisplay[i].setBorder(BorderFactory.createTitledBorder(cityName));
                time = new JLabel();
                time.setOpaque(true);
                timeInFormat = textAsTime(cityTime);
                time.setText(timeInFormat);
                clockDisplay[i].add(time);
                topRight.add(clockDisplay[i]);
            }
            right.add(topRight);
        }
    }

    public void setUpCities() {
        cities = new String[] { "Accra,0", "Addis Abada,+3", "Adelaide,+11", "Algiers,-1",
                "Almaty,+6", "Amman,+3", "Amsterdam,+1", "Anadyr,+12", "Anchorage,-8", "Ankara,+2",
                "London,0", "Paris,+1" };
    }

    public String textAsTime(long currentTime) {
        long second = (currentTime / 1000) % 60;
        long minute = (currentTime / (1000 * 60)) % 60;
        long hour = (currentTime / (1000 * 60 * 60)) % 24;
        return String.format("%02d:%02d:%02d", hour, minute, second);
    }

    public static void main(String[] args) {
        new AssignmentV1();
    }

    public long tick(long cityTime) {
        cityTime = cityTime + 1000;
        return cityTime;
    }
}

1 个答案:

答案 0 :(得分:0)

看看actionPerformed(),那里有一些问题。您需要以允许您在for循环中添加更多内容的方式设置时钟JLabel,并且每次定时器触发时仍然引用已经生成的JLabel。目前,每次添加新时钟时,您只是覆盖单个“时间”JLabel的值,这实际上无法在先前的标签上调用setText()(或其他任何内容)。

此外,一旦完成此更改,请确保记住迭代所有时钟并更新它们各自的时间。