2012-10-09 50 views
1

我想使日曆月&年選擇器。如何從JSpinner結果值設置日曆?

如何設置兩個jspinner的日曆?

的JSpinner 1 =月份值

的JSpinner 2 =年份值

也許這只是爲了讓從JSpinner的int值.. 怎麼辦呢?

這裏是我的代碼: (更新)

import java.awt.BorderLayout; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JSpinner; 
    import javax.swing.SpinnerDateModel; 
    import javax.swing.event.ChangeEvent; 
    import javax.swing.event.ChangeListener; 


    public class datepicker extends JFrame{ 
     JSpinner Smonth = new JSpinner(); 
     JSpinner Syear = new JSpinner(); 
     Calendar cal = Calendar.getInstance(); 
     Date date = cal.getTime(); 
     SimpleDateFormat sdf_Month = new SimpleDateFormat("MM"); 
     SimpleDateFormat sdf_Year = new SimpleDateFormat("yyyy"); 
     SimpleDateFormat sdf_final_Date = new SimpleDateFormat("MMMM yyyy"); 
     JLabel label = new JLabel(); 

     datepicker(){ 
      Smonth.setModel(new SpinnerDateModel(date, null, null, cal.MONTH)); 
      Smonth.setEditor(new JSpinner.DateEditor(Smonth, "MMMM")); 
      Syear.setModel(new SpinnerDateModel(date, null, null, cal.YEAR)); 
      Syear.setEditor(new JSpinner.DateEditor(Syear, "yyyy")); 

      Smonth.addChangeListener(new ChangeListener() { 

       @Override 
       public void stateChanged(ChangeEvent e) { 
        execute(); 

       } 
      }); 

      Syear.addChangeListener(new ChangeListener() { 

       @Override 
       public void stateChanged(ChangeEvent e) { 
        execute(); 

       } 
      }); 

      setLayout(new BorderLayout()); 
      add(Smonth, BorderLayout.WEST); 
      add(label, BorderLayout.CENTER); 
      add(Syear, BorderLayout.EAST); 

      execute(); 
     } 

      void execute(){ 


       cal.set(Integer.parseInt(sdf_Year.format(Syear.getValue())), 
        (Integer.parseInt(sdf_Month.format(Smonth.getValue())))-1, 
        1); 

       label.setText(sdf_final_Date.format(cal.getTime())); 
      } 

      public static void main(String[]args){ 
       new datepicker().setVisible(true); 
      }  
    } 

我的完整簡單的日期選擇器

package Feature.Other; 

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Component; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.GridLayout; 
import java.awt.Insets; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.text.DateFormat; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import javax.swing.JDialog; 
import javax.swing.JFormattedTextField; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JSpinner; 
import javax.swing.SpinnerDateModel; 
import javax.swing.event.ChangeEvent; 
import javax.swing.event.ChangeListener; 

public class Calender extends JDialog{ 
    private Calendar cal=Calendar.getInstance(); 
    public java.sql.Date initDate = new java.sql.Date(cal.getTime().getTime()); 
    private JPanel north = new JPanel(); 
    private JPanel center = new JPanel(); 
    private JLabel lFinalDate=new JLabel("tanggal"); 
    private JLabel lDate[]= new JLabel[49]; 
    private String day[]= {"SUN","MON","TUE","WED","THU","FRI","SAT"}; 
    private JSpinner smonth = new JSpinner(); 
    private JSpinner sYear = new JSpinner(); 
    private DateFormat sdf = new SimpleDateFormat("dd MMMM yyyy"); 
    private DateFormat sdfM = new SimpleDateFormat("MM"); 
    private DateFormat sdfY = new SimpleDateFormat("yyyy"); 

