2016-11-12 26 views
0

我试图使JComboBox在那里我想获取所有时区,但没有显示任何时区。有谁能帮我解决这个问题吗?将时区显示到JComboBox中

这里是我的代码:

public class Window extends JFrame { 
    private static final String DATE_FORMAT = "dd-M-yyyy hh:mm:ss a"; 
    public Window() { 
     super("Genesys"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setExtendedState(JFrame.MAXIMIZED_BOTH); 
     setSize(600, 470); 
     setVisible(true); 
     setLayout(null); 
     gradeCombo = new JComboBox <Object>(); 
     add(gradeCombo); 
     gradeCombo.setBounds(500, 100, 200, 20); 
     search.addActionListener(new csearch()); 
    } 
    class csearch implements ActionListener { 
     public void actionPerformed(ActionEvent event) { 
      SimpleDateFormat formatter = new SimpleDateFormat(DATE_FORMAT); 
      Date date = new Date(); 
      TimeZone tz = TimeZone.getDefault(); 
      pane.setBounds(10, 530, 760, 40); 
      pane1.setBounds(10, 260, 760, 40); 
      System.out.println("TimeZone : " + tz.getID() + " - " + tz.getDisplayName()); 
      System.out.println("TimeZone : " + tz); 
      System.out.println("Date (Ro - UTC+02:00) : " + formatter.format(date)); 
      date10.setText("Current time for Romania"); 
      date7.setText(String.valueOf("Time Zone : " + tz.getID() + " - " + tz.getDisplayName())); 
      date8.setText(String.valueOf("TimeZone : " + tz)); 
      date9.setText(String.valueOf("Date (Ro - UTC+02:00) : " + formatter.format(date))); 
      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(); 
      gradeCombo = new JComboBox <Object> (TimeZone.getAvailableIDs()); 
     } 
    } 
} 

谁能帮助我?为什么时区不显示在组合框中?

回答

1

而不是实例化一个新的JComboBox只需将适当的模型设置为您的现有组合。

gradeCombo.setModel(new DefaultComboBoxModel<Object>(TimeZone.getAvailableIds()));