| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1958 人关注过本帖
标题:自己写的一个简单日历
只看楼主 加入收藏
hwpayg
Rank: 2
等 级:论坛游民
帖 子:108
专家分:25
注 册:2008-4-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:12 
自己写的一个简单日历
主要是用了JList和JTable,欢迎大家下载,提出意见,如果直接运行jar文件会显得不那么美观,因为图片显示不出来,另外自己的GridBagLayout布局也没学到多少,所以显得排版不这么好。

calendarTest.rar (13.53 KB)
搜索更多相关主题的帖子: 日历 
2009-07-31 16:22
hwpayg
Rank: 2
等 级:论坛游民
帖 子:108
专家分:25
注 册:2008-4-3
收藏
得分:0 
真是不好意思,忘记将源代码加入到jar文件,现在重新上传带源代码的附件
另外发现这个日历不能显示当天是几号,由于对JTable还不是很懂,所以希望有人给个提示或帮助

[[it] 本帖最后由 hwpayg 于 2009-8-2 09:58 编辑 [/it]]

CalendarPro.rar (16.86 KB)
2009-08-01 13:55
家乡的雨
Rank: 2
等 级:论坛游民
帖 子:25
专家分:44
注 册:2009-1-12
收藏
得分:0 
谢谢楼主分享!
2009-08-02 09:52
hwpayg
Rank: 2
等 级:论坛游民
帖 子:108
专家分:25
注 册:2008-4-3
收藏
得分:0 
下面是能够显示单天日期的完整源代码:有兴趣的朋友可以看下
import java.awt.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

