java使用图形用户界面万年历,java实现万年历图形界面
本文实例为大家分享了爪哇实现图形化界面日历的具体代码,供大家参考,具体内容如下
此程序主要功能实现了可以根据用户选择的年月日来定位日期,日期的旁边用#加以标注
主界面如下:
代码如下:
导入Java。awt。*;导入Java。awt。事件。动作事件;导入Java。awt。事件。动作监听器;导入Java。文字。简单的日期格式;导入Java。util。日历;导入Java。util。日期;导入javax。挥棒。*;类awt { JFrame JF=new JFrame();集装箱港口=JF。getcontentpane();JPanel p _ mid=new JPanel();JPanel pTime=new JPanel();JPanel ptime 2=new JPanel();JComboBoxString box 1=new JComboBoxString();JComboBoxString box 2=new JComboBoxString();JComboBoxString box 3=new JComboBoxString();字符串年、月、日;//记录年、月、日日历ca=日历。getinstance();公共awt() { //基本设置JF。设置可见(真);jf.setLocation(300,300);setDefaultCloseOperation(jf .EXIT _ ON _ CLOSE);JF。set layout(new BorderLayout());JF。setresizable(false);下午三点。设置布局(new GridLayout(7,7,1,1));//调用方法setTime();week();day();//自适应窗口大小JF。pack();} //下拉列表框监听器公共类行动1实现操作侦听器{ JComboBoxString bool@ Override public void action performed(action event e){ bool=(JComboBoxString)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=12I){ str[I]=integer。tostring(I);}方框2。set model(new DefaultComboBoxModelString(str));} } }类行动2实现操作侦听器{ JComboBoxString boolstring str[];@ Override public void action performed(action event e){ bool=(JComboBoxString)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) { //进入此循环,则为闰年,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 { //进入此循环,则为平年,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) { //实现方法 if(!year.equals("--年份--") && !month.equals("--月份--") && !day.equals("--日期--")) { //移除组件 con.remove(p_mid); p_mid.removeAll(); //实现方法 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); } //新的天数组件 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); //当前月有几天 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++) { 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(); }}实现过程中碰到的主要问题有:
1、关于JComBobox组件的更新问题,开始是直接修改值和将组件移除后重新添加,发现都行不通,在查看了JComBobox的源码之后发现底层是调用setModel方法来实现添加元素的,于是我将使用此方法来更新下拉列表框的值,成功解决此问题
box.setModel(new DefaultComboBoxModel<String>(str));
2、将按钮移除重新添加后必须将界面框最小化之后打开才能出现界面,查阅资料后发现Container有一个validate()方法可以用来重绘,于是便添加此方法,成功解决问题
con.validate();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。