2009-06-23 161 views
0

我正在創建JSpinner並使用自定義格式設置NumberEditor。JSpinner編輯器區域

但無論我做什麼格式使用「。」而不是「,」,而不是根據我的區域設置(pt_BR)。

priceSpinner = new JSpinner(); 
priceSpinner.setEditor(new JSpinner.NumberEditor(priceSpinner, "0.00")); 

是否可以使用「,」而不是「。」。作爲小數點分隔符?

回答

3

通過指定自定義格式模式,您告訴JRE忽略您的語言環境並使用該特定格式。如果你對數字的微調到小數點後兩位以後是簡單地說,使用則setModel()而不是setEditor(),它會爲你創建一個NumberEditor:

JSpinner priceSpinner = new JSpinner(); 
    priceSpinner.setModel(new SpinnerNumberModel(0.00, 0.00, 100.00, 0.01)); 

如果你絕對必須使用自己的格式模式,您可以調整該模式的十進制格式符號後創建它:

JSpinner priceSpinner = new JSpinner(); 
    JSpinner.NumberEditor editor = new JSpinner.NumberEditor(priceSpinner, "0.00"); 
    DecimalFormat format = editor.getFormat(); 
    //better to use Locale.getDefault() here if your locale is already pt_BR 
    Locale myLocale = new Locale("pt", "BR"); 
    format.setDecimalFormatSymbols(new DecimalFormatSymbols(myLocale)); 
    priceSpinner.setEditor(editor); 
+0

謝謝,它的工作。我真的需要自定義模式,因爲我想要「1,00」而不是「1」。 – tuler 2009-06-24 14:06:21

相關問題