Java使用圖形用戶(hù)界面萬(wàn)年歷_第1頁(yè)
Java使用圖形用戶(hù)界面萬(wàn)年歷_第2頁(yè)
Java使用圖形用戶(hù)界面萬(wàn)年歷_第3頁(yè)
Java使用圖形用戶(hù)界面萬(wàn)年歷_第4頁(yè)
Java使用圖形用戶(hù)界面萬(wàn)年歷_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、9、萬(wàn)年歷要求:使用圖形用戶(hù)界面。實(shí)現(xiàn)日期與星期的查詢(xún)import java.awt.*;import java.awt.event.*;import java.text.*;import java.util.*;import javax.swing.*;import javax.swing.table.*;public class test1 extends JFrame implements ActionListener private Calendar cal= Calendar.getInstance(); private String str = 星期一, 星期二, 星期三, 星期四

2、, 星期五, 星期六, 星期日; private DefaultTableModel dtm = new DefaultTableModel(null,str); private JTable table = new JTable(dtm); /裝日期的表格 private JScrollPane sp = new JScrollPane(table); private JButton bLastYear = new JButton(上一年); private JButton bNextYear = new JButton(下一年); private JButton bLastMonth =

3、new JButton(上月); private JButton bNextMonth = new JButton(下月); private JTextField Year = new JTextField(4);/jtfYear年份顯示和輸入文本 private JLabel jla=new JLabel(年); private JTextField month = new JTextField(4); private JLabel jla1=new JLabel(月); private JTextField jtfMonth = new JTextField(2);/jtfMonth月份顯

4、示文本框 private JPanel p1 = new JPanel(); /裝入控制日期按鈕的模塊 private JPanel p2 = new JPanel(); private JPanel p3 = new JPanel(new BorderLayout(); private JPanel p4 = new JPanel(new GridLayout(2,1); private JPanel p5 = new JPanel(new BorderLayout(); private JLabel l = new JLabel(文本框中可直接鍵入要查找的年份,以提高查詢(xún)效率); priv

5、ate JLabel lt = new JLabel(); private int lastTime; public test1() super(萬(wàn)年歷); /框架命名 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/窗口關(guān)閉函數(shù) this.getContentPane().setLayout(new BorderLayout(10, 0); table.setGridColor(Color.GRAY); /星期之間的網(wǎng)格線(xiàn)是灰色的 table.setColumnSelectionAllowed(true); table.setSele

6、ctionBackground(Color.BLACK);/當(dāng)選定某一天時(shí)這一天背景黑色 table.setSelectionForeground(Color.GREEN);/選定的日期字體是綠色的 table.setBackground(new Color(184,207,229);/日期顯示表格顏色淺藍(lán)色 table.setFont(new Font(黑體,Font.BOLD,24);/日期數(shù)字字體格式 table.setRowHeight(30);/表格的高度 Year.addActionListener(this);/可輸入年份的文本框 month.addActionListener(

7、this);/可輸入年份的文本框 /為各個(gè)按鈕添加監(jiān)聽(tīng)函數(shù) bLastYear.addActionListener(this); bNextYear.addActionListener(this); bLastMonth.addActionListener(this); bNextMonth.addActionListener(this); /將按鈕添加到Jpanel上 p1.add(bLastYear); p1.add(Year);/年份輸入文本框 p1.add(jla); p1.add(month);/年份輸入文本框 p1.add(jla1); p1.add(bNextYear); p1.

8、add(bLastMonth); p1.add(jtfMonth); p1.add(bNextMonth); p3.add(p2, BorderLayout.SOUTH); p4.add(l); p4.add(lt); p5.add(p4, BorderLayout.SOUTH); p5.add(sp, BorderLayout.CENTER); p5.add(p1, BorderLayout.NORTH); this.getContentPane().add(p5, BorderLayout.CENTER); this.getContentPane().add(p3, BorderLayou

9、t.EAST); String strDate = DateFormat.getDateInstance().format(new Date().split(-);/獲得日期 cal.set(Integer.parseInt(strDate0), Integer.parseInt(strDate1)-1, 0); showCalendar(Integer.parseInt(strDate0), Integer.parseInt(strDate1), cal); jtfMonth.setEditable(false);/設(shè)置月份的文本框?yàn)椴豢删庉?Year.setText(strDate0);

10、month.setText(strDate1); jtfMonth.setText(strDate1); new Timer(lt).start(); this.setBounds(200,200,600,320); this.setResizable(false); this.setVisible(true); public void showCalendar(int localYear, int localMonth, Calendar cld) int Days = getDaysOfMonth(localYear, localMonth) + cld.get(Calendar.DAY_

11、OF_WEEK) - 2; Object ai = new Object7; lastTime = 0; for (int i = cld.get(Calendar.DAY_OF_WEEK)-1; i = Days; i+) aii%7 = String.valueOf(i-(cld.get(Calendar.DAY_OF_WEEK)-2); if (i%7 = 6) dtm.addRow(ai); ai = new Object7; lastTime+; dtm.addRow(ai); public int getDaysOfMonth(int year, int Month) /顯示所選月

12、份的天數(shù) if(Month = 1 | Month = 3 | Month = 5 | Month = 7 | Month = 8 | Month = 10 | Month = 12) return 31; if(Month = 4 | Month = 6 | Month = 9 | Month = 11) return 30; if(year%4 = 0 & year%100 != 0 | year%400 = 0)/閏年 return 29; else return 28; public void actionPerformed(ActionEvent e) if(e.getSource(

13、) = Year | e.getSource() = bLastYear | e.getSource() = bNextYear | e.getSource() = bLastMonth | e.getSource() = bNextMonth) int m, y; try/控制輸入的年份正確,異??刂?if (Year.getText().length() != 4) throw new NumberFormatException(); y = Integer.parseInt(Year.getText(); m = Integer.parseInt(jtfMonth.getText();

14、catch (NumberFormatException ex) JOptionPane.showMessageDialog(this, 請(qǐng)輸入4位0-9的數(shù)字!, 年份有誤, JOptionPane.ERROR_MESSAGE); return; for (int i = 0; i lastTime+1; i+) dtm.removeRow(0); if(e.getSource() = bLastYear) Year.setText(String.valueOf(-y); if(e.getSource() = bNextYear)Year.setText(String.valueOf(+y)

15、; if(e.getSource() = bLastMonth) if(m = 1) Year.setText(String.valueOf(-y); m = 12; jtfMonth.setText(String.valueOf(m); else jtfMonth.setText(String.valueOf(-m); if(e.getSource() = bNextMonth) if(m = 12) Year.setText(String.valueOf(+y); m = 1; jtfMonth.setText(String.valueOf(m); else jtfMonth.setTex

16、t(String.valueOf(+m); cal.set(y, m-1, 0); showCalendar(y, m, cal); month.setText(String.valueOf(m); public static void main(String args) JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); new test1(); class Timer extends Thread/顯示系統(tǒng)時(shí)間 private JLabel lt; private SimpleDateFormat fy = new SimpleDateFormat(yyyy.MM.dd G at HH:mm:ss z); private SimpleDateFormat fn = new SimpleDateFormat(yyyy.MM.dd G at HH mm ss z); pr

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論