版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、文檔import java.awt.*;import java.awt.eve nt.MouseAdapter;import java.awt.eve nt.MouseEve nt;import java.util.*;import java.util.Timer;import javax.swi ng.*;import javax.swi ng.eve nt.*;import javax.swi ng.table.*;這是一個用JList和JTable完成的簡單日歷public class cale nderpublic static void main(String args)Even t
2、Queue.i nvokeLater( new Runn able()public void run()JFrame frame = new Cale ndarFrame();frame.setDefaultCloseOperati on (JFrame.EXIT_ON_CLOSE); frame.setVisible(true););class Cale ndarFrame exte nds JFrameprivate static fin al lo ng serialVersio nUID = 8067844586793608064L;JPanel listPanel; /顯示月份的列表
3、面板JPa nel tablePa nel; /顯示日期的表格面板JPa nel textPa nel; / 文本面板JMenuBar menuBar; / JMenuBar,JMenu定義的變量都沒有實現(xiàn)功能JMe nu fileMe nu;JMenu editMe nu;JMenu formatMe nu;JMenu lookMe nu;JMe nu helpMe nu;JEditorPane text; /自帶復(fù)制,剪切和刪除快捷鍵功能的面板JScrollPa ne textscrollpa ne;JScrollPa ne tablescrollpa ne;JList list; II顯
4、示月份的列表JTable table; II顯示日期的表格JPa nel yearPa nel; II顯示年份的面板 JSpinner spinner; II JSpinner 空間 JLabel label = new JLabel();public static boolea n flag = true;public Cale ndarFrame()table = new JTable(new MyTableModel();tablescrollpa ne = new JScrollPa ne(table);for (int i = 0; i 7; i+)獲取表格每一列TableColu
5、mn colu mn = table.getColu mnM odel().getColu mn (i); column.setResizable(false);/ 不允許改變列的大小table.setBackgrou nd(Color.GREEN);table.setRowHeight(72); II 設(shè)定每列的大小 table.setRowSelectio nAllowed(false);/ 設(shè)定行的選擇模式 table.setDefaultRenderer(String.class, new MyTableRenderer();list = new JList(new AbstractL
6、istModel() II匿名的 list 模型類,提供 list 顯示的月份數(shù)據(jù)private static fin al lo ng serialVersio nUID = -3703164069291737586L;public Object getElementAt(int index) Stri ng obj = n ull;switch (in dex)case 0:obj = Ja n;break;case 1:obj = Feb;break;case 2:obj = Mar;break;case 3:obj = Apr;break;case 4:obj = May; break
7、;case 5:obj = Jun; break;case 6:obj = Jul; break;case 7:obj = Aug; break;case 8:obj = Sep; break;case 9:obj = Oct; break;case 10:obj = Nov; break;case 11:obj = Dec; break;return obj;Overridepublic int getSize()return 12;);list.setSelectio nM ode(ListSelectio nM odel.SINGLE_SELECTION); /設(shè)定列表的選擇模式list
8、.setCelIRe nderer( new MyListCellRe nderer(); /列表元素的繪制類MyListCelIRe ndererlist.setSelectedIndex(Calendar.getlnstance().get(Calendar.MONTH);/ JList 默認(rèn)的月份為現(xiàn) 實的月份list.addListSelecti on Liste ner(new ListSelectio nListe ner()當(dāng)某個列表被選中時,更新日期table控件public void valueCha nged(ListSelect ion Eve nt e) flag =
9、true;table.updateUI(););list.addMouseListe ner(new MouseAdapter()public void mouseClicked(MouseEve nt evt)if (evt.getClickCou nt() = 2)Stri ng s = (Stri ng) list.getSelectedValue();JOpti onPan e.showMessageDialog(list, s); );listPa nel = new JPa nel();listPa nel.setLayout (new BorderLayout();listPa
10、nel.add(list);tablePa nel = new JPa nel();tablePa nel.setBackgro un d(Color.white);tablePa nel.setLayout (new BorderLayout();tablePa nel.add(tablescrollpa ne);/ table.setBackgro un d(Color.gree n);yearPanel = new JPanel();將label控件和JSpinner控件加入年份面板上yearPa nel.setLayout (new GridLayout(0, 4);JLabel go
11、ngyuan = new JLabel(公元, JLabel.RIGHT);JLabel year = new JLabel(年);spinner = new JSp inner(new Spi nn erNumberModel(Cale ndar.getl nsta nce().get(Calendar.YEAR), null, null, 1);spinner.addChangeListener(new ChangeListener() / 為 JSpinner 空間添加事件監(jiān)聽器 Overridepublic void stateCha nged(Cha ngeEve nt e) fla
12、g = true;table.updateUI(););yearPa nel.add(go ngyua n);yearPa nel.add(sp inner);yearPa nel.add(year);yearPa nel.add(label);showTime();new Timer().schedule( new TimerTask()Overridepublic void run()while (true)showTime();, 1000);tablePa nel.add(yearPa nel, BorderLayout.NORTH);/ 將年份面板加入到表格面板的北部 textPa
13、nel = new JPa nel();textPa nel.setLayout (new BorderLayout();text = new JEditorPa ne();textscrollpane = new JScrollPane(text);textPa nel.add(textscrollpa ne);text.setFo nt(new Fo nt(Sa nsSerif, Font. PLAIN, 40);tablePa nel.add(textPa nel, BorderLayout.SOUTH);menuBar = new JMenuBar(); JMenu 定義的空間都沒實現(xiàn)
14、功能 fileMenu = new JMenu(文件);editMenu = new JMenu(編輯);formatMenu = new JMenu(格式);lookMenu = new JMenu(查看);helpMenu = new JMenu(幫助); men uBar.add(fileMe nu);men uBar.add(editMe nu);men uBar.add(formatMe nu);men uBar.add(lookMe nu);men uBar.add(helpMe nu);GridBagLayout layout = new GridBagLayout(); / 設(shè)
15、成 GridBagLayout 布局 GridBagC on stra ints con stra ints = new GridBagC on stra in ts();this.setFo nt(n ew Fon t(Sa nsSerif, Fon t.PLAIN, 14);this.setLayout(layout);this.setTitle(”萬年歷);/設(shè)定標(biāo)題this.setIc onl mage (new ImageIc on (restitle.jpg).getlmage(); /設(shè)定圖標(biāo)co nstrai nts.fill = GridBagCo nstrai nts.BO
16、TH;con strai nts.weightx = 0.5;con stra in ts.weighty = 0.5;layout.setC on stra in ts(listPa nel, con stra in ts);con strai nts.weightx = 8.0;con stra in ts.weighty = 0.5;layout.setC on stra in ts(tablePa nel, con stra in ts);this.setJMe nuBar(me nuBar);this.add(listPa nel);this.add(tablePa nel);thi
17、s.setSize(this.getToolkit().getScree nSize().width * 3 / 4, this .getToolkit().getScree nSize().height * 3 / 4); /設(shè)定窗體的大小private void showTime()int hour = Cale ndar.getl nsta nce().get(Cale ndar.HOUR_OF_DAY);int mi nute = Cale ndar.getl nsta nce().get(Cale ndar.MINUTE);int seco nd = Cale ndar.getl n
18、sta nce().get(Cale ndar.SECOND);Strin gBuilder sb = new Stri ngBuilder();if (hour 10)sb.appe nd(0 + hour); elsesb.appe nd(hour);if (mi nute 10)sb.appe nd(:0 + minu te); elsesb.appe nd(: + minu te);if (second 10)sb.appe nd(:O + sec on d); elsesb.appe nd(: + sec on d);label.setFo nt(new Fon t(Sa nsSer
19、if, Fon t.BOLD, 12);label.setForegrou nd(Color.BLACK); label.setText(sb.toStri ng();class MyTableModel exte nds AbstractTableModel / 表格模型類,提供表格數(shù)據(jù) private static final long serialVersionUID = 1L;Strin g colu mnN ame = new Strin g/表格列名 Sunday, Monday, Tuesday, Wednesday, Thursday, Friday,Saturday ;Ove
20、rridepublic in t getColu mn Cou nt() / 返回表格列數(shù)return colu mnN ame.le ngth;public in t getRowCou nt() / 返回表格行數(shù)return 6;public String getColumnName(int col) /獲得表格列名return colu mnN amecol;public Class getColu mn Class(i nt c)return getValueAt(0, c).getClass();public Object getValueAt( int row In dex, in
21、t colu mnIn dex)int mon th = list.getSelectedl ndex(); 獲得列表的月份int year = (Integer) spinner.getValue(); 獲得 spinner 顯示的年份Gregoria nCale ndar gc = new Gregoria nCale ndar(year, mon th, 1);int dayOfWeek = gc.get(Cale ndar.DAY_OF_WEEK) - 1; /獲得這個月的第一天是星期幾int countDayInMonth = 0;/ 某月的總天數(shù),這里的月是從0-11,表示1-12
22、月if (mon th = 0 | mon th = 2 | month = 4 | mon th = 6| month = 7 | month = 9 | month = 11)countDaylnMonth = 31; / 是 31 天的月份 else if (month = 3 | month = 5 | month = 8 | month = 10)countDaylnMonth = 30; / 是 30 天的月份 else if (gc.isLeapYear(year) & month = 1)countDaylnMonth = 29; / 閏年 2 月 else if (!gc.i
23、sLeapYear(year) & month = 1)countDaylnMonth = 28; / 非閏年 2 月String value = ; /因為返回值是 Object類型,所以不能直接返回int類型的數(shù)據(jù),所以轉(zhuǎn)換成String類型if (rowl ndex = 0) / 第一列的數(shù)據(jù)if (colum nln dex dayOfWeek)value =; elsevalue = + (colu mnln dex - dayOfWeek + 1); else/其余列的數(shù)據(jù)if (rowl ndex - 1) * 7) + (colu mnln dex + 1 + 7 - dayO
24、fWeek) = cou ntDayl nMonth) value = (rowl ndex - 1) * 7) + (colu mnlndex + 1 + 7 - dayOfWeek)+ ;return value;列表元素繪制類繼承class MyListCelIRe nderer exte nds JLabel impleme nts ListCelIRe nderer/ 自 JLabel,實現(xiàn) ListCellRenderer 接口private static final long serialVersionUID = 1L;public Comp onent getListCellR
25、e ndererComp onen t(JList list, / the list Object value, / value to display int in dex, / cell in dex boolea n isSelected, / is the cell selectedboolea n cellHasFocus) / does the cell have focusStri ng s = value.toStri ng().trim(); / 獲取選中元素的字符容 setText(s); /顯示這個字符Dime nsion dime nsion = list.getSize
26、(); / 設(shè)定這個每個元素的大小 int height = dime nsion .height / 12;int width = dime nsio n.width;setSize(width, height);if (isSelected) /如果該元素被選中,則背景色為紅色setBackgro un d(Color.red);setForegro un d(list.getSelect ion Backgro un d(); else/未被選中的元素背景色顯示為灰色,前景色為粉紅色setBackgro un d(Color.gray);setForegro un d(Color.p i
27、n k);setIcon(new ImageIcon(resWmon .jpg); / 設(shè)定該元素的圖標(biāo) setE nabled(list.isE nabled();setFont(new Font(SansSerif, Font.PLAIN, 20); /設(shè)定字體大小setOpaque(true);return this;class MyTableRe nderer exte nds JLabel impleme nts TableCellRe nderer private static final long serialVersionUID = 1L;public Comp onent getTableCe
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 現(xiàn)代簡約風(fēng)格與科技公司辦公環(huán)境的融合
- 現(xiàn)代物流技術(shù)與醫(yī)療物資保障體系
- 溝通技巧在教育工作中的創(chuàng)新應(yīng)用
- 環(huán)保技術(shù)在現(xiàn)代城市建設(shè)中的應(yīng)用
- 物流信息技術(shù)在商業(yè)領(lǐng)域的應(yīng)用
- Unit 3 Where did you go?PartB (說課稿)-2023-2024學(xué)年人教PEP版英語六年級下冊
- 2《燭之武退秦師》說課稿-2024-2025學(xué)年高一語文下學(xué)期同步說課稿(統(tǒng)編版必修下冊)
- 2024新教材高中地理 第四章 區(qū)域發(fā)展戰(zhàn)略 第二節(jié) 我國區(qū)域發(fā)展戰(zhàn)略說課稿 湘教版必修第二冊
- Unit3 Amazing animals(說課稿)-2024-2025學(xué)年人教PEP版(2024)英語三年級上冊001
- 2024年高中化學(xué) 第三章 晶體結(jié)構(gòu)與性質(zhì) 章末整合說課稿 新人教版選修3
- 2021中考地理真題試卷 山東省煙臺地理含答案
- 俄羅斯水資源現(xiàn)狀分析
- 非法捕撈水產(chǎn)品罪
- 新概念第一冊單詞匯總帶音標(biāo)EXCEL版
- 作用于血液及造血器官的藥 作用于血液系統(tǒng)藥物
- 心肺復(fù)蘇(最全版)完整版
- 春節(jié)節(jié)后施工復(fù)工安全培訓(xùn)
- GB/T 3478.1-1995圓柱直齒漸開線花鍵模數(shù)基本齒廓公差
- GB/T 1346-2001水泥標(biāo)準(zhǔn)稠度用水量、凝結(jié)時間、安定性檢驗方法
- FZ/T 25001-2012工業(yè)用毛氈
- 瑞幸咖啡SWOT分析
評論
0/150
提交評論