Java 程序以在 Java 中设置日期选择器
要设置日期选择器,首先创建一个日期对象 −
Date today = new Date();
现在,使用 SpinnerDateModel −
JSpinner spinner2 = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH));
我们现在将考虑一个 DateEditor,它是 JSpinner 的编辑器,其模型是 SpinnerDateModel。在此处设置月份的格式 −
JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner2, "MM"); spinner2.setEditor(editor);
以下是在 Java 中设置日期选择器的示例 −
示例
package my; import java.awt.GridBagLayout; import java.util.Calendar; import java.util.Date; import javax.swing.*; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Spinner Demo"); JPanel panel = new JPanel(); JLabel label = new JLabel("ResultID"); JLabel label2 = new JLabel(" Appeared On (Month)"); panel.setLayout(new GridBagLayout()); int min = 0; int max = 10; int step = 1; int i = 3; SpinnerModel value = new SpinnerNumberModel(i, min, max, step); JSpinner spinner = new JSpinner(value); spinner.setBounds(50, 80, 70, 100); Date today = new Date(); JSpinner spinner2 = new JSpinner(new SpinnerDateModel(today, null, null, Calendar.MONTH)); JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner2, "MM"); spinner2.setEditor(editor); panel.add(label); panel.add(spinner); panel.add(label2); panel.add(spinner2); frame.add(panel); frame.setSize(550,300); frame.setVisible(true); } }
输出
广告