使用Java Swing的日历显示(Calendar display using Java Swin

2019-10-18 07:27发布

我工作中的Java Swing(一个独立的应用程序),这将更新每个每当他们登录与他们的用户名和密码的员工考勤的考勤项目。

出席将在一天内拍摄一次。

现在,我想显示他与在标以不同的颜色从记录他所做的不是天的日子(这意味着他不在那些未登录的天数)的日历。

我现在的状态是在链路http://devajkumarsuthapalli.blogspot.in/2013/06/my-java-project_20.html在日历这样http://tinyurl.com/ps23csu

我希望看到登录所用不同的颜色和缺席天的员工用不同的颜色

Answer 1:

你将不得不创建自己的日历组件,这样就可以在天设置为不同的颜色。

下面是我为我的项目之一创建的日历。

这里的MonthPanel产生的日历代码。 现在,它凸显了当前日期。 你可以修改它强调的是员工登录的天数。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MonthPanel extends JPanel {

    private static final long   serialVersionUID    = 1L;

    protected int               month;
    protected int               year;

    protected String[]          monthNames          = { "January", "February",
            "March", "April", "May", "June", "July", "August", "September",
            "October", "November", "December"       };

    protected String[]          dayNames            = { "S", "M", "T", "W",
            "T", "F", "S"                           };

    public MonthPanel(int month, int year) {
        this.month = month;
        this.year = year;

        this.add(createGUI());
    }

    protected JPanel createGUI() {
        JPanel monthPanel = new JPanel(true);
        monthPanel.setBorder(BorderFactory
                .createLineBorder(SystemColor.activeCaption));
        monthPanel.setLayout(new BorderLayout());
        monthPanel.setBackground(Color.WHITE);
        monthPanel.setForeground(Color.BLACK);
        monthPanel.add(createTitleGUI(), BorderLayout.NORTH);
        monthPanel.add(createDaysGUI(), BorderLayout.SOUTH);

        return monthPanel;
    }

    protected JPanel createTitleGUI() {
        JPanel titlePanel = new JPanel(true);
        titlePanel.setBorder(BorderFactory
                .createLineBorder(SystemColor.activeCaption));
        titlePanel.setLayout(new FlowLayout());
        titlePanel.setBackground(Color.WHITE);

        JLabel label = new JLabel(monthNames[month] + " " + year);
        label.setForeground(SystemColor.activeCaption);

        titlePanel.add(label, BorderLayout.CENTER);

        return titlePanel;
    }

    protected JPanel createDaysGUI() {
        JPanel dayPanel = new JPanel(true);
        dayPanel.setLayout(new GridLayout(0, dayNames.length));

        Calendar today = Calendar.getInstance();
        int tMonth = today.get(Calendar.MONTH);
        int tYear = today.get(Calendar.YEAR);
        int tDay = today.get(Calendar.DAY_OF_MONTH);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.DAY_OF_MONTH, 1);

        Calendar iterator = (Calendar) calendar.clone();
        iterator.add(Calendar.DAY_OF_MONTH,
                -(iterator.get(Calendar.DAY_OF_WEEK) - 1));

        Calendar maximum = (Calendar) calendar.clone();
        maximum.add(Calendar.MONTH, +1);

        for (int i = 0; i < dayNames.length; i++) {
            JPanel dPanel = new JPanel(true);
            dPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            JLabel dLabel = new JLabel(dayNames[i]);
            dPanel.add(dLabel);
            dayPanel.add(dPanel);
        }

        int count = 0;
        int limit = dayNames.length * 6;

        while (iterator.getTimeInMillis() < maximum.getTimeInMillis()) {
            int lMonth = iterator.get(Calendar.MONTH);
            int lYear = iterator.get(Calendar.YEAR);

            JPanel dPanel = new JPanel(true);
            dPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            JLabel dayLabel = new JLabel();

            if ((lMonth == month) && (lYear == year)) {
                int lDay = iterator.get(Calendar.DAY_OF_MONTH);
                dayLabel.setText(Integer.toString(lDay));
                if ((tMonth == month) && (tYear == year) && (tDay == lDay)) {
                    dPanel.setBackground(Color.ORANGE);
                } else {
                    dPanel.setBackground(Color.WHITE);
                }
            } else {
                dayLabel.setText(" ");
                dPanel.setBackground(Color.WHITE);
            }
            dPanel.add(dayLabel);
            dayPanel.add(dPanel);
            iterator.add(Calendar.DAY_OF_YEAR, +1);
            count++;
        }

        for (int i = count; i < limit; i++) {
            JPanel dPanel = new JPanel(true);
            dPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            JLabel dayLabel = new JLabel();
            dayLabel.setText(" ");
            dPanel.setBackground(Color.WHITE);
            dPanel.add(dayLabel);
            dayPanel.add(dPanel);
        }

        return dayPanel;
    }

}

这里是我用来产生代码JFrame显示MonthPanel

import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class CalendarFrame implements Runnable {

    private JFrame  frame;

    @Override
    public void run() {
        // Month is zero based
        MonthPanel panel = new MonthPanel(5, 2013);

        frame = new JFrame();
        frame.setTitle("Calendar");
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent event) {
                exitProcedure();
            }
        });

        frame.setLayout(new FlowLayout());
        frame.add(panel);
        frame.pack();
        // frame.setBounds(100, 100, 400, 200);
        frame.setVisible(true);
    }

    public void exitProcedure() {
        frame.dispose();
        System.exit(0);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new CalendarFrame());

    }

}


Answer 2:

所述LGoodDatePicker库包括一个CalendarPanel组件,它允许程序员(可选地)指定一个多色“HighlightPolicy”。 通过使用HighlightPolicy,程序员可以选择背景色,前景色,并为每个高亮显示的日期(可选)提示文本。 (提供的颜色可以是全部相同,或每日期是唯一的。)在您的使用情况,可以使用高亮色彩来表示员工考勤。

公平披露:我的主要开发者。

我粘贴一个CalendarPanel的屏幕截图与多色HighlightPolicy,(和VetoPolicy)。

除了CalendarPanel,图书馆还具有的DatePicker,TimePicker和的DateTimePicker组件。 我包括所有的库组件(和演示应用程序)的屏幕截图。

库可以安装到从项目Java项目发布页面 。

该项目主页是在Github在:
https://github.com/LGoodDatePicker/LGoodDatePicker 。



(点击放大演示图像。)



文章来源: Calendar display using Java Swing