我用JSpinner来设置时间,点击上下调整箭头按钮能够正确的调整时间,能够控制小时在0到23之间,控制分钟在0到59之间,控制秒在0到59之间;
但是我的JSpinner是可以直接编辑的,当直接输入数据的时候如何控制它的输入范围?就像Windows的日期时间对话框中的那个设置时间的JSpinner那样?救9命啊,我都急疯了,帮帮忙啊,谢谢了啊关于此的全部代码在这里:import javax.swing.*;
import javax.swing.event.*;
import java.text.*;
import java.awt.*;
import java.util.*;public class TestSpinner {  public static void main (String args[]) throws Exception {
    JFrame frame = new JFrame("Test Spinner");    SpinnerDateModel model = new SpinnerDateModel();
    model.setCalendarField(Calendar.WEEK_OF_MONTH);
    JSpinner spinner = new JSpinner(model);
    JSpinner.DateEditor editor = new JSpinner.DateEditor(
      spinner, "HH:mm:ss");    spinner.setEditor(editor);    frame.getContentPane().add(spinner, BorderLayout.CENTER) ;    frame.pack();
    frame.show();
  }
}

解决方案 »

  1.   

    我做VC的时候,都是截获消息的,在java里估计不能这么来
    但是java应该有个什么可以重载的触发事件吧,当你敲击的时候,
    可能叫做Edit Changed什么的
      

  2.   

    我原来是想在editor里加上keylistener然后判断输入的字符,但我发现加进去的keylistener不起作用. 于是只能彩另一种方法, 就是将editor设为只读, 不知能否接受.JFrame frame = new JFrame("Test Spinner");SpinnerDateModel model = new SpinnerDateModel();
    model.setCalendarField(Calendar.WEEK_OF_MONTH);
    JSpinner spinner = new JSpinner(model);
    JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner,
    "HH:mm:ss");
    editor.getTextField().setEditable(false);//加上这句.
    spinner.setEditor(editor);

    frame.getContentPane().add(spinner, BorderLayout.CENTER);frame.pack();
    frame.show();原来的想法是这样
    editor.getTextField().addKeyListener(new MyKeyListener());
    但发现不起做用.
    editor.addKeyListener(new MyKeyListener());和
    spinner.addKeyListener(new MyKeyListener());
    也试过,都不起作用, 根本拦截不到键盘输入.
      

  3.   

    你运气好, 我在java.sun.com上找到了答案.import javax.swing.*;
    import javax.swing.event.*;
    import java.text.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    import javax.swing.text.*;public class MySpinner
    {
    JFormattedTextField tf; public MySpinner()
    {
    JFrame frame = new JFrame("Spinner");
    frame.setDefaultCloseOperation(3); final SpinnerDateModel model = new SpinnerDateModel();
    JSpinner spinner = new JSpinner(model);
    JSpinner.DateEditor editor = new JSpinner.DateEditor(spinner,
    "HH:ss:mm");
    spinner.setEditor(editor); tf = ((JSpinner.DateEditor) spinner.getEditor()).getTextField();
    tf.setEditable(true);
    DefaultFormatterFactory factory = (DefaultFormatterFactory) tf
    .getFormatterFactory();
    DateFormatter formatter = (DateFormatter) factory.getDefaultFormatter();
    formatter.setAllowsInvalid(false); frame.getContentPane().add(spinner, BorderLayout.SOUTH);
    frame.pack();
    frame.show();
    } public static void main(String args[]) throws Exception
    {
    new MySpinner();
    }}
      

  4.   

    sorry, 我没有测试就把代码贴上来了, 不知大家发现没有那段代码运行后虽然不能输入错误的时间范围,但输入时有问题. 我在http://www.javadatepicker.com上找到了一个好东东, 那里有一些API, 已经实现了楼主想要的功能, 可以直接使用. 我现在把实现了Windows datetime 属性框的代码贴上来./**
     * @author Administrator
     *
     */
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.SwingConstants;
    import javax.swing.UIManager;import com.standbysoft.component.date.swing.JDateField;
    import com.standbysoft.component.date.swing.JMonthView;
    import com.standbysoft.demo.date.plaf.NoButtonsMonthViewUI;
    import com.standbysoft.demo.date.plaf.SpinnerTitleMonthUI;/**
     * Reproduces in part the Date/Time Properties dialog from Windows. The purpose
     * of this demo is to show that the calendar (JMonthView) can be customized to 
     * take on any appearance by changing its UI delegate.
     */
    public class WindowsDateTimePropertiesDemo extends JTabbedPane {
        public WindowsDateTimePropertiesDemo() {
            add("Date & Time", createDateTimePane());
            add("Time Zone", createTimeZonePane());
        }    private JPanel createDateTimePane() {
            JPanel panel = new JPanel();        JPanel datePanel = new JPanel();
            datePanel.setBorder(BorderFactory.createTitledBorder("Date"));        UIManager.put("MonthViewUI", NoButtonsMonthViewUI.class.getName());
            UIManager.put("MonthUI", SpinnerTitleMonthUI.class.getName());
            
            JMonthView monthView = new JMonthView();
            monthView.setFont(monthView.getFont().deriveFont(11.0f));
            monthView.setDateRenderer(new CustomDateRenderer());
            monthView.setWeekNumbersVisible(false);
            monthView.setStatusVisible(false);
            monthView.setBorder(BorderFactory.createEmptyBorder());
            monthView.setTrailingNextEnabled(false);
            monthView.setTrailingPreviousEnabled(false);
            
            datePanel.setLayout(new GridBagLayout());
            datePanel.add(monthView, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
            
            JPanel timePanel = new JPanel();
            JDateField dateField = new JDateField();
            dateField.setCustomDateFormat("hh:mm:ss a");
            dateField.setHorizontalAlignment(SwingConstants.RIGHT);
            
            timePanel.setBorder(BorderFactory.createTitledBorder("Time"));
            timePanel.setLayout(new GridBagLayout());
            timePanel.add(new JLabel("Windows Clock"), new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
            timePanel.add(dateField, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 5, 5), 0, 0));
            
            panel.setLayout(new GridBagLayout());
            panel.add(datePanel, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
            panel.add(timePanel, new GridBagConstraints(1, 0, 1, 1, 2.0, 1.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 5, 5, 5), 0, 0));
            panel.add(new JLabel("Current time zone: Greenwich Standard"), new GridBagConstraints(0, 1, 2, 1, 1.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 9, 5, 5), 0, 0));
            
            return panel;
        }
        
        private JPanel createTimeZonePane() {    
            JPanel timezonePanel = new JPanel();
            timezonePanel.add(new JLabel());
            
            return timezonePanel;
        }
        
        private static void createAndShowGUI() {
            //Create and set up the window.
            JFrame frame = new JFrame("Date/Time Properties Demo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        //Create and set up the content pane.
            WindowsDateTimePropertiesDemo newContentPane = new WindowsDateTimePropertiesDemo();
            newContentPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(newContentPane);        //Display the window.
            frame.pack();
            frame.setVisible(true);    
        }    public static void main(String[] args) {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }        
            
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    } 其中用到了下面的class/**
     * @author Administrator
     *
     */
    import java.awt.Color;
    import java.awt.Graphics;import com.standbysoft.component.date.swing.DefaultDateRenderer;/**
     * Custom date renderer that extends the default one to display the selected
     * dates as rectangles and not like ovals.
     */
    public class CustomDateRenderer extends DefaultDateRenderer {
        public CustomDateRenderer() {
        }
        
        /**
         * Paints a selected date as a rectangle.
         */
        protected void paintSelected(Graphics g) {
            g.setColor(month.getTitleBackground());
            g.fillRect(1, 1, getWidth() - 1, getHeight() - 1);
        }    /**
         * Paints the today date as a rectangle with a red border.
         */
        protected void paintToday(Graphics g) {
            g.setColor(Color.red);
            g.drawRect(1, 1, getWidth() - 2, getHeight() - 2);
        }

    这两个程序都是从http://www.javadatepicker.com上下载的API包里的demo.