2014-04-06 20 views
0

我知道類似這樣的問題,有人問及在javascript回答,但我在Java中的分配是涉及到顯示單/多位置的時間(從列表選擇)。多個時鐘在一次 - 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; 
    } 
} 

回答

0

看一看的actionPerformed(),也有存在一些問題。您需要設置時鐘JLabels,使您可以在for循環中添加更多內容,並且每次Timer觸發時仍然引用已製作的JLabels。目前,每次添加新時鐘時,您只是覆蓋單個「時間」JLabel的值,這實際上無法在以前的標籤上調用setText()(或其他任何內容)。

而且,一旦這種變化是完整的,一定要記得在遍歷所有的時鐘和更新其各自的時間。