在组合框中显示时间 [英] show the time in a combobox

查看:343
本文介绍了在组合框中显示时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个ComboBox,显示一天中每小时的列表,没有成功。它给我构造函数JComboBox(JSpinner)未定义的错误。任何帮助?谢谢

I'm trying to implement a ComboBox that shows the list of every hour of the day, without success. It gives me "The constructor JComboBox(JSpinner) is undefined" error. any help? Thank you

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 24);
    calendar.set(Calendar.MINUTE, 0);
    SpinnerDateModel model = new SpinnerDateModel();
    model.setValue(calendar.getTime());
    JSpinner spinner = new JSpinner(model);
    departureTime = new JComboBox<String>(spinner);


推荐答案

不,这没有意义。您应该传递combobox a ComboBoxModel 填充您想要显示的值

No, this makes no sense. You should be passing the combobox a ComboBoxModel filled with the values you want displayed

请参阅如何使用组合框如何使用纺纱器了解更多详情

请使用类似的类型,它使用 JSpinner 显示时间并允许用户操作

You can either use something like this which uses a JSpinner to display the time and allows the user to manipulate it

或者您需要向组合框填充您希望用户允许选择,例如给定的时间间隔...

Or you need to fill the combobox with the values you want the user to be allowed to select, such as a given time interval...

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);

            Calendar end = Calendar.getInstance();
            end.set(Calendar.HOUR_OF_DAY, 23);
            end.set(Calendar.MINUTE, 59);
            DefaultComboBoxModel<Date> model = new DefaultComboBoxModel<>();
            do {
                model.addElement(calendar.getTime());
                calendar.add(Calendar.MINUTE, 15);
            } while (calendar.getTime().before(end.getTime()));

            JComboBox<Date> cb = new JComboBox<>(model);
            cb.setRenderer(new DateFormattedListCellRenderer(new SimpleDateFormat("HH:mm")));

            add(cb);

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        public class DateFormattedListCellRenderer extends DefaultListCellRenderer {

            private DateFormat format;

            public DateFormattedListCellRenderer(DateFormat format) {
                this.format = format;
            }

            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                if (value instanceof Date) {
                    value = format.format((Date) value);
                }
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            }

        }

    }

}

这篇关于在组合框中显示时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