W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
JSpinner組件組合來自JFormattedTextField和可編輯的JComboBox的函數(shù)。
JSpinner可以有一個(gè)選擇列表,同時(shí),我們也可以應(yīng)用一個(gè)格式到顯示的值。
它從選擇列表中一次只顯示一個(gè)值,它允許我們輸入一個(gè)新值。
JSpinner中的選擇列表必須是有序列表。
JSpinner根據(jù)其型號(hào)提供紡紗能力。
我們必須在JSpinner的構(gòu)造函數(shù)中提供一個(gè)模型,除非我們只需要一個(gè)帶有整數(shù)列表的JSpinner。
JSpinner支持三種有序的選擇列表。
它提供了三個(gè)類來創(chuàng)建三種不同類型的列表的模型:
Spinner模型是SpinnerModel接口的一個(gè)實(shí)例。它定義了getValue(),setValue(),getPreviousValue()和getNextValue()方法來處理JSpinner中的值。
SpinnerNumberModel類可以旋轉(zhuǎn)一個(gè)有序的數(shù)字列表。我們需要在列表中指定最小值,最大值和當(dāng)前值。我們還可以指定當(dāng)我們使用JSpinner的向上/向下按鈕時(shí)用于遍歷數(shù)字列表的步長(zhǎng)值。
以下代碼創(chuàng)建一個(gè)JSpinner,其中包含1到10之間的數(shù)字列表。它讓我們以1為步長(zhǎng)旋轉(zhuǎn)列表。字段的當(dāng)前值設(shè)置為5。
int minValue = 1; int maxValue = 10; int currentValue = 5; int steps = 1; SpinnerNumberModel nModel = new SpinnerNumberModel(currentValue, minValue, maxValue, steps); JSpinner numberSpinner = new JSpinner(nModel);
SpinnerDateModel類提供了一個(gè)模型來旋轉(zhuǎn)日期的有序列表。
我們需要指定開始日期,結(jié)束日期,當(dāng)前值和步驟。
以下代碼創(chuàng)建一個(gè)JSpinner,用于旋轉(zhuǎn)2000年1月1日至2050年12月31日的日期列表,每次一天。
將當(dāng)前系統(tǒng)日期設(shè)置為字段的當(dāng)前值。
Calendar calendar = Calendar.getInstance(); calendar.set(2000, 1, 1); Date minValue = calendar.getTime(); calendar.set(2050, 12, 31); Date maxValue = calendar.getTime(); Date currentValue = new Date(); int steps = Calendar.DAY_OF_MONTH; // Must be a Calendar field SpinnerDateModel dModel = new SpinnerDateModel(currentValue, minValue, maxValue, steps); dateSpinner = new JSpinner(dModel);
JSpinner中的日期值將以默認(rèn)語言環(huán)境格式顯示。
SpinnerListModel類允許我們旋轉(zhuǎn)任何對(duì)象的有序列表。
SpinnerListModel類允許我們旋轉(zhuǎn)任何對(duì)象的有序列表。...
對(duì)象的toString()方法的String值顯示在JSpinner中。
以下代碼段創(chuàng)建一個(gè)JSpinner,顯示四個(gè)季節(jié)的列表:
String[] seasons = new String[] {"Spring", "Summer", "Fall", "Winter"}; SpinnerListModel sModel = new SpinnerListModel(seasons); listSpinner = new JSpinner(sModel);
JSpinner使用編輯器對(duì)象顯示當(dāng)前值。 它有以下三個(gè)靜態(tài)內(nèi)部類來顯示三種不同類型的有序列表:
要以特定格式顯示數(shù)字或日期,我們需要為JSpinner設(shè)置一個(gè)新的編輯器。
數(shù)字和日期編輯器的編輯器類允許我們指定格式。
以下代碼將數(shù)字格式設(shè)置為“00"。
JSpinner.NumberEditor Editor = new JSpinner.NumberEditor(numberSpinner,“00");numberSpinner.set Editor(Editor);
以下代碼將日期格式設(shè)置為mm / dd / yyyy
JSpinner.DateEditor dEditor = new JSpinner.DateEditor(dateSpinner, "mm/dd/yyyy"); dateSpinner.setEditor(dEditor);
我們可以使用JSpinner或SpinnerModel定義的getValue()方法來獲取JSpinner中的當(dāng)前值作為對(duì)象。
SpinnerNumberModel和SpinnerDateModel定義了getNumber()和getDate()方法,分別返回Number和Date對(duì)象。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: