import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.GregorianCalendar; class MyCalendar extends JFrame implements ActionListener { private JPanel p1, p2, p3; //三个面板 p1最上面 p2中间 p3下面 private JLabel yearStr, monthStr; //标签 private JTextField inputYear, inputMonth; private JButton confirm; //确认 private JButton lastMonth; private JButton nextMonth; private JLabel dayText;//文本框 private JLabel TimeText;//文本框 //p2面板里控件的声明 private String[] week = {"日", "一", "二", "三", "四", "五", "六"}; private JLabel[] weekLable = new JLabel[week.length];//数组的声明 //p3面板的42个按钮声明 private JButton[] dayBtn = new JButton[42]; private Calendar nowDate = new GregorianCalendar(); //Calendar是抽象类 new不出 用直接子类 private int nowYear = nowDate.get(Calendar.YEAR); private int nowMonth = nowDate.get(Calendar.MONTH); public MyCalendar() throws HeadlessException { p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); yearStr = new JLabel("Year:"); inputYear = new JTextField(4); monthStr = new JLabel(" Month:"); inputMonth = new JTextField(3); confirm = new JButton("确认"); lastMonth = new JButton("上月"); nextMonth = new JButton("下月"); dayText = new JLabel(); TimeText = new JLabel(); new Thread() { //线程内部类用来实时显示时间 public void run() { while (true) { LocalDateTime dateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); //大写的HH是24小时制的 String nowTime = dateTime.format(formatter); TimeText.setText(nowTime); try { Thread.sleep(1000); } catch (Inter
时间: 2024-04-28 11:22:52 浏览: 136
ruptedException e) { e.printStackTrace(); } } } }.start(); //启动线程 //设置布局管理器 this.setLayout(new BorderLayout()); p1.setLayout(new FlowLayout(FlowLayout.CENTER)); p2.setLayout(new GridLayout(1, 7)); p3.setLayout(new GridLayout(6, 7)); //给p1面板添加控件 p1.add(yearStr); p1.add(inputYear); p1.add(monthStr); p1.add(inputMonth); p1.add(confirm); p1.add(lastMonth); p1.add(nextMonth); p1.add(TimeText); //给p2面板添加控件 for (int i = 0; i < week.length; i++) { weekLable[i] = new JLabel(week[i], SwingConstants.CENTER); p2.add(weekLable[i]); } //给p3面板添加控件 for (int i = 0; i < dayBtn.length; i++) { dayBtn[i] = new JButton(""); dayBtn[i].setContentAreaFilled(false);//将按钮设为透明 p3.add(dayBtn[i]); } confirm.addActionListener(this); lastMonth.addActionListener(this); nextMonth.addActionListener(this); //设置窗口基本属性 this.add(p1, BorderLayout.NORTH); this.add(p2, BorderLayout.CENTER); this.add(p3, BorderLayout.SOUTH); this.setLocation(100, 100); this.setSize(400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); //初始化 init(); } private void init() { nowDate.set(Calendar.DAY_OF_MONTH, 1); showCalendar(); } @Override public void actionPerformed(ActionEvent e) { if (e.getSource() == confirm) { int year = Integer.parseInt(inputYear.getText().trim()); int month = Integer.parseInt(inputMonth.getText().trim()); nowDate.set(Calendar.YEAR, year); nowDate.set(Calendar.MONTH, month - 1); init(); } else if (e.getSource() == lastMonth) { nowDate.add(Calendar.MONTH, -1); init(); } else if (e.getSource() == nextMonth) { nowDate.add(Calendar.MONTH, 1); init(); } else { for (int i = 0; i < dayBtn.length; i++) { if (e.getSource() == dayBtn[i]) { String dayStr = dayBtn[i].getText().trim(); if (!dayStr.equals("")) { JOptionPane.showMessageDialog(null, "您选择的日期是:" + nowYear + "年" + (nowMonth + 1) + "月" + dayStr + "日"); } break; } } } } private void showCalendar() { nowYear = nowDate.get(Calendar.YEAR); nowMonth = nowDate.get(Calendar.MONTH); inputYear.setText(String.valueOf(nowYear)); inputMonth.setText(String.valueOf(nowMonth + 1)); int maxDay = nowDate.getActualMaximum(Calendar.DAY_OF_MONTH);//获取当月最大天数 int weekNo = nowDate.get(Calendar.DAY_OF_WEEK) - 1;//获取当月第一天是星期几 for (int i = 0; i < dayBtn.length; i++) { dayBtn[i].setText(""); } for (int i = weekNo, n = 1; n <= maxDay; i++, n++) { dayBtn[i].setText(String.valueOf(n)); } dayText.setText(nowYear + "年" + (nowMonth + 1) + "月"); } } public class Main { public static void main(String[] args) { new MyCalendar(); } }
这是一个Java GUI程序,是一个日历软件,实现了对日历的显示和时间的实时更新。用户可以选择日期,程序会弹出对话框,显示用户选择的日期。程序的界面比较简单,分为三个面板,p1面板上放置了输入年月和确认按钮,p2面板上放置了星期几,p3面板上放置了日期。程序主要是通过Calendar类和JButton、JLabel等Swing组件实现的。
阅读全文