    public Calender() { 

     smonth.setModel(new SpinnerDateModel(initDate, null, null, cal.MONTH)); 
     smonth.setEditor(new JSpinner.DateEditor(smonth, "MMMM")); 
     JFormattedTextField jft = ((JSpinner.DefaultEditor)smonth.getEditor()).getTextField(); 
     jft.setEditable(false); 
     jft.setHorizontalAlignment(JLabel.LEFT); 
     sYear.setModel(new SpinnerDateModel(initDate, null, null, cal.YEAR)); 
     sYear.setEditor(new JSpinner.DateEditor(sYear, "yyyy")); 
     JFormattedTextField jft2 = ((JSpinner.DefaultEditor)sYear.getEditor()).getTextField(); 
     jft2.setEditable(false); 

     smonth.addChangeListener(new ChangeListener() { 
      @Override 
      public void stateChanged(ChangeEvent e) { 
       dor(); 
      } 
     }); 

     sYear.addChangeListener(new ChangeListener() { 
      @Override 
      public void stateChanged(ChangeEvent e) { 
       dor(); 
      } 
     }); 

     setLayout(new BorderLayout()); 
     setSize(500,300); 
     init(); 
    } 

    private void init(){ 

     add(north, BorderLayout.NORTH); 
     add(center, BorderLayout.CENTER); 
     north.setLayout(new GridBagLayout()); 
     center.setLayout(new GridLayout(7,7)); 

     addComponen(sYear, 2,0,1,1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     addComponen(smonth, 0,0,1,1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 
     addComponen(lFinalDate,1,0,1,1, GridBagConstraints.CENTER, GridBagConstraints.BOTH); 

     for (int i=0; i<49; i++){ 
      lDate[i]= new JLabel(""); 
      center.add(lDate[i]); 

      if(i<7){ 
       lDate[i].setText(day[i]); 
       } 
     } 
     dor(); 
    } 

    private void addComponen(Component component,int gridx, int gridy, int gridwid, int gridheig,int achor, int fill){ 

     GridBagConstraints gc = new GridBagConstraints(gridx, gridy, gridwid, gridheig, 1.0, 1.0, achor,fill,new Insets(0, 0, 0, 0), 0, 0); 
     north.add(component, gc); 
    } 

    private void dor(){ 
     cal.set(Integer.parseInt(sdfY.format(sYear.getValue())), 
       Integer.parseInt(sdfM.format(smonth.getValue()))-1, 
       1); 
     lFinalDate.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); 
     lFinalDate.setText(sdf.format(cal.getTime())); 
     int dayMax = cal.getActualMaximum(Calendar.DAY_OF_MONTH); 
     int start = cal.get(Calendar.DAY_OF_WEEK); 

     for (int i=7; i<49; i++){ 
      lDate[i].setText(""); 
      lDate[i].setForeground(Color.BLACK); 
     } 

     for (int a=6+start,b=1;b<=dayMax;a++,b++){ 
      final int selection = a; 
      lDate[a].setText(""+b); 
      lDate[a].addMouseListener(new MouseAdapter() { 
       public void mouseClicked(MouseEvent evt){ 
        cal.set(Integer.parseInt(sdfY.format(sYear.getValue())), 
         Integer.parseInt(sdfM.format(smonth.getValue()))-1, 
         Integer.parseInt(lDate[selection].getText())); 
        initDate = new java.sql.Date(cal.getTime().getTime()); 
        for (int z=6;z<=42;z++){ 
        lDate[z].setForeground(Color.BLACK);} 
        lDate[selection].setForeground(Color.blue); 
        lFinalDate.setText(sdf.format(initDate));  
       } 
      }); 
     } 
    } 

    public static void main (String[]args){ 
     new Calender().setVisible(true); 
    } 

} 

回答

2
  • 請閱讀教程約JSpinner

  • 有詳細desirbtions關於JSpinner,SpinnerDateModel,INC。有關教程中的代碼示例

編輯

  • 有沒有理由去解析什麼,用正確的SpinnerXxxModel,

