Java實(shí)現(xiàn)窗體程序顯示日歷表
本文實(shí)例為大家分享了Java實(shí)現(xiàn)窗體程序顯示日歷表的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)訓(xùn)要求:
1.簡單實(shí)現(xiàn)日歷功能,能查看前月后月的日歷功能。
2.使用JTable 組件顯示日歷。
參考結(jié)果:
代碼:
CalendaBean.java
import java.util.Calendar; ? public class CalendaBean { ?? ?Test test; ?? ?String[] day; ? ?? ?int year = 2017, month = 7; ? ?? ?public void setYear(int year) { ?? ??? ?this.year = year; ?? ?} ? ?? ?public void setMonth(int month) { ?? ??? ?this.month = month; ?? ?} ? ?? ?public int Last() { ?? ??? ?month--; ?? ??? ?if (month == 0) { ?? ??? ??? ?month = 12; ?? ??? ??? ?year--; ?? ??? ?} ?? ??? ?return month; ?? ?} ? ?? ?public int Next() { ?? ??? ?month++; ?? ??? ?if (month == 13) { ?? ??? ??? ?month = 1; ?? ??? ??? ?year++; ?? ??? ?} ?? ??? ?return month; ?? ?} ? ?? ?public String[] getCalendar() { ?? ??? ?String[] a = new String[42]; ?? ??? ?Calendar rili = Calendar.getInstance(); ?? ??? ?rili.set(year, month - 1, 1); ?? ??? ?int weekDay = rili.get(Calendar.DAY_OF_WEEK) - 1; ?? ??? ?int day = 0; ?? ??? ?if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ?? ??? ??? ??? ?|| month == 10 || month == 12) { ?? ??? ??? ?day = 31; ?? ??? ?} ?? ??? ?if (month == 4 || month == 6 || month == 9 || month == 11) { ?? ??? ??? ?day = 30; ?? ??? ?} ?? ??? ?if (month == 2) { ?? ??? ??? ?if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) ?? ??? ??? ??? ?day = 29; ?? ??? ??? ?else ?? ??? ??? ??? ?day = 28; ?? ??? ?} ?? ??? ?for (int i = 0; i < weekDay; i++) ?? ??? ??? ?a[i] = " "; ?? ??? ?for (int i = weekDay, n = 1; i < weekDay + day; i++) { ?? ??? ??? ?a[i] = String.valueOf(n); ?? ??? ??? ?n++; ?? ??? ?} ?? ??? ?for (int i = weekDay + day; i < a.length; i++) ?? ??? ??? ?a[i] = " "; ?? ??? ?return a; ?? ?} }
Change.java
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ? import javax.swing.JLabel; ? public class Change implements ActionListener { ?? ?CalendaBean c; ?? ?JLabel now; ?? ?Test test; ? ?? ?public void actionPerformed(ActionEvent e) { ?? ??? ?String str = e.getActionCommand(); ?? ??? ?if (str.equals("lastmonth")) { ?? ??? ??? ?c.Last(); ?? ??? ?} else if (str.equals("nextmonth")) { ?? ??? ??? ?c.Next(); ?? ??? ?} ?? ??? ?test.Rili(); ?? ??? ?now.setText("日歷 :" + c.year + "年" + c.month + "月"); ?? ?} ? }
Test.java
import java.awt.*; import java.awt.event.*; ? import javax.swing.*; import javax.swing.table.DefaultTableModel; ? public class Test extends JFrame { ?? ?JButton bx, by; ?? ?CalendaBean cb = new CalendaBean(); ?? ?Change change = new Change(); ?? ?DefaultTableModel model; ?? ?String[] label; ?? ?JLabel now; ?? ?JTable table = new JTable(); ?? ?JScrollPane pane = new JScrollPane(); ?? ?Object[] columnNames = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; ?? ?Object[][] rowData = { { null, null, null, null, null, null, null }, ?? ??? ??? ?{ null, null, null, null, null, null, null }, ?? ??? ??? ?{ null, null, null, null, null, null, null }, ?? ??? ??? ?{ null, null, null, null, null, null, null }, ?? ??? ??? ?{ null, null, null, null, null, null, null }, ?? ??? ??? ?{ null, null, null, null, null, null, null }, }; ? ?? ?public static void main(String[] args) { ?? ??? ?Test frame = new Test(); ?? ??? ?frame.setSize(500, 400); ?? ??? ?frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ?? ??? ?frame.setTitle("日歷"); ?? ??? ?frame.setVisible(true); ? ?? ?} ? ?? ?public Test() { ?? ??? ?change.test = this; ?? ??? ?int year, month; ?? ??? ?setLayout(new BorderLayout()); ?? ??? ?JPanel pNorth = new JPanel(); ?? ??? ?cb = new CalendaBean(); ?? ??? ?change.c = cb; ?? ??? ?bx = new JButton("上月"); ?? ??? ?by = new JButton("下月"); ?? ??? ?bx.setActionCommand("lastmonth"); ?? ??? ?by.setActionCommand("nextmonth"); ?? ??? ?bx.addActionListener(new ActionListener() { ?? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ?change.actionPerformed(e); ? ?? ??? ??? ?} ?? ??? ?}); ?? ??? ?by.addActionListener(new ActionListener() { ?? ??? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ??? ?change.actionPerformed(e); ?? ??? ??? ?} ?? ??? ?}); ?? ??? ?pNorth.add(bx); ?? ??? ?pNorth.add(by); ? ?? ??? ?Rili(); ?? ??? ?model = new DefaultTableModel(rowData, columnNames); ? ?? ??? ?table = new JTable(model); ?? ??? ?table.setRowHeight(38); ?? ??? ?table.getTableHeader().setResizingAllowed(false); ? ?? ??? ?pane = new JScrollPane(table); ?? ??? ?JPanel pSouth = new JPanel(); ?? ??? ?now = new JLabel(); ?? ??? ?now.setText("日歷:" + cb.year + "年" + cb.month + "月"); ?? ??? ?change.now = now; ?? ??? ?pSouth.add(now); ?? ??? ?add(pNorth, BorderLayout.NORTH); ?? ??? ?add(pane, BorderLayout.CENTER); ?? ??? ?add(pSouth, BorderLayout.SOUTH); ?? ?} ? ?? ?public void Rili() { ? ?? ??? ?String[] a = cb.getCalendar(); ?? ??? ?int x = 0; ?? ??? ?if (model != null) { ?? ??? ??? ?model.setRowCount(0); ?? ??? ?} ?? ??? ?for (int i = 0; i < 6; i++) { ?? ??? ??? ?for (int j = 0; j < 7; j++) { ?? ??? ??? ??? ?rowData[i][j] = a[x]; ?? ??? ??? ??? ?x++; ?? ??? ??? ?} ?? ??? ??? ?if (model != null) ?? ??? ??? ??? ?model.addRow(rowData[i]); ?? ??? ?} ?? ?} }
運(yùn)行結(jié)果
說明:這個程序與前幾天上傳的日歷顯示表差不多,只是改了中間的組件,原本以為是很簡單的事,結(jié)果做的時候才發(fā)現(xiàn)其中的難點(diǎn),初學(xué)Java還有很多不懂的地方,幸好有大神的解答,自己還有很多需要學(xué)習(xí)的地方。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java子類實(shí)例化總是默認(rèn)調(diào)用父類的無參構(gòu)造操作
這篇文章主要介紹了Java子類實(shí)例化總是默認(rèn)調(diào)用父類的無參構(gòu)造操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10MyBatis將查詢出的兩列數(shù)據(jù)裝配成鍵值對的操作方法
這篇文章主要介紹了MyBatis將查詢出的兩列數(shù)據(jù)裝配成鍵值對的操作代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08Kafka在客戶端實(shí)現(xiàn)消息的發(fā)送與讀取
這篇文章主要介紹了Kafka在客戶端實(shí)現(xiàn)消息的發(fā)送與讀取,KafkaProducer是用于發(fā)送消息的類,ProducerRecord類用于封裝Kafka的消息,KafkaProducer的實(shí)例化需要指定的參數(shù),Producer的參數(shù)定義在 org.apache.kafka.clients.producer.ProducerConfig類中,需要的朋友可以參考下2023-12-12Java class文件格式之特殊字符串_動力節(jié)點(diǎn)Java學(xué)院整理
特殊字符串出現(xiàn)在class文件中的常量池中,本著循序漸進(jìn)和減少跨度的原則, 首先把class文件中的特殊字符串做一個詳細(xì)的介紹, 然后再回過頭來繼續(xù)講解常量池,對java class 文件格式相關(guān)知識感興趣的的朋友一起學(xué)習(xí)吧2017-06-06Android開發(fā)在輪播圖片上加入點(diǎn)擊事件的方法
這篇文章主要介紹了Android開發(fā)在輪播圖片上加入點(diǎn)擊事件的方法,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11mybatis中實(shí)現(xiàn)讓返回值與bean中字段相匹配
這篇文章主要介紹了mybatis中實(shí)現(xiàn)讓返回值與bean中字段相匹配,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler
這篇文章主要為大家介紹了MyBatisPlus?TypeHandler自定義字段類型轉(zhuǎn)換Handler示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Java多線程中的ThreadPoolExecutor使用解析
這篇文章主要介紹了Java多線程中的ThreadPoolExecutor使用解析,作為線程池的緩沖,當(dāng)新增線程超過maximumPoolSize時,會將新增線程暫時存放到該隊(duì)列中,需要的朋友可以參考下2023-12-12Windows下使用IDEA搭建Hadoop開發(fā)環(huán)境的詳細(xì)方法
這篇文章主要介紹了Windows下使用IDEA搭建Hadoop開發(fā)環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12