Swing 写的日期时间组件

在网上收集的swing时间组件完善了一下,日期上下按钮可以点击,闰年平年判断,月份是多少天自动更新改变。希望大家继续完善。

MainApp

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
  
 
public class MainApp   extends JFrame {
 
     
      public static int x= 0 ;
      public static int y= 0 ;
     /**
      * @param args
      */
     public static void main(String[] args) {
          
          JFrame jf =  new JFrame();
          JPanel tabPanel =  new JPanel();
          JButton timeButton =  new DataChooser();
          tabPanel.add(timeButton);
          jf.add(tabPanel);
          jf.setBounds( 350 200 500 400 );
          jf.setVisible( true );
          jf.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
          jf.addWindowListener( new WindowAdapter(){
                            public void windowClosing(WindowEvent e){
                              int n=JOptionPane.showConfirmDialog( null , "确认退出吗?" , "确认对话框" ,
                                                       JOptionPane.YES_NO_OPTION );
                              if (n==JOptionPane.YES_OPTION) 
                                 {System.exit( 0 );}
                              return ;
                            }});
            
           jf.setEnabled( true );
     }
  
      
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Swing中提供了JDatePicker控件来创建日期选择器,但是它不支持时间选择器。如果需要创建时间选择器,可以使用JSpinner控件和SpinnerDateModel类来实现。 下面是一个简单的例子: ```java import java.awt.FlowLayout; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSpinner; import javax.swing.SpinnerDateModel; public class TimePickerExample { public static void main(String[] args) { JFrame frame = new JFrame("Time Picker Example"); frame.setLayout(new FlowLayout()); frame.setSize(300, 100); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a SpinnerDateModel with current time as default value SpinnerDateModel model = new SpinnerDateModel(Calendar.getInstance().getTime(), null, null, Calendar.HOUR_OF_DAY); // Create a JSpinner with the SpinnerDateModel JSpinner spinner = new JSpinner(model); // Set the format of the displayed value JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner, "HH:mm:ss"); spinner.setEditor(editor); // Add the spinner to the frame frame.add(new JLabel("Select Time:")); frame.add(spinner); frame.setVisible(true); } } ``` 在这个例子中,我们使用了FlowLayout布局管理器来排列控件。我们创建了一个SpinnerDateModel对象,并将当前时间设置为默认值。然后,我们使用SpinnerDateModel对象创建了一个JSpinner对象。我们还使用JSpinner.DateEditor类设置了显示值的格式。最后,我们将JSpinner添加到框架中并使其可见。 这个例子创建的时间选择器只允许选择小时、分钟和秒。如果需要选择其他时间单位,需要调整SpinnerDateModel对象的构造函数中的参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值