使用 Java Swing 显示日历

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17232038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:23:58  来源:igfitidea点击:

Calendar display using Java Swing

javaswingdatecalendar

提问by Devaj Kumar Suthapalli

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.htmlin 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

我想看到员工以不同的颜色登录,而缺勤的日期则以不同的颜色

回答by Gilbert Le Blanc

You're going to have to create your own Calendar component so that you can set the days to different colors.

您将不得不创建自己的日历组件,以便可以将日期设置为不同的颜色。

Here's a calendar I created for one of my projects.

这是我为我的一个项目创建的日历。

Calendar

日历

Here's the MonthPanelcode that produces the calendar. Right now, it highlights the current day. You can modify it to highlight the days that your employees log in.

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

}

And here's the code I used to produce the JFrameto 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());

    }

}

回答by BlakeTNC

The LGoodDatePickerlibrary includes a CalendarPanelcomponent, which allows the programmer to (optionally) specify a multicolor "HighlightPolicy". By using a HighlightPolicy, the programmer can choose a background color, a foreground color, and (optional) tooltip text for each highlighted date. (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.

所述LGoodDatePicker库包括一个CalendarPanel组件,它允许程序员(可选地)指定一个多色“HighlightPolicy”。通过使用 HighlightPolicy,程序员可以为每个突出显示的日期选择背景颜色、前景色和(可选)工具提示文本。(提供的颜色可以完全相同,也可以每个日期唯一。)在您的使用中,突出显示颜色可用于指示员工出勤情况。

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. I included screenshots of all the library components (and the demo application).

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

The library can be installed into your Java project from the project release page.

该库可以从项目发布页面安装到您的 Java 项目中。

The project home page is on Github at:
https://github.com/LGoodDatePicker/LGoodDatePicker.

项目主页位于 Github 上:
https: //github.com/LGoodDatePicker/LGoodDatePicker


Highlight Policy Example


突出策略示例


DateTimePicker screenshot


日期时间选择器截图

(Click to zoom the demo image.) Demo screenshot

(单击放大演示图像。) 演示截图