  • 你可以測試,如描述分裂三個JSpinners SpinnerDateModel的教程,簡單代碼

import java.awt.*; 
import java.text.SimpleDateFormat; 
import java.util.*; 
import javax.swing.*; 
import javax.swing.event.*; 

public class TimeZoneSpinners { 

    private final String[] zones = {"Asia/Tokyo", "Asia/Hong_Kong", 
     "Asia/Calcutta", "Europe/Paris", "Europe/London", 
     "America/New_York", "America/Los_Angeles" 
    }; 
    private final JLabel[] labels = new JLabel[zones.length]; 
    private final SimpleDateFormat[] formats = new SimpleDateFormat[zones.length]; 
    private JSpinner spinner; 
    private JSpinner spinnerDate; 
    private JSpinner spinnerMonth; 
    private JSpinner spinnerYear; 
    private SpinnerDateModel model; 
    private SimpleDateFormat format; 
    private JPanel panel; 
    private JFrame frame = new JFrame(); 

    public void makeUI() { 
     Calendar cal = Calendar.getInstance(); 
     Date date = cal.getTime(); 
     model = new SpinnerDateModel(); 
     model.setValue(date); 
     spinner = new JSpinner(model); 
     spinner.addChangeListener(new ChangeListener() { 

      @Override 
      public void stateChanged(ChangeEvent e) { 
       Date date = (Date) ((JSpinner) e.getSource()).getValue(); 
       for (int i = 0; i < labels.length; i++) { 
        labels[i].setText(formats[i].format(date)); 
       } 
      } 
     }); 
     format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat(); 
     format.setTimeZone(TimeZone.getTimeZone(zones[0])); 
     format.applyPattern("yyyy-MM-dd HH:mm:ss"); 
     panel = new JPanel(new GridLayout(zones.length, 2, 10, 10)); 
     for (int i = 0; i < zones.length; i++) { 
      formats[i] = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); 
      formats[i].setTimeZone(TimeZone.getTimeZone(zones[i])); 
      JLabel label = new JLabel(zones[i]); 
      labels[i] = new JLabel(formats[i].format(date)); 
      panel.add(label); 
      panel.add(labels[i]); 
     } 
     frame.setLayout(new BorderLayout(10, 10)); 
     frame.add(spinner, BorderLayout.NORTH); 
     frame.add(panel, BorderLayout.CENTER); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       new TimeZoneSpinners().makeUI(); 
      } 
     }); 
    } 
} 
+0

我已經讀過它,真的搞不清它.. 更新我的代碼..我想我做什麼,它不是一個好辦法..有其他好方法? 真的真的謝謝你的幫助和建議.. –

+0

'和真的混淆它接受'接受,在教程中描述了三種方法1)SpinnerListModel約字符串值,使用情況下,你想顯示月份名稱2)SpinnerNumberModel,如果你想爲所有三個JSpinners(日,月和年)使用munbers,3)最重要的是使用返回Date實例的SpinnerDateModel,或者4)我會更新一些關於JSpinner和SpinnerDateModel的代碼,休息取決於你,沒有理由從JSpinner解析整數或munber實例,僅使用SpinnerNumberModel – mKorbel

+0

thankz ..我已經學習了你的腳本(它需要很長時間才能理解〜, 〜「,但最後我得到了它),但我仍然混淆瞭如何獲得與我的腳本的關係..(原諒我的愚蠢)... 我需要詮釋月份價值e設置我的日曆..然後用我的日曆我設置我的jlabel [日]基於日曆(月)..我更新我的日期選擇器的完整代碼..如何做到這一點,而無需從jspinner結果解析simpledateformat int? –

2

把適當的參數類型,即:

cal.set(Integer.parseInt(sdf_Year.format(Syear.getValue())), 
      Integer.parseInt(sdf_Month.format(Smonth.getValue())), 
      1); 
+0

嘿嘿 非常感謝.. 阿公哭 –