//这是一个用JList和JTable完成的简单日历
public class CalendarTest
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new CalendarFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class CalendarFrame extends JFrame
{
    private static final long serialVersionUID = 8067844586793608064L;
    JPanel listPanel; // 显示月份的列表面板
    JPanel tablePanel; // 显示日期的表格面板
    JPanel textPanel; // 文本面板
    JMenuBar menuBar; // JMenuBar,JMenu定义的变量都没有实现功能
    JMenu fileMenu;
    JMenu editMenu;
    JMenu formatMenu;
    JMenu lookMenu;

    JMenu helpMenu;

    JEditorPane text; // 自带复制,剪切和删除快捷键功能的面板
    JScrollPane textscrollpane;
    JScrollPane tablescrollpane;
    JList list; // 显示月份的列表
    JTable table; // 显示日期的表格
    JPanel yearPanel; // 显示年份的面板
    JSpinner spinner; // JSpinner空间
    public static boolean flag = true;

    public CalendarFrame()
    {
        table = new JTable(new MyTableModel());
        tablescrollpane = new JScrollPane(table);
        for (int i = 0; i < 7; i++)
        {
            TableColumn column = table.getColumnModel().getColumn(i);// 获取表格每一列
            column.setResizable(false);// 不允许改变列的大小
        }

        table.setBackground(Color.GREEN);
        table.setRowHeight(72); // 设定每列的大小
        table.setRowSelectionAllowed(false);// 设定行的选择模式
        table.setDefaultRenderer(String.class, new MyTableRenderer());

        list = new JList(new AbstractListModel() // 匿名的list模型类,提供list显示的月份数据
                {
                    private static final long serialVersionUID = -3703164069291737586L;

                    public Object getElementAt(int index)
                    {
                        String obj = null;
                        switch (index)
                        {
                        case 0:
                            obj = "Jan";
                            break;
                        case 1:
                            obj = "Feb";
                            break;
                        case 2:
                            obj = "Mar";
                            break;
                        case 3:
                            obj = "Apr";
                            break;
                        case 4:
                            obj = "May";
                            break;
                        case 5:
                            obj = "Jun";
                            break;
                        case 6:
                            obj = "Jul";
                            break;
                        case 7:
                            obj = "Aug";
                            break;
                        case 8:
                            obj = "Sep";
                            break;
                        case 9:
                            obj = "Oct";
                            break;
                        case 10:
                            obj = "Nov";
                            break;
                        case 11:
                            obj = "Dec";
                            break;
                        }
                        return obj;
                    }

                    @Override
                    public int getSize()
                    {
                        return 12;
                    }

                });
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 设定列表的选择模式
        list.setCellRenderer(new MyListCellRenderer()); // 列表元素的绘制类MyListCellRenderer
        list.setSelectedIndex(Calendar.getInstance().get(Calendar.MONTH));// JList默认的月份为现实的月份
        list.addListSelectionListener(new ListSelectionListener()// 当某个列表被选中时,更新日期table控件
                {
                    @Override
                    public void valueChanged(ListSelectionEvent e)
                    {
                        flag = true;
                        table.updateUI();
                    }

                });
        listPanel = new JPanel();
        listPanel.setLayout(new BorderLayout());
        listPanel.add(list);

        tablePanel = new JPanel();
        tablePanel.setBackground(Color.white);
        tablePanel.setLayout(new BorderLayout());
        tablePanel.add(tablescrollpane);
        // table.setBackground(Color.green);

        yearPanel = new JPanel();// 将label控件和JSpinner控件加入年份面板上
        yearPanel.setLayout(new GridLayout(0, 3));
        JLabel gongyuan = new JLabel("公元", JLabel.RIGHT);
        JLabel year = new JLabel("年");
        spinner = new JSpinner(new SpinnerNumberModel(Calendar.getInstance()
                .get(Calendar.YEAR), null, null, 1));
        spinner.addChangeListener(new ChangeListener() // 为JSpinner空间添加事件监听器
                {

                    @Override
                    public void stateChanged(ChangeEvent e)
                    {
                        flag = true;
                        table.updateUI();
                    }

                });
        yearPanel.add(gongyuan);
        yearPanel.add(spinner);
        yearPanel.add(year);
        tablePanel.add(yearPanel, BorderLayout.NORTH);// 将年份面板加入到表格面板的北部

        textPanel = new JPanel();
        textPanel.setLayout(new BorderLayout());
        text = new JEditorPane();
        textscrollpane = new JScrollPane(text);
        textPanel.add(textscrollpane);
        text.setFont(new Font("SansSerif", Font.PLAIN, 40));
        tablePanel.add(textPanel, BorderLayout.SOUTH);
        menuBar = new JMenuBar();// JMenu定义的空间都没实现功能
        fileMenu = new JMenu("文件");
        editMenu = new JMenu("编辑");
        formatMenu = new JMenu("格式");
        lookMenu = new JMenu("查看");

        helpMenu = new JMenu("帮助");
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(formatMenu);
        menuBar.add(lookMenu);
        menuBar.add(helpMenu);

        GridBagLayout layout = new GridBagLayout(); // 设成GridBagLayout布局
        GridBagConstraints constraints = new GridBagConstraints();
        this.setFont(new Font("SansSerif", Font.PLAIN, 14));
        this.setLayout(layout);
        this.setTitle("万年历"); // 设定标题
        this.setIconImage(new ImageIcon("res\\title.jpg").getImage()); // 设定图标
        constraints.fill = GridBagConstraints.BOTH;

        constraints.weightx = 0.5;
        constraints.weighty = 0.5;
        layout.setConstraints(listPanel, constraints);
        constraints.weightx = 8.0;
        constraints.weighty = 0.5;
        layout.setConstraints(tablePanel, constraints);

        this.setJMenuBar(menuBar);
        this.add(listPanel);
        this.add(tablePanel);
        this.setSize(this.getToolkit().getScreenSize().width * 3 / 4, this
                .getToolkit().getScreenSize().height * 3 / 4); // 设定窗体的大小
    }

    class MyTableModel extends AbstractTableModel // 表格模型类,提供表格数据
    {
        private static final long serialVersionUID = 1L;
        String[] columnName = new String[]
        // 表格列名
        { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
                "Saturday" };

        @Override
        public int getColumnCount() // 返回表格列数
        {
            return columnName.length;
        }

        public int getRowCount() // 返回表格行数
        {
            return 6;
        }

        public String getColumnName(int col) // 获得表格列名
        {
            return columnName[col];
        }

        public Class<? extends Object> getColumnClass(int c)
        {
            return getValueAt(0, c).getClass();
        }

        public Object getValueAt(int rowIndex, int columnIndex)
        {
            int month = list.getSelectedIndex();// 获得列表的月份
            int year = (Integer) spinner.getValue();// 获得spinner显示的年份
            GregorianCalendar gc = new GregorianCalendar(year, month, 1);
            int dayOfWeek = gc.get(gc.DAY_OF_WEEK) - 1; // 获得这个月的第一天是星期几
            int countDayInMonth = 0;// 某月的总天数,这里的月是从0-11,表示1-12月
            if (month == 0 || month == 2 || month == 4 || month == 6
                    || month == 7 || month == 9 || month == 11)
            {
                countDayInMonth = 31; // 是31天的月份
            } else if (month == 3 || month == 5 || month == 8 || month == 10)
            {
                countDayInMonth = 30; // 是30天的月份
            } else if (gc.isLeapYear(year) && month == 1)
            {
                countDayInMonth = 29; // 闰年2月
            } else if (!gc.isLeapYear(year) && month == 1)
            {
                countDayInMonth = 28; // 非闰年2月
            }
            String value = ""; // 因为返回值是Object类型,所以不能直接返回int类型的数据,所以转换成String类型
            if (rowIndex == 0) // 第一列的数据
            {
                if (columnIndex < dayOfWeek)
                {
                    value = "";
                } else
                {
                    value = "" + (columnIndex - dayOfWeek + 1);
                }
            } else
            // 其余列的数据
            {
                if (((rowIndex - 1) * 7) + (columnIndex + 1 + 7 - dayOfWeek) <= countDayInMonth)
                    value = (((rowIndex - 1) * 7) + (columnIndex + 1 + 7 - dayOfWeek))
                            + "";
            }
            return value;
        }
    }

    class MyListCellRenderer extends JLabel implements ListCellRenderer // 列表元素绘制类继承自JLabel,实现ListCellRenderer接口
    {
        private static final long serialVersionUID = 1L;

        public Component getListCellRendererComponent(JList list, // the list
                Object value, // value to display
                int index, // cell index
                boolean isSelected, // is the cell selected
                boolean cellHasFocus) // does the cell have focus
        {
            String s = value.toString().trim(); // 获取选中元素的字符内容
            setText(s); // 显示这个字符
            Dimension dimension = list.getSize(); // 设定这个每个元素的大小
            int height = dimension.height / 12;
            int width = dimension.width;
            setSize(width, height);
            if (isSelected) // 如果该元素被选中,则背景色为红色
            {
                setBackground(Color.red);
                setForeground(list.getSelectionBackground());
            } else
            // 未被选中的元素背景色显示为灰色,前景色为粉红色
            {
                setBackground(Color.gray);
                setForeground(Color.pink);
            }
            setIcon(new ImageIcon("res\\mon.jpg")); // 设定该元素的图标
            setEnabled(list.isEnabled());
            setFont(new Font("SansSerif", Font.PLAIN, 20)); // 设定字体大小
            setOpaque(true);
            return this;
        }
    }

    class MyTableRenderer extends JLabel implements TableCellRenderer
    {
        private static final long serialVersionUID = 1L;

        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column)
        {
            if ((Calendar.getInstance().get(Calendar.DAY_OF_MONTH) + "") //如果这个标签上的值等于该天的值
                    .equals(value)
                    && flag == true  //标记为true
                    && list.getSelectedIndex() == Calendar.getInstance().get(
                            Calendar.MONTH)  //列表显示的月份是该月的值
                    && (Integer) spinner.getValue() == Calendar.getInstance()
                            .get(Calendar.YEAR))//spinner显示的年份是该年
            {
                setFont(new Font("SansSerif", Font.PLAIN, 40)); // 设定字体大小
                setText((String) value);  //显示日期字符串
                setBorder(UIManager.getBorder("Table.focusCellHighlightBorder"));
                setForeground(Color.RED);  //设定字符串颜色
                flag = false;
            } else  //如果不是改日的标签上的值
            {
                setFont(new Font("SansSerif", Font.PLAIN, 40)); // 设定字体大小
                setText((String) value);  //显示标签上的日期字符串
                if (hasFocus)   //如果是被点中的表格的单元格
                {
                    setBorder(UIManager
                            .getBorder("Table.focusCellHighlightBorder"));  //设定边框
                    setForeground(Color.RED);//设定日期字符串颜色
                } else
                {
                    setForeground(null);
                    setBorder(null);
                }
            }
            return this;
        }
    }
}
2009-08-02 13:54
usbboy2009
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:39
专家分:183
注 册:2009-7-31
收藏
得分:20 
int dayOfWeek = gc.get(gc.DAY_OF_WEEK) - 1 提示warning改为静态方法好些吧
int dayOfWeek = gc.get(Calendar.DAY_OF_WEEK) - 1; // 获得这个月的第一天是星期几
2009-08-02 15:15
hwpayg
Rank: 2
等 级:论坛游民
帖 子:108
专家分:25
注 册:2008-4-3
收藏
得分:0 
是哦,不是你说我还不知道为什么会有个警告在上面,非常感谢
2009-08-03 10:36
紫衫
Rank: 4
来 自:风之谷
等 级:业余侠客
帖 子:126
专家分:211
注 册:2009-6-6
收藏
得分:0 
网上有很多日历控件,不过是用js写的
2009-08-03 12:40
hwpayg
Rank: 2
等 级:论坛游民
帖 子:108
专家分:25
注 册:2008-4-3
收藏
得分:0 
刚看过Java核心技术的高级swing,所以使用下JTable,JList等熟练下
2009-08-04 07:12
xyc_lemon
Rank: 1
来 自:重庆
等 级:新手上路
帖 子:185
专家分:0
注 册:2007-12-22
收藏
得分:0 
看过了。。对于我而言这个还是很有难度的。。谢谢分享···

[url=http://www./bbs/down.asp?html=863986]
2009-08-05 06:10
vdestroyer
Rank: 2
等 级:论坛游民
帖 子:136
专家分:14
注 册:2009-1-7
收藏
得分:0 
我也还达不到lz的水平。。。希望以后能达到吧。。

问一下,怎么把写好的程序作成一个jar文件?
要运行.jar文件,需要些什么?没有开发环境和编译器能运行jar吗?
主要是,我想把我写的一个程序,发给一个没有装任何开发环境和编译器的人玩玩
2009-08-08 03:13
快速回复:自己写的一个简单日历
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.080207 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved