Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将日期从字符串添加到JComboBox_Java_Swing_Date_Jcombobox - Fatal编程技术网

Java 如何将日期从字符串添加到JComboBox

Java 如何将日期从字符串添加到JComboBox,java,swing,date,jcombobox,Java,Swing,Date,Jcombobox,大家好,我只是想在JComboBox上输入从字符串中检索到的日期…但如果我愿意,还可以在GUI上更改…我对此非常着迷,因为我不知道怎么做…干杯=) 我认为您可以使用一些非常好的库来实现这一目的,比如JCalendar。在这里您可以找到一些信息: 希望这对你有帮助 如果您知道格式,可以使用将字符串转换为日期 String strDate = "01011970"; Date date = new SimpleDateFormat("ddMMyyyy").parse(strDate); 要更改日期

大家好,我只是想在JComboBox上输入从字符串中检索到的日期…但如果我愿意,还可以在GUI上更改…我对此非常着迷,因为我不知道怎么做…干杯=)


我认为您可以使用一些非常好的库来实现这一目的,比如JCalendar。在这里您可以找到一些信息:


希望这对你有帮助

如果您知道格式,可以使用将
字符串
转换为
日期

String strDate = "01011970";
Date date = new SimpleDateFormat("ddMMyyyy").parse(strDate);
要更改日期,您需要提供用户可以选择的有效日期


我建议使用Swinglabs’或已经有人建议过的,
JCalendar
,这样可以省去很多麻烦

您可以使用
SimpleDateFormat

String edate = "November 1, 1970";
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(edate);
System.out.println(date);
输出

Sun Nov 01 00:00:00 IST 1970
  • 阅读Oracle教程

  • 使用在中实现的方法

  • XxxRenderer
    取消注释
    //comboBoxDate.setRenderer(新的ComboBoxRenderer())

然后

源代码,仅基于API中实现的标准方法

