2011-08-16 48 views
0

你如何使用Swing的文本字段從另一個減去一個日期嗎?有沒有可能是一個DateField?我可以使用Swing TextField在Java中進行日期數學嗎?

+0

您是否在尋找格式化文本框? - http://download.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html – miku

+3

減去日期是合乎邏輯的操作,而不是一個GUI之一。你真的想做什麼? –

回答

1

使用JFormattedTextField,叫myFormattedTextField.setValue(new Date()),而且該格式設置爲javax.swing.text.DateFormatter

下面是一個簡單示例:

JFormattedTextField formattedTextField1 = new JFormattedTextField(); 
formattedTextField1.setFormatterFactory(new DefaultFormatterFactory(new DateFormatter(DateFormat.getDateInstance(DateFormat.SHORT)))); 
formattedTextField1.setValue(new Date()); 
1

或考慮對日期更好JComponent /時間/ DATATIME將JSpinner

enter image description here

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 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

+1我以前運行過這個很好的例子;但仔細觀察,我發現它也是'SimpleDateFormat'和'TimeZone'在應用於同一個'Date'時如何關聯的一個很好的例子。 – trashgod

+1

@trashgod感謝,但最初的想法是由其他作者(@Andrew湯普森),然後是簡單的與其他insipration一起合併 – mKorbel

相關問題