如何在JSpinner中同时选择时间和日期?

时间:2016-11-12 13:29:19

标签: java timer jspinner

使用下面的代码我尝试更改并从JSpinner中选择时间和日期。

实际上我可以在格式化时更改时间。

我应该尝试使用SpinnerDatamodel添加单独的JSpinner吗?

是否有可能更改日期?

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;
import java.util.Date;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SpinnerTest {

 Timer timer;
 public static void main(String args[]) {
   new SpinnerTest().startApp();
 }

 private void startApp() {
 JFrame frame = new JFrame("JSpinner Sample");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


 SpinnerModel model1 = new SpinnerDateModel();
 JSpinner spinner1 = new JSpinner(model1);
 spinner1.setEditor(new JSpinner.DateEditor(spinner1, "HH:mm:ss dd-MM-yyyy"));
 spinner1.addChangeListener(new CalendarListener());

 JLabel label1 = new JLabel("Dates/Date");
 JPanel panel1 = new JPanel(new BorderLayout());
 panel1.add(label1, BorderLayout.WEST);
 panel1.add(spinner1, BorderLayout.CENTER);
 frame.add(panel1, BorderLayout.CENTER);

 frame.setSize(200, 90);
 frame.setVisible(true);
}


 private class CalendarListener implements ChangeListener {
  @Override
  public void stateChanged(ChangeEvent e)  {
   JSpinner jSpinner = (JSpinner) e.getSource();
   Date date = (Date) jSpinner.getValue();
   long delay = date.getTime() - System.currentTimeMillis();
   timerStart();
   if (delay > 0) {
   timer.setInitialDelay((int) delay);
   timer.restart();
  }
 }   
}

private void timerStart() {
 this.timer = new Timer(Integer.MAX_VALUE, (ActionEvent evt) -> {
 System.out.println("okey");
});}

}

1 个答案:

答案 0 :(得分:0)

最后我可以让它发挥作用。

只需一个JSpinner就需要相同的Date值。

要选择值,需要在数字中间按下(小时,分钟等)。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerModel;
import java.util.Date;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class SpinnerTest {

 Timer timer;
 Date date;
 public static void main(String args[]) {
   new SpinnerTest().startApp();
 }

 private void startApp() {
 JFrame frame = new JFrame("JSpinner Sample");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 date = new Date();
 SpinnerModel model1 = new SpinnerDateModel(date,null,null,0);
 JSpinner spinner1 = new JSpinner(model1);

 spinner1.addChangeListener(new CalendarListener());
 spinner1.setEditor(new JSpinner.DateEditor(spinner1, "HH:mm dd-MM-yyyy"));

 JLabel label1 = new JLabel("Times/Dates");
 JPanel panel1 = new JPanel(new BorderLayout());
 JPanel panel2 = new JPanel(new BorderLayout());

 panel2.add(label1, BorderLayout.WEST);
 panel1.add(spinner1, BorderLayout.CENTER);

 frame.add(panel1, BorderLayout.CENTER);
 frame.add(panel2, BorderLayout.WEST);


 frame.setSize(200, 90);
 frame.setVisible(true);
}


 private class CalendarListener implements ChangeListener {
  @Override
  public void stateChanged(ChangeEvent e)  {

 JSpinner jSpinner = (JSpinner) e.getSource();
 date = (Date) jSpinner.getValue() ;


 long delay = date.getTime() - System.currentTimeMillis();
 //System.out.println(delay);
  timerStart();
  if (delay > 0) {
  timer.setInitialDelay((int) delay);
  timer.restart();
 }
}   

}

 private void timerStart() {
  this.timer = new Timer(Integer.MAX_VALUE, (ActionEvent evt) -> {
  System.out.println("okey");
 });}
}