嗨,大家好只想尝试从字符串中检索到的JComboBox日期...但是如果我愿意的话,还可以在GUI上进行更改...我为此疯狂,因为我不知道如何做到这一点.. .Cheers =)
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);
}
}
答案 0 :(得分:3)
阅读Oracle教程How to Use Combo Boxes
让XxxRenderer
能够取消注释//comboBoxDate.setRenderer(new 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();
}
});
}
}
答案 1 :(得分:2)
我认为你可以像JCalendar那样使用一些非常好的库。您可以在此处找到一些信息:JCalendar
希望这能帮到你!
答案 2 :(得分:2)
如果您知道格式,则可以使用SimpleDateFormat
将String
转换为Date
...
String strDate = "01011970";
Date date = new SimpleDateFormat("ddMMyyyy").parse(strDate);
要更改日期,您需要提供有效供用户选择的Date
。
我建议使用Swinglabs'JXDatePicker
或已经提出建议,JCalendar
并为自己省去很多痛苦
答案 3 :(得分:2)
见How to Use Spinners&amp;最具体的是JSpinner.DateEditor
。
答案 4 :(得分: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