Java實現(xiàn)圖形化界面的日歷
本文實例為大家分享了Java實現(xiàn)圖形化界面日歷的具體代碼,供大家參考,具體內(nèi)容如下
此程序主要功能實現(xiàn)了可以根據(jù)用戶選擇的年月日來定位日期,日期的旁邊用#加以標(biāo)注
主界面如下:
代碼如下:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import javax.swing.*; class awt{ ?? ?JFrame jf = new JFrame(); ?? ?Container con = jf.getContentPane(); ?? ?JPanel p_mid = new JPanel(); ?? ?JPanel pTime = new JPanel(); ?? ?JPanel pTime2 = new JPanel(); ?? ?JComboBox<String> box1 = new JComboBox<String>(); ?? ?JComboBox<String> box2 = new JComboBox<String>(); ?? ?JComboBox<String> box3 = new JComboBox<String>(); ?? ?String year, month, day; ? ? ? ? ? ? ? ? ? ? ? ? ? ?//記錄年、月、日 ?? ?Calendar ca = Calendar.getInstance(); ?? ?public awt() { ?? ??? ?//基本設(shè)置 ?? ??? ?jf.setVisible(true); ?? ??? ?jf.setLocation(300, 300); ?? ??? ?jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE); ?? ??? ?jf.setLayout(new BorderLayout()); ?? ??? ?jf.setResizable(false); ?? ??? ?p_mid.setLayout(new GridLayout(7, 7, 1, 1)); ?? ??? ?//調(diào)用方法 ?? ??? ?setTime(); ?? ??? ?week(); ?? ??? ?Day(); ?? ??? ?//自適應(yīng)窗口大小 ?? ??? ?jf.pack(); ?? ?} ?? ? ?? ?//下拉列表框監(jiān)聽器 ?? ?public class action1 implements ActionListener { ?? ??? ?JComboBox<String> bool; ?? ??? ?@Override ?? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ?bool = (JComboBox<String>)e.getSource(); ?? ??? ??? ?year = (String)bool.getSelectedItem(); ?? ??? ??? ?combox(); ?? ??? ?} ?? ??? ?public void combox() { ?? ??? ??? ?if(!year.equals("--年份--")) { ?? ??? ??? ??? ?String str[] = new String[13]; ?? ??? ??? ??? ?str[0] = "--月份--"; ?? ??? ??? ??? ?for(int i=1; i<=12; i++) { ?? ??? ??? ??? ??? ?str[i] = Integer.toString(i); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?box2.setModel(new DefaultComboBoxModel<String>(str)); ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?class action2 implements ActionListener{ ?? ??? ?JComboBox<String> bool; ?? ??? ?String str[]; ?? ??? ?@Override ?? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ?bool = (JComboBox<String>)e.getSource(); ?? ??? ??? ?month = (String)bool.getSelectedItem(); ?? ??? ??? ?combox(); ?? ??? ?} ?? ??? ?public void combox() { ?? ??? ??? ?if(!month.equals("--月份--") && (month.matches("[13578]") || month.equals("10") || month.equals("12"))) { ?? ??? ??? ??? ?str = new String[32]; ?? ??? ??? ??? ?str[0] = "--日期--"; ?? ??? ??? ??? ?for(int i=1; i<=31; i++) { ?? ??? ??? ??? ??? ?str[i] = Integer.toString(i); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?box3.setModel(new DefaultComboBoxModel<String>(str)); ?? ??? ??? ?}else if(!month.equals("--月份--") && (month.matches("[469]") || month.equals("11"))){ ?? ??? ??? ??? ?str = new String[31]; ?? ??? ??? ??? ?str[0] = "--日期--"; ?? ??? ??? ??? ?for(int i=1; i<=30; i++) { ?? ??? ??? ??? ??? ?str[i] = Integer.toString(i); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?box3.setModel(new DefaultComboBoxModel<String>(str)); ?? ??? ??? ?}else if(!month.equals("--月份--") && month.equals("2")) { ?? ??? ??? ??? ?//判斷為閏年還是平年 ?? ??? ??? ??? ?if(Integer.parseInt(year)%4==0 && Integer.parseInt(year)%100!=0) { ?? ??? ??? ??? ??? ?//進入此循環(huán),則為閏年,2月份有29天 ?? ??? ??? ??? ??? ?str = new String[30]; ?? ??? ??? ??? ??? ?str[0] = "--日期--"; ?? ??? ??? ??? ??? ?for(int i=1; i<=29; i++) { ?? ??? ??? ??? ??? ??? ?str[i] = Integer.toString(i); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?box3.setModel(new DefaultComboBoxModel<String>(str)); ?? ??? ??? ??? ?}else { ?? ??? ??? ??? ??? ?//進入此循環(huán),則為平年,2月份有28天 ?? ??? ??? ??? ??? ?str = new String[29]; ?? ??? ??? ??? ??? ?str[0] = "--日期--"; ?? ??? ??? ??? ??? ?for(int i=1; i<=28; i++) { ?? ??? ??? ??? ??? ??? ?str[i] = Integer.toString(i); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?box3.setModel(new DefaultComboBoxModel<String>(str)); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?}? ?? ?class action3 implements ActionListener{ ?? ??? ?JComboBox<String> bool; ?? ??? ?@Override ?? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ?bool = (JComboBox<String>)e.getSource(); ?? ??? ??? ?day = (String)bool.getSelectedItem(); ?? ??? ?} ?? ??? ? ?? ?} ?? ?class action4 extends JComponent implements ActionListener{ ?? ??? ?@Override ?? ??? ?public void actionPerformed(ActionEvent e) { ?? ??? ??? ?//實現(xiàn)方法 ?? ??? ??? ?if(!year.equals("--年份--") && !month.equals("--月份--") && !day.equals("--日期--")) { ?? ??? ??? ??? ?//移除組件 ?? ??? ??? ??? ?con.remove(p_mid); ?? ??? ??? ??? ?p_mid.removeAll(); ?? ??? ??? ??? ?//實現(xiàn)方法 ?? ??? ??? ??? ?Week_new(); ?? ??? ??? ??? ?Day_new(); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?//新的星期組件 ?? ??? ?public void Week_new() { ?? ??? ??? ?JButton lab1 = new JButton("一"); ?? ??? ??? ?JButton lab2 = new JButton("二"); ?? ??? ??? ?JButton lab3 = new JButton("三"); ?? ??? ??? ?JButton lab4 = new JButton("四"); ?? ??? ??? ?JButton lab5 = new JButton("五"); ?? ??? ??? ?JButton lab6 = new JButton("六"); ?? ??? ??? ?JButton lab7 = new JButton("七"); ?? ??? ??? ?lab1.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ??? ?lab2.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ??? ?lab3.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ??? ?lab4.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ??? ?lab5.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ??? ?lab6.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ??? ?lab7.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ??? ?p_mid.add(lab1); ?? ??? ??? ?p_mid.add(lab2); ?? ??? ??? ?p_mid.add(lab3); ?? ??? ??? ?p_mid.add(lab4); ?? ??? ??? ?p_mid.add(lab5); ?? ??? ??? ?p_mid.add(lab6); ?? ??? ??? ?p_mid.add(lab7); ?? ??? ?} ?? ??? ?//新的天數(shù)組件 ?? ??? ?public void Day_new() { ?? ??? ??? ?int temp=1; ?? ??? ??? ?ca.set(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day)); ?? ??? ??? ?int Month_max = ca.getActualMaximum(Calendar.DATE); ?? ??? ??? ?int day = ca.get(Calendar.DATE); ?? ??? ??? ?ca.set(Calendar.DATE, 1); ?? ??? ??? ?int Week = ca.get(Calendar.DAY_OF_WEEK)-1; ?? ??? ??? ?if(Week == 0) { ?? ??? ??? ??? ?Week = 7; ?? ??? ??? ?} ?? ??? ??? ?for(int i=1; i<=42; i++) { ?? ??? ??? ??? ?if(i>=Week && i<Week+Month_max) { ?? ??? ??? ??? ??? ?if(temp == day) { ?? ??? ??? ??? ??? ??? ?p_mid.add(new Button("#"+temp)); ?? ??? ??? ??? ??? ??? ?temp++; ?? ??? ??? ??? ??? ??? ?continue; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?p_mid.add(new Button(Integer.toString(temp))); ?? ??? ??? ??? ??? ?temp++; ?? ??? ??? ??? ??? ?continue; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?p_mid.add(new Button()); ?? ??? ??? ?} ?? ??? ??? ?con.add(p_mid, BorderLayout.CENTER); ?? ??? ??? ?con.validate(); ?? ??? ?} ?? ?} ?? ?//------// ?? ?public void setTime() { ?? ??? ?//聲明變量 ?? ??? ?JLabel labTime = new JLabel(); ?? ??? ?JButton button = new JButton("查詢"); ?? ??? ?box1.addActionListener(new action1()); ?? ??? ?box2.addActionListener(new action2()); ?? ??? ?box3.addActionListener(new action3()); ?? ??? ?button.addActionListener(new action4()); ?? ??? ?Date d = new Date(); ?? ??? ?SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd"); ?? ??? ?String time = sdf.format(d); ?? ??? ?labTime.setText("北京時間:"+time); ?? ??? ?labTime.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ?box1.addItem("--年份--"); ?? ??? ?box1.addItem("2016"); ?? ??? ?box1.addItem("2017"); ?? ??? ?box1.addItem("2018"); ?? ??? ?box1.addItem("2019"); ?? ??? ?box2.addItem("--月份--"); ?? ??? ?box3.addItem("--日期--"); ?? ??? ?pTime.setLayout(new FlowLayout(FlowLayout.LEFT,20,5)); ?? ??? ?pTime2.setLayout(new FlowLayout(FlowLayout.CENTER,1,0)); ?? ??? ?pTime.add(labTime); ?? ??? ?pTime2.add(box1); ?? ??? ?pTime2.add(box2); ?? ??? ?pTime2.add(box3); ?? ??? ?pTime2.add(button); ?? ??? ?pTime.add(pTime2); ?? ??? ?con.add(pTime, BorderLayout.NORTH); ?? ?} ?? ?public void week() { ?? ??? ?JButton lab1 = new JButton("一"); ?? ??? ?JButton lab2 = new JButton("二"); ?? ??? ?JButton lab3 = new JButton("三"); ?? ??? ?JButton lab4 = new JButton("四"); ?? ??? ?JButton lab5 = new JButton("五"); ?? ??? ?JButton lab6 = new JButton("六"); ?? ??? ?JButton lab7 = new JButton("七"); ?? ??? ?lab1.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ?lab2.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ?lab3.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ?lab4.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ?lab5.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ?lab6.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ?lab7.setFont(new Font("微軟雅黑", Font.BOLD, 15)); ?? ??? ?p_mid.add(lab1); ?? ??? ?p_mid.add(lab2); ?? ??? ?p_mid.add(lab3); ?? ??? ?p_mid.add(lab4); ?? ??? ?p_mid.add(lab5); ?? ??? ?p_mid.add(lab6); ?? ??? ?p_mid.add(lab7); ?? ?} ?? ?public void Day() { ?? ??? ?int temp=1; ?? ??? ?int Month_max = ca.getActualMaximum(Calendar.DATE); ? ?//當(dāng)前月有幾天 ?? ??? ?int day = ca.get(Calendar.DATE); ? ? ? ? ? ? ? ? ? ? ? //當(dāng)前日期 ?? ??? ?ca.set(Calendar.DATE, 1); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ??? ?int Week = ca.get(Calendar.DAY_OF_WEEK)-1; ? ? ? ? ? ? //星期幾 ?? ??? ?if(Week == 0) { ?? ??? ??? ?Week = 7; ?? ??? ?} ?? ??? ?for(int i=1; i<=42; i++) { ?? ??? ??? ?Button b = new Button(); ?? ??? ??? ?if(i>=Week && i<Week+Month_max) { ?? ??? ??? ??? ?if(temp == day) { ?? ??? ??? ??? ??? ?p_mid.add(new Button("#"+temp)); ?? ??? ??? ??? ??? ?temp++; ?? ??? ??? ??? ??? ?continue; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?p_mid.add(new Button(Integer.toString(temp))); ?? ??? ??? ??? ?temp++; ?? ??? ??? ??? ?continue; ?? ??? ??? ?} ?? ??? ??? ?p_mid.add(new Button()); ?? ??? ?} ?? ??? ?con.add(p_mid, BorderLayout.CENTER); ?? ?} } public class application { ?? ?public static void main(String[] args) { ?? ??? ?awt at = new awt(); ?? ?} }
實現(xiàn)過程中碰到的主要問題有:
1、關(guān)于JComBobox組件的更新問題,開始是直接修改值和將組件移除后重新添加,發(fā)現(xiàn)都行不通,在查看了JComBobox的源碼之后發(fā)現(xiàn)底層是調(diào)用setModel方法來實現(xiàn)添加元素的,于是我將使用此方法來更新下拉列表框的值,成功解決此問題
box.setModel(new DefaultComboBoxModel<String>(str));
2、將按鈕移除重新添加后必須將界面框最小化之后打開才能出現(xiàn)界面,查閱資料后發(fā)現(xiàn)Container有一個validate()方法可以用來重繪,于是便添加此方法,成功解決問題
con.validate();
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
bool當(dāng)成函數(shù)參數(shù)錯誤理解
經(jīng)常會在函數(shù)的參數(shù)里使用bool參數(shù),這會大大地降低代碼的可讀性2012-11-11maven <repositories>標(biāo)簽和<pluginRepositories>標(biāo)簽的使用
這篇文章主要介紹了maven <repositories>標(biāo)簽和<pluginRepositories>標(biāo)簽的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java interrupt()方法使用注意_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java interrupt()方法使用注意_動力節(jié)點Java學(xué)院整理,需要的朋友可以參考下2017-05-05