版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、Swing日期控件一、截圖:二、源代碼: package com.longtu.rpt.app.view.rptview.multisheet; import java.util.Calendar; import javax.swing.JComboBox; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; public class DateChoiceActive /初始化年選擇下拉框 public static String initItemsYear() String arrYear = new
2、String70; for(int i=1980;i2050;i+) arrYeari-1980=String.valueOf(i); return arrYear; /初始化月選擇下拉框 public static void initItemsMonth(JComboBox cmbMonth)cmbMonth.addItem(一月);cmbMonth.addItem(二月);cmbMonth.addItem(三月);cmbMonth.addItem(四月);cmbMonth.addItem(五月);cmbMonth.addItem(六月);cmbMonth.addItem(七月);cmbMo
3、nth.addItem(八月);cmbMonth.addItem(九月);cmbMonth.addItem(十月);cmbMonth.addItem(十一月);cmbMonth.addItem(十二月);public static String initComboboxSelected(int month)if(1 = month)return 一月;else if(2 = month)return 二月;else if(3 = month)return 三月;else if(4 = month)return 四月;else if(5 = month)return 五月;else if(6 =
4、 month)return 六月;else if(7 = month)return 七月;else if(8 = month)return 八月;else if(9 = month)return 九月;else if(10 = month)return 十月;else if(11 = month)return 十一月;else return 十二月;public static int getSelectItemValue(String month)if(一月.equals(month)return 1;else if(二月.equals(month)return 2;else if(三月.eq
5、uals(month)return 3;else if(四月.equals(month)return 4;else if(五月.equals(month)return 5;else if(六月.equals(month)return 6;else if(七月.equals(month)return 7;else if(八月.equals(month)return 8;else if(九月.equals(month)return 9;else if(十月.equals(month)return 10;else if(十一月.equals(month)return 11;else if(十二月.e
6、quals(month)return 12;return 0;private static int getCurrentMonthFirstDay(int year,int month)Calendar now = Calendar.getInstance();now.set(year,month-1,1);int week = now.get(Calendar.DAY_OF_WEEK)-1;return week;private static int getCurrentMonthDays(int year,int month)int days = 0; / 存儲當月的天數(shù)boolean i
7、sRn = false;/是否閏年for (int i = 1900; i year; i+) if (i % 4 = 0 & !(i % 100 = 0) | i % 400 = 0) isRn = true; else for (int i = 1; i = month; i+) switch (i) case 1:case 3:case 5:case 7:case 8:case 10:case 12:days = 31;break;case 2:if (isRn) days = 29; else days = 28; break;default:days = 30; break;retu
8、rn days;public static void updateMonthPanel(JTable table,DefaultTableModelmodel,int year,int month,Object rowData)int week = getCurrentMonthFirstDay(year,month);int totalday = getCurrentMonthDays(year,month);int count = 1;for(int i=0;i6;i+)model.addRow(rowData);for(int j=0;jtotalday)table.setValueAt
9、(, i, j);else if(i=0&week!=0)j = week;table.setValueAt(count, i, j);elsetable.setValueAt(count, i, j);week = 0;count+;public static String formatDateToString(int year,int month,int date)String day = ;String mon = ;if(date 10)day = 0+date;elseday = +date;if(month 0) result.x -= offsetX;if (offsetY 0)
10、 result.y -= height + r.height;dialog = new JDialog();dialog.setModal(false);dialog.setUndecorated(true);dialog.setResizable(true);dialog.setLocation(result);dialog.setSize(width, height);dialog.addWindowListener(new WindowAdapter() /在任意的非日期選擇區(qū)單擊,則日期選擇組件將變?yōu)榉腔顒訝顟B(tài),自動釋放資源。public void windowDeactivated(
11、WindowEvent e) javax.swing.JDialog f = (javax.swing.JDialog)e.getSource();f.dispose();JDialog.setDefaultLookAndFeelDecorated(true););dialog.getContentPane().add(getContentPanel();dialog.setVisible(true););btn.setPreferredSize(new Dimension(20,20);this.add(btn,b.EAST);private JPanel getContentPanel()
12、 if(content = null)content = new JPanel();content.setLayout(null);content.add(getToolbarPanel();content.add(getCenterPanel();content.add(getBottomPanel();content.setVisible(true);return content;private JPanel getToolbarPanel() if(toolbar = null)toolbar = new JPanel();FlowLayout f = new FlowLayout();
13、f.setAlignment(f.LEFT);toolbar.setLayout(f);initToolbarPanel();toolbar.setBounds(0, 0, 222, 25);return toolbar;/初始化工具欄private void initToolbarPanel() if(btnLeft = null)btnLeft = new JButton(null,newImageIcon(Main.class.getResource(/icons/leftbtn.png);btnLeft.setPreferredSize(new Dimension(20, 20); b
14、tnLeft.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) if(1980 = years)return;elseyears = years - 1;spinYear.setValue(String.valueOf(years); if(null != table)refreshTable(););toolbar.add(btnLeft);if(cmbMonth = null)cmbMonth = new JComboBox(); DateChoiceActive.initIt
15、emsMonth(cmbMonth); cmbMonth.addItemListener(new ItemListener() public void itemStateChanged(ItemEvent e) cmbMonth = (JComboBox) e.getSource(); String mon = cmbMonth.getSelectedItem().toString(); month = DateChoiceActive.getSelectItemValue(mon); if(null != table) refreshTable(); ); cmbMonth.setSelec
16、tedItem(DateChoiceActive.initComboboxSelected(month);cmbMonth.setPreferredSize(new Dimension(70, 20);toolbar.add(cmbMonth);if(spinYear = null)String year = DateChoiceActive.initItemsYear();SpinnerModel models = new SpinnerListModel(year);spinYear = new JSpinner(models);spinYear.setValue(String.value
17、Of(years);spinYear.addChangeListener(new ChangeListener()public void stateChanged(ChangeEvent e) spinYear = (JSpinner)e.getSource();years = Integer.parseInt(spinYear.getValue().toString();refreshTable(););spinYear.setPreferredSize(new Dimension(70, 20);toolbar.add(spinYear);if(btnRight = null)btnRig
18、ht = new JButton(null,newImageIcon(Main.class.getResource(/icons/rightbtn.png);btnRight.setPreferredSize(new Dimension(20, 20);btnRight.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) if(2049 = years)return;elseyears = years + 1;spinYear.setValue(String.valueOf(years
19、);if(null != table)refreshTable(););toolbar.add(btnRight);private JPanel getCenterPanel() if(center = null)center = new JPanel();center.setLayout(null);center.setBounds(0,0, 220,164);initCenterPanel();center.setVisible(true);return center;/初始化日期界面private void initCenterPanel() JScrollPane jp = null;
20、if(table = null)table = new JTable(model);model.addColumn(日);model.addColumn(一);model.addColumn(二);model.addColumn(三);model.addColumn(四);model.addColumn(五);model.addColumn(六);table.setRowHeight(20);jp = new JScrollPane(table);table.getTableHeader().setReorderingAllowed(false);jp.setBounds(0, 26, 221
21、,164);DateChoiceActive.updateMonthPanel(table, model, years, month,rowData);table.setDefaultRenderer(Object.class, new EvenOddRenderer();table.addMouseListener(new MouseListener()public void mouseClicked(MouseEvent e) public void mouseEntered(MouseEvent e) public void mouseExited(MouseEvent e) publi
22、c void mouseReleased(MouseEvent e) public void mousePressed(MouseEvent e) if(2 = e.getClickCount()Object o = e.getSource();if(o instanceof JTable)JTable t = (JTable) o;int row = t.getSelectedRow();int col = t.getSelectedColumn();DefaultTableModel tb = (DefaultTableModel)t.getModel();if(null!=tb.getV
23、alueAt(row,col)&!.equals(tb.getValueAt(row, col).toString()int date = Integer.parseInt(tb.getValueAt(row,col).toString();text.setText(DateChoiceActive.formatDateToString(years,month,date);dialog.dispose();JDialog.setDefaultLookAndFeelDecorated(true););center.add(jp);private JPanel getBottomPanel() i
24、f(bottom = null)bottom = new JPanel();bottom.setLayout(null);bottom.setBounds(0, 166, 220, 20);initBottomPanel();bottom.setBorder(BorderFactory.createTitledBorder ();/設置邊框bottom.setVisible(true); return bottom; /初始化底部按鈕 private void initBottomPanel() if(today = null) today = new JButton(今天); today.s
25、etBounds(58, 0, 50, 20); today.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) SimpleDateFormat sdf = new SimpleDateFormat(yyyy-MM-dd); String now = sdf.format(new Date(); years = Integer.parseInt(now.substring(0, 4); spinYear.setValue(String.valueOf(years); String
26、mon = now.substring(5, 7); if (mon.startsWith(0) mon = mon.substring(1); month = Integer.parseInt(mon); cmbMonth.setSelectedItem(DateChoiceActive.initComboboxSelected(month);text.setText(now);dialog.dispose();JDialog.setDefaultLookAndFeelDecorated(true););bottom.add(today);if(clear = null)clear = ne
27、w JButton(清除);clear.setBounds(116, 0, 50, 20);clear.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) text.setText();dialog.dispose();JDialog.setDefaultLookAndFeelDecorated(true););bottom.add(clear);/刷新表格protected void refreshTable() (DefaultTableModel) table.getModel(
28、).getDataVector().clear();/清除表格數(shù)據(jù)(DefaultTableModel) table.getModel().fireTableDataChanged();/通知模型更新table.updateUI();/刷新表格table.setRowHeight(20);DateChoiceActive.updateMonthPanel(table, model, years, month,rowData);/獲取日期控件的值public String getDate()return text.getText();package com.longtu.rpt.app.view.rptview.multisheet;import java.aw
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024至2030年中國干粉式膠粘復合機數(shù)據(jù)監(jiān)測研究報告
- 2024至2030年中國外衣行業(yè)投資前景及策略咨詢研究報告
- 2024至2030年中國親水型氨基硅油數(shù)據(jù)監(jiān)測研究報告
- 2024至2030年中國丙酰苯胺數(shù)據(jù)監(jiān)測研究報告
- 2024至2030年中國PVC無毒粒料數(shù)據(jù)監(jiān)測研究報告
- 2024年中國鞋口市場調(diào)查研究報告
- 2024年中國重負荷純油切削油市場調(diào)查研究報告
- 2024年中國珍珠眼貼膜市場調(diào)查研究報告
- 2024年中國大口徑中空纏繞管機組市場調(diào)查研究報告
- 2024年中國雙層小童毯市場調(diào)查研究報告
- 肩周炎的護理ppt課件
- 施工工期計算器
- 木棧道安裝檢驗批質(zhì)量驗收記錄表
- TBM豆礫石回填灌漿施工方案十八局修改8-275
- PLC課程設計:YA32—200四柱式萬能液壓機系統(tǒng)課件
- 山東省高中英語課堂教學基本要求-2021版 附件
- 緊固件國家標準目錄
- 建筑工程資料管理標準(吉林省地方標準db22t4982010)
- 初二藏文 (2)
- 節(jié)約型公共機構(gòu)示范單位評價標準
- 綠色(環(huán)保)施工方案(DOC)
評論
0/150
提交評論