-2

嗨,大家好,只是想在 JComboBox 上设置从字符串中检索到的日期……但如果我愿意,也可以在 GUI 上进行更改……我为此发疯了,因为我不知道该怎么做……干杯 = )

public class StudentInfoGUI extends JPanel{  

private JTextField stFirstName;
private JTextField stLastName;
private JComboBox birthDate,birthMonth,birthYear;
private JRadioButton genderMale,genderFemale;
private JLabel l1,l2,l3,l4,l5;

public StudentInfoGUI(){


    super();
    StudentInfo st = new StudentInfo("123456","Homer","Simpsons","01011970",'m');

    l1 = new JLabel("First name:");
    stFirstName = new JTextField(st.getFirstName());
    l2 = new JLabel("Last name:");
    stLastName = new JTextField(st.getLastName());

    Calendar cal = new GregorianCalendar();
   // int date = cal.get(Calendar.DAY_OF_MONTH);
    //int month = cal.
    int year = cal.get(Calendar.YEAR);
    l3 = new JLabel("Birthday");
    birthDate = new JComboBox();
    birthDate.addItem(st.getBirthday());
    //birthMonth.addItem(month);
    birthYear = new JComboBox();


    l4 = new JLabel("male");
    genderMale = new JRadioButton();
    l5 = new JLabel("female");
    genderFemale = new JRadioButton();
    if(st.getGender()== 'm'){
        genderMale.setSelected(true);
    }
    else
        genderFemale.setSelected(true);


    JPanel stPanel = new JPanel();
    stPanel.setLayout(new GridLayout(3,4));

    stPanel.add(l1);
    stPanel.add(stFirstName);
    stPanel.add(l2);
    stPanel.add(stLastName);
    stPanel.add(l3);
    stPanel.add(birthDate);
    stPanel.add(birthMonth);
    stPanel.add(birthYear);
    stPanel.add(l4);
    stPanel.add(genderMale);
    stPanel.add(l5);
    stPanel.add(genderFemale);

    add(stPanel);
} 
public static void main(String[] args) {
    // TODO code application logic here
    JFrame frame = new JFrame("Student info");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new StudentInfoGUI());
    frame.pack(); 
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}
}
4

5 回答 5

3

然后

在此处输入图像描述

来自代码,仅基于 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();
            }
        });
    }
}
于 2013-05-23T07:03:23.143 回答
2

如果您知道格式,则可以将 a 转换StringDateusing ...SimpleDateFormat

String strDate = "01011970";
Date date = new SimpleDateFormat("ddMMyyyy").parse(strDate);

要更改日期,您需要提供Date有效的 s 供用户选择。

我建议 SwinglabsJXDatePicker或已经建议,JCalendar并为自己省去很多痛苦

于 2013-05-23T06:32:41.517 回答
2

请参阅如何使用微调器,尤其是JSpinner.DateEditor.

微调日期演示。

于 2013-05-23T07:07:34.337 回答
2

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

希望这对你有帮助!

于 2013-05-23T06:16:44.377 回答
0

您可以使用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
于 2013-05-23T06:44:07.937 回答