简体   繁体   English

使用Java Swing的日历显示

[英]Calendar display using Java Swing

I am working on an attendance project in java swing (a stand alone application), which updates attendance of each employee whenever they log in with their user id and password. 我正在使用java swing(一个独立的应用程序)中的一个出勤项目,该项目在每位员工使用其用户ID和密码登录时都会更新其出勤。

Attendance will be taken only once in a day. 每天仅参加一次。

Now, I want to display a calendar with the days he logged in marked a different color from the days he didn't (meaning he was absent those un-logged-in days). 现在,我想显示一个日历,其中他登录的日子与未登录的日子标记为不同的颜色(这意味着他缺席那些未登录的日子)。

My current status is in the link http://devajkumarsuthapalli.blogspot.in/2013/06/my-java-project_20.html in a calendar like this http://tinyurl.com/ps23csu 我当前的状态是在日历中的链接http://devajkumarsuthapalli.blogspot.in/2013/06/my-java-project_20.html ,例如http://tinyurl.com/ps23csu

I want to see employees logged in days with different color and absentees with a different color 我想看到以不同颜色登录的员工和以不同颜色登录的缺勤员工

You're going to have to create your own Calendar component so that you can set the days to different colors. 您将必须创建自己的Calendar组件,以便可以将日期设置为不同的颜色。

Here's a calendar I created for one of my projects. 这是我为我的一个项目创建的日历。

日历

Here's the MonthPanel code that produces the calendar. 这是产生日历的MonthPanel代码。 Right now, it highlights the current day. 现在,它突出显示了当前日期。 You can modify it to highlight the days that your employees log in. 您可以对其进行修改以突出显示您的员工登录的日期。

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;
    }

}

And here's the code I used to produce the JFrame to display the MonthPanel . 这是我用来生成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());

    }

}

The LGoodDatePicker library includes a CalendarPanel component, which allows the programmer to (optionally) specify a multicolor "HighlightPolicy". LGoodDatePicker库包含CalendarPanel组件,该组件允许程序员(可选)指定多色“ HighlightPolicy”。 By using a HighlightPolicy, the programmer can choose a background color, a foreground color, and (optional) tooltip text for each highlighted date. 通过使用HighlightPolicy,程序员可以为每个突出显示的日期选择背景色,前景色和(可选)工具提示文本。 (The supplied colors can be all the same, or unique per date.) In your usage, the highlight colors could be used to indicate employee attendance. (提供的颜色可以全部相同,或每个日期唯一。)在您使用时,突出显示的颜色可以用于指示员工出勤。

Fair disclosure: I'm the primary developer. 公开披露:我是主要开发商。

I pasted a screenshot of a CalendarPanel with a multicolor HighlightPolicy, (and a VetoPolicy). 我粘贴了带有多色HighlightPolicy(和VetoPolicy)的CalendarPanel屏幕截图。

Besides the CalendarPanel, the library also has the DatePicker, TimePicker, and DateTimePicker components. 除了CalendarPanel之外,该库还具有DatePicker,TimePicker和DateTimePicker组件。 I included screenshots of all the library components (and the demo application). 我提供了所有库组件(和演示应用程序)的屏幕截图。

The library can be installed into your Java project from the project release page . 该库可以从项目发布页面安装到Java项目中。

The project home page is on Github at: 该项目的主页位于Github上,网址为:
https://github.com/LGoodDatePicker/LGoodDatePicker . https://github.com/LGoodDatePicker/LGoodDatePicker


突出显示策略示例


DateTimePicker屏幕截图

(Click to zoom the demo image.) (单击以放大演示图像。) 演示截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM