2014-03-03 34 views
0

我有這樣定義一個DateTime微調:SWT的DateTime的setDate()錯誤

//Class Global 
DateTime dateMin = null; 

//added to the UI 
DateTime dateMin = new DateTime(grpPlaybackSearchOptions, SWT.BORDER); 
dateMin.setBounds(335, 44, 123, 23); 

後來在我的代碼我希望用戶能夠加載可能已經保存過的日期。通過swt DateTime文檔挖掘我沒有看到我如何更新日期。

我已經試過,但收到異常錯誤:

//running from a method
dateMin.setDate(pMinDate[0], pMinDate[1], pMinDate[2]);

能的對象不會被更新或我試圖不正確地更新對象?

謝謝!

回答

1

不知道你用的文檔是非常明確的奮鬥,並提供了方法:

setDate(int year, int month, int day) 
setHours(int hours) 
setMinutes(int minutes) 
setSeconds(int seconds) 

下面是一個簡單的例子:

public static void main(String[] args) 
{ 
    final Display display = new Display(); 

    Shell shell = new Shell(display); 
    shell.setText("StackOverflow"); 
    shell.setLayout(new GridLayout(1, false)); 

    final DateTime date = new DateTime(shell, SWT.DATE); 
    final DateTime time = new DateTime(shell, SWT.TIME); 

    Button button = new Button(shell, SWT.PUSH); 
    button.setText("Change date"); 
    button.addListener(SWT.Selection, new Listener() 
    { 
     @Override 
     public void handleEvent(Event arg0) 
     { 
      date.setDate(1999, 0, 1); 
      time.setHours(0); 
      time.setMinutes(0); 
      time.setSeconds(0); 
     } 
    }); 

    shell.pack(); 
    shell.open(); 
    while (!shell.isDisposed()) 
    { 
     if (!display.readAndDispatch()) 
      display.sleep(); 
    } 
} 

它將設置日期時間到:01/01/1999 00:00:00(使用dd/MM/yyyy HH:mm:ss即...)

+0

Baz,謝謝你的回覆SE。問題是我已經定義了DateTime dateMin = null;作爲該類的一個對象,然後在該方法內再次定義它。 – txcotrader

+0

@txcotrader啊,我明白了。所以你基本上「隱藏」你的領域變量......呃,那會發生;) – Baz