import java.awt.Component;
import java.awt.GridLayout;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class ComboBoxModels {

    private JComboBox comboBoxDouble;
    private JComboBox comboBoxInteger;
    private JComboBox comboBoxBoolean;
    private JComboBox comboBoxIcon;
    private JComboBox comboBoxDate;
    private Vector<Double> doubleVector = new Vector<Double>();
    private Vector<Integer> integerVector = new Vector<Integer>();
    private Vector<Boolean> booleanVector = new Vector<Boolean>();
    private Vector<Icon> iconVector = new Vector<Icon>();
    private Vector<Date> dateVector = new Vector<Date>();
    private Icon icon1 = ((UIManager.getIcon("OptionPane.errorIcon")));
    private Icon icon2 = (UIManager.getIcon("OptionPane.informationIcon"));
    private Icon icon3 = (UIManager.getIcon("OptionPane.warningIcon"));
    private Icon icon4 = (UIManager.getIcon("OptionPane.questionIcon"));
    private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

    public ComboBoxModels() {
        doubleVector.addElement(1.001);
        doubleVector.addElement(10.00);
        doubleVector.addElement(0.95);
        doubleVector.addElement(4.2);
        comboBoxDouble = new JComboBox(doubleVector);
        integerVector.addElement(1);
        integerVector.addElement(2);
        integerVector.addElement(3);
        integerVector.addElement(4);
        comboBoxInteger = new JComboBox(integerVector);
        booleanVector.add(Boolean.TRUE);
        booleanVector.add(Boolean.FALSE);
        comboBoxBoolean = new JComboBox(booleanVector);
        iconVector.addElement(icon1);
        iconVector.addElement(icon2);
        iconVector.addElement(icon3);
        iconVector.addElement(icon4);
        comboBoxIcon = new JComboBox(iconVector);
        dateVector.addElement(parseDate("25.01.2013"));
        dateVector.addElement(parseDate("01.02.2013"));
        dateVector.addElement(parseDate("03.03.2013"));
        dateVector.addElement(parseDate("18.04.2013"));
        comboBoxDate = new JComboBox(dateVector);
        //comboBoxDate.setRenderer(new ComboBoxRenderer());
        JFrame frame = new JFrame("");
        frame.setLayout(new GridLayout(2, 2, 5, 5));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(comboBoxDouble);
        frame.add(comboBoxInteger);
        frame.add(comboBoxBoolean);
        frame.add(comboBoxIcon);
        frame.add(comboBoxDate);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    private Date parseDate(String str) {
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (ParseException ex) {
        }
        return date;
    }

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {

        private static final long serialVersionUID = 1L;


        public ComboBoxRenderer() {
            setOpaque(true);
            setBorder(new EmptyBorder(1, 1, 1, 1));
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            if (!(value instanceof Date)) {
                return this;
            }
            setText(sdf.format((Date) value));
            return this;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ComboBoxModels comboBoxModel = new ComboBoxModels();
            }
        });
    }
}
导入java.awt.Component;
导入java.awt.GridLayout;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入java.util.Vector;
导入javax.swing.Icon;
导入javax.swing.JComboBox;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JList;
导入javax.swing.ListCellRenderer;
导入javax.swing.SwingUtilities;
导入javax.swing.UIManager;
导入javax.swing.border.EmptyBorder;
公共类组合模型{
私人JComboxComboxDouble;
私有JComboBox ComboxInteger;
私有JComboxComboxBoolean;
专用JComboBox ComboxIcon;
私人JComboBox ComboxDate;
私有向量doubleVector=新向量();
私有向量integerVector=新向量();
私有向量booleanVector=新向量();
私有向量IConverctor=新向量();
私有向量dateVector=新向量();
私有图标icon1=((UIManager.getIcon(“OptionPane.errorIcon”));
私有图标icon2=(UIManager.getIcon(“OptionPane.informationIcon”);
私有图标icon3=(UIManager.getIcon(“OptionPane.warningIcon”);
私有图标icon4=(UIManager.getIcon(“OptionPane.questionIcon”);
私有SimpleDataFormat sdf=新SimpleDataFormat(“dd.MM.yyyy”);
公共模型(){
双矢量加法(1.001);
双矢量加法(10.00);
双矢量加法(0.95);
双矢量加法(4.2);
comboBoxDouble=新的jComboxBox(doubleVector);
积分加元(1);
积分加元(2);
积分补元(3);
积分补元(4);
ComboxInteger=新的JComboBox(integerVector);
booleanVector.add(Boolean.TRUE);
booleanVector.add(Boolean.FALSE);
comboBoxBoolean=新的jComboxBox(booleanVector);
Iconvertor.addElement(icon1);
Iconverter.addElement(icon2);
Iconverter.addElement(icon3);
Iconverter.addElement(icon4);
ComboxIcon=新的JComboBox(IConverctor);
dateVector.addElement(解析日期(“25.01.2013”);
dateVector.addElement(解析日期(“01.02.2013”);
dateVector.addElement(解析日期(“03.03.2013”);
dateVector.addElement(解析日期(“18.04.2013”);
ComboxDate=新的JComboBox(dateVector);
//setRenderer(新的ComboBoxRenderer());
JFrame=新JFrame(“”);
frame.setLayout(新的GridLayout(2,2,5,5));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(comboBoxDouble);
frame.add(comboBoxInteger);
frame.add(comboBoxBoolean);
frame.add(comboBoxIcon);
frame.add(comboBoxDate);
frame.setLocationRelativeTo(空);
frame.pack();
frame.setVisible(true);
}
私有日期解析日期(字符串str){
日期=新日期();
试一试{
日期=sdf.parse(str);
}捕获(解析异常){
}
返回日期;
}
私有类ComboBoxRenderer扩展JLabel实现ListCellRenderer{
私有静态最终长serialVersionUID=1L;
公共ComboxRenderer(){
set不透明(true);
新订单(新的空订单(1,1,1,1));
}
@凌驾
公共组件GetListCellRenderComponent(JList列表、对象值、,
整型索引,布尔型isSelected,布尔型cellHasFocus){
如果(!(日期的值实例)){
归还这个;
}
setText(sdf.format((日期)值));
归还这个;
}
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
ComboBoxModels comboBoxModel=新ComboBoxModels();
}
});
}
}
请参见&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&mdash;&


“只是想在JComboBox上显示从字符串中检索到的日期…但如果我想…,也可以在GUI上更改…”
--请将此翻译成英语。你是说你不明白我的问题?sghhh我需要一个日期(dd/mm/yyyyy)在来自字符串的jcombobox上…我可以使用方法st.getBirthday()获取它。。。如果需要,还可以在面板上进行更改,清楚吗?不知道如何更好地解释:P谢谢,我也不太明白,请确保这个社区必须有很长的时间来适应我的英语新分支关于
JComboBox
Date
s的问题,我在这个代码片段中看到的第一件事是“first Name”和“Last Name”标签和文本字段。只发布相关代码怎么样。这至少表明了你的努力你的评论很可笑,罗宾。。。我认为把整个代码放在那里(不是那么大)会让其他人更容易
import java.awt.Component;
import java.awt.GridLayout;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class ComboBoxModels {

    private JComboBox comboBoxDouble;
    private JComboBox comboBoxInteger;
    private JComboBox comboBoxBoolean;
    private JComboBox comboBoxIcon;
    private JComboBox comboBoxDate;
    private Vector<Double> doubleVector = new Vector<Double>();
    private Vector<Integer> integerVector = new Vector<Integer>();
    private Vector<Boolean> booleanVector = new Vector<Boolean>();
    private Vector<Icon> iconVector = new Vector<Icon>();
    private Vector<Date> dateVector = new Vector<Date>();
    private Icon icon1 = ((UIManager.getIcon("OptionPane.errorIcon")));
    private Icon icon2 = (UIManager.getIcon("OptionPane.informationIcon"));
    private Icon icon3 = (UIManager.getIcon("OptionPane.warningIcon"));
    private Icon icon4 = (UIManager.getIcon("OptionPane.questionIcon"));
    private SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");

    public ComboBoxModels() {
        doubleVector.addElement(1.001);
        doubleVector.addElement(10.00);
        doubleVector.addElement(0.95);
        doubleVector.addElement(4.2);
        comboBoxDouble = new JComboBox(doubleVector);
        integerVector.addElement(1);
        integerVector.addElement(2);
        integerVector.addElement(3);
        integerVector.addElement(4);
        comboBoxInteger = new JComboBox(integerVector);
        booleanVector.add(Boolean.TRUE);
        booleanVector.add(Boolean.FALSE);
        comboBoxBoolean = new JComboBox(booleanVector);
        iconVector.addElement(icon1);
        iconVector.addElement(icon2);
        iconVector.addElement(icon3);
        iconVector.addElement(icon4);
        comboBoxIcon = new JComboBox(iconVector);
        dateVector.addElement(parseDate("25.01.2013"));
        dateVector.addElement(parseDate("01.02.2013"));
        dateVector.addElement(parseDate("03.03.2013"));
        dateVector.addElement(parseDate("18.04.2013"));
        comboBoxDate = new JComboBox(dateVector);
        //comboBoxDate.setRenderer(new ComboBoxRenderer());
        JFrame frame = new JFrame("");
        frame.setLayout(new GridLayout(2, 2, 5, 5));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(comboBoxDouble);
        frame.add(comboBoxInteger);
        frame.add(comboBoxBoolean);
        frame.add(comboBoxIcon);
        frame.add(comboBoxDate);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
    }

    private Date parseDate(String str) {
        Date date = new Date();
        try {
            date = sdf.parse(str);
        } catch (ParseException ex) {
        }
        return date;
    }

    private class ComboBoxRenderer extends JLabel implements ListCellRenderer {

        private static final long serialVersionUID = 1L;


        public ComboBoxRenderer() {
            setOpaque(true);
            setBorder(new EmptyBorder(1, 1, 1, 1));
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value,
                int index, boolean isSelected, boolean cellHasFocus) {
            if (!(value instanceof Date)) {
                return this;
            }
            setText(sdf.format((Date) value));
            return this;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                ComboBoxModels comboBoxModel = new ComboBoxModels();
            }
        });
    }
}