Java 日期和日历控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/468696/
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-10-29 12:32:03  来源:igfitidea点击:

Java date and calendar controls

javaswingcontrolscomponentsjavabeans

提问by Miles D

Does anybody have any recommendations of good date pickers (either drop down calendars or small calendar components) for use in a Java Swing application - either beans or source code? They need to be robust enough for commercial applications.

有没有人有任何关于在 Java Swing 应用程序中使用的好的日期选择器(下拉日历或小型日历组件)的建议——bean 或源代码?它们需要足够健壮以用于商业应用。

回答by Kosi2801

JCalendaris free and provides beans and source code. It is quite configurable and in my applications so far served its purpose quite well.

JCalendar是免费的,并提供 bean 和源代码。它是非常可配置的,并且在我的应用程序中到目前为止它的用途非常好。

回答by Kosi2801

Try swinglabs. There's lots more than datepickers... enjoy.

尝试摇摆实验室。不仅仅是日期选择器......享受。

回答by Thomas Zuberbühler

I agree with Dan Dimerman and can recommend the swingX library. We use JXDatePickerwithin a commercial application as well. And with JXMonthViewyou've got the possibility to write your own variant of date picker.

我同意 Dan Dimerman 的观点,可以推荐 swingX 库。我们JXDatePicker也在商业应用程序中使用。并且JXMonthView您可以编写自己的日期选择器变体。

回答by Adel Bachene

you can use this one just change the main on your needs ^^

你可以使用这个,只是根据你的需要改变主要的^^

   import java.awt.BorderLayout;
   import java.awt.Color;
   import java.awt.Dimension;
   import java.awt.GridLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;

   import javax.swing.JButton;
   import javax.swing.JDialog;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JPanel;
   import javax.swing.JTextField;

   public class DatePicker {
 int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
 int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;
 JLabel l = new JLabel("", JLabel.CENTER);
 String day = "";
 JDialog d;
 JButton[] button = new JButton[49];

 public DatePicker(JFrame parent) {
         d = new JDialog();
         d.setModal(true);
         String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" };
         JPanel p1 = new JPanel(new GridLayout(7, 7));
         p1.setPreferredSize(new Dimension(430, 120));

         for (int x = 0; x < button.length; x++) {
                 final int selection = x;
                 button[x] = new JButton();
                 button[x].setFocusPainted(false);
                 button[x].setBackground(Color.white);
                 if (x > 6)
                         button[x].addActionListener(new ActionListener() {
                                 public void actionPerformed(ActionEvent ae) {
                                         day = button[selection].getActionCommand();
                                         d.dispose();
                                 }
                         });
                 if (x < 7) {
                         button[x].setText(header[x]);
                         button[x].setForeground(Color.red);
                 }
                 p1.add(button[x]);
         }
         JPanel p2 = new JPanel(new GridLayout(1, 3));
         JButton previous = new JButton("<< Previous");
         previous.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent ae) {
                         month--;
                         displayDate();
                 }
         });
         p2.add(previous);
         p2.add(l);
         JButton next = new JButton("Next >>");
         next.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent ae) {
                         month++;
                         displayDate();
                 }
         });
         p2.add(next);
         d.add(p1, BorderLayout.CENTER);
         d.add(p2, BorderLayout.SOUTH);
         d.pack();
         d.setLocationRelativeTo(parent);
         displayDate();
         d.setVisible(true);
 }

 public void displayDate() {
         for (int x = 7; x < button.length; x++)
                 button[x].setText("");
         java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                         "MMMM yyyy");
         java.util.Calendar cal = java.util.Calendar.getInstance();
         cal.set(year, month, 1);
         int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK);
         int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
         for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++)
                 button[x].setText("" + day);
         l.setText(sdf.format(cal.getTime()));
         d.setTitle("Date Picker");
 }

 public String setPickedDate() {
         if (day.equals(""))
                 return day;
         java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
                         "dd-MM-yyyy");
         java.util.Calendar cal = java.util.Calendar.getInstance();
         cal.set(year, month, Integer.parseInt(day));
         return sdf.format(cal.getTime());
 }
}

  class Picker {
 public static void main(String[] args) {
         JLabel label = new JLabel("Selected Date:");
         final JTextField text = new JTextField(20);
         JButton b = new JButton("popup");
         JPanel p = new JPanel();
         p.add(label);
         p.add(text);
         p.add(b);
         final JFrame f = new JFrame();
         f.getContentPane().add(p);
         f.pack();
         f.setVisible(true);
         b.addActionListener(new ActionListener() {
                 public void actionPerformed(ActionEvent ae) {
                         text.setText(new DatePicker(f).setPickedDate());
                 }
         });
 }

  }