Java JTable 實現(xiàn)日歷的示例
效果圖:
主要思想:日歷最核心的功能就是能顯示某年某月對應(yīng)的日期和星期幾。因此只要實現(xiàn)傳入具體的年份和月份,得到一組存放了日期的數(shù)組a[ ]即可。其中數(shù)組的大小設(shè)置成42,要考慮的問題是當(dāng)月的第一天對應(yīng)星期幾。日期數(shù)組中的前七個,肯定包含了當(dāng)月的第一天,把這一天找到,將“1”填入,后面的日期依次累加直到加完該月最后一天為止。
MyCalendar類:
得到用于顯示日期數(shù)組a[ ]
import java.util.Calendar; public class MyCalendar { String day[]; int year = 2020,month=0; public String[] getDay() { return day; } public void setDay(String[] day) { this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } private boolean isLeapYear() { if(this.year%4==0 && this.year%100!=0){ return true; }else if(this.year%400==0){ return true; }else return false; } //獲得顯示數(shù)組 public String[] getCalendar(){ Calendar calendar=Calendar.getInstance(); String a[]=new String[42]; calendar.set(year,month-1,1); int weekday=calendar.get(Calendar.DAY_OF_WEEK)-1; int day=0; int days = 31; if (this.month == 4 || this.month == 6 || this.month == 9 || this.month == 11) days = 30; if (this.month == 2 && isLeapYear()) days = 29; if (this.month == 2 && !isLeapYear()) days = 28; for(int i = weekday,n=1;i< weekday +days;i++){ a[i]=String.valueOf(n); n++; } return a; } }
MyFrame類:
創(chuàng)造顯示面板,主要用到JTable。
public class MyFrame extends JFrame implements ActionListener { //存儲數(shù)據(jù) MyCalendar calendar = new MyCalendar(); JComboBox choiceYear,choiceMonth; JTable table = null; JPanel root = new JPanel(); JLabel lyear, lmonth; private Object[] name = {"日","一","二","三","四","五","六"}; private TableModel tableModel = new DefaultTableModel(name,6); // private static int row = 6; // private static int column = 7; public MyFrame(String title) { super(title); this.setContentPane(root); root.setLayout(new BorderLayout()); //年月選擇欄 choiceYear=new JComboBox(); choiceMonth=new JComboBox(); lyear=new JLabel("年"); lmonth=new JLabel("月 "); for(int i=1990;i<2050;i++) choiceYear.addItem(i); choiceYear.addActionListener(this); for(int i=1;i<=12;i++) choiceMonth.addItem(i); choiceMonth.addActionListener(this); JPanel pNorth=new JPanel(); pNorth.add(choiceYear); pNorth.add(lyear); pNorth.add(choiceMonth); pNorth.add(lmonth); root.add(pNorth,BorderLayout.NORTH); // 表格初始化 setYearAndMonth( 1990, 1); } //設(shè)置年月日 public void setYearAndMonth(int y,int m){ calendar.setYear(y); calendar.setMonth(m); String day[]=calendar.getCalendar(); Vector<Object> rowData = new Vector<>(); int row = 0; int column = 0; for(int i = 0; i< 42; i++) { row = i / 7; column = i % 7; tableModel.setValueAt(day[i], row, column); } // 創(chuàng)建 JTable,直接重寫 isCellEditable(),設(shè)為不可編輯 table = new JTable(tableModel){ @Override public boolean isCellEditable(int row, int column) { return false; } }; JScrollPane scrollPane = new JScrollPane(table); root.add(scrollPane, BorderLayout.CENTER); // 添加到主界面 table.setFillsViewportHeight(true); table.setRowSelectionAllowed(true); // 整行選擇 table.setRowHeight(30); } public void actionPerformed(ActionEvent e){ //選擇年份 if (e.getSource()==choiceYear){ calendar.setYear((Integer) choiceYear.getSelectedItem()); String day[]=calendar.getCalendar(); Vector<Object> rowData = new Vector<>(); int row = 0; int column = 0; for(int i = 0; i< 42; i++) { row = i / 7; column = i % 7; tableModel.setValueAt(day[i], row, column); } table = new JTable(tableModel){ @Override public boolean isCellEditable(int row, int column) { return false; } }; JScrollPane scrollPane = new JScrollPane(table); root.add(scrollPane, BorderLayout.CENTER); // 添加到主界面 table.setFillsViewportHeight(true); table.setRowSelectionAllowed(true); // 整行選擇 table.setRowHeight(30); } //選擇月份 else if (e.getSource()==choiceMonth){ calendar.setMonth((Integer) choiceMonth.getSelectedItem()); String day[]=calendar.getCalendar(); Vector<Object> rowData = new Vector<>(); int row = 0; int column = 0; for(int i = 0; i< 42; i++) { row = i / 7; column = i % 7; tableModel.setValueAt(day[i], row, column); } } table = new JTable(tableModel){ @Override public boolean isCellEditable(int row, int column) { return false; } }; JScrollPane scrollPane = new JScrollPane(table); root.add(scrollPane, BorderLayout.CENTER); // 添加到主界面 table.setFillsViewportHeight(true); table.setRowSelectionAllowed(true); // 整行選擇 table.setRowHeight(30); } }
ShowView類:
用于顯示窗口,照抄即可,無需理解。
import java.awt.Container; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; public class ShowView { private static void createGUI() { // 語法:因為MyFrame是JFrame的子類,所以可以這么寫 JFrame frame = new MyFrame("日歷"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 設(shè)置窗口的其他參數(shù),如窗口大小 frame.setSize(400, 300); // 顯示窗口 frame.setVisible(true); } public static void main(String[] args) { // 此段代碼間接地調(diào)用了 createGUI() javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createGUI(); } }); } }
以上就是Java JTable 實現(xiàn)日歷的示例的詳細(xì)內(nèi)容,更多關(guān)于Java JTable 實現(xiàn)日歷的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring Boot Admin管理監(jiān)控數(shù)據(jù)的方法
本篇文章主要介紹了Spring Boot Admin管理監(jiān)控數(shù)據(jù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Python如何使用@property @x.setter及@x.deleter
這篇文章主要介紹了Python如何使用@property @x.setter及@x.deleter,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05SpringBoot 使用 Ehcache 作為緩存的操作方法
這篇文章主要介紹了SpringBoot 如何使用 Ehcache 作為緩存,我們通過添加 Ehcache 依賴、創(chuàng)建 Ehcache 配置文件并在 Spring Boot 應(yīng)用程序的配置文件中啟用 Ehcache 緩存,來配置 Ehcache 緩存,需要的朋友可以參考下2023-06-06關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題
這篇文章主要介紹了關(guān)于Prometheus + Spring Boot 應(yīng)用監(jiān)控的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03SSH框架網(wǎng)上商城項目第3戰(zhàn)之使用EasyUI搭建后臺頁面框架
SSH框架網(wǎng)上商城項目第3戰(zhàn)之使用EasyUI搭建后臺頁面框架,討論兩種搭建方式:基于frameset和基于easyUI,感興趣的小伙伴們可以參考一下2016-05-05