自动填充组合框系列-Java

自动填充组合框系列-Java,java,date,jcombobox,autofill,Java,Date,Jcombobox,Autofill,我在写一个简单的程序;这个程序的一部分是一系列组合框,我在其中输入了月份(字母)、天数(数字)和年份(数字)字符串。我想让Java以某种方式提取日期,以月、日和年为单位,然后根据系统时钟自动用正确的日期填充这些组合框 以下是我的一些代码: public static final String[] MONTHS = {"January", "February", "March", "April", "May", "June",

我在写一个简单的程序;这个程序的一部分是一系列组合框,我在其中输入了月份(字母)、天数(数字)和年份(数字)字符串。我想让Java以某种方式提取日期,以月、日和年为单位,然后根据系统时钟自动用正确的日期填充这些组合框

以下是我的一些代码:

public static final String[] MONTHS = {"January", "February", "March", "April", "May", "June",
                                       "July", "August", "September", "October", "November",
                                       "December"};
public static final String[] DAYS = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", 
                                 "12", "13", "14", "15", "16", "17", "18", "19", "20", 
                                 "21", "22", "23", "24", "25", "26", "27", "28", "29", 
                                 "30", "31"};
public static final String[] YEARS = {"2015", "2014", "2013", "2012", "2011", "2010"};

Note to FORUMS: THIS ISN'T ALL THE CODE. I'VE JUST PROVIDED INFORMATION NECESSARY FOR THE QUESTION.

JLabel start = new JLabel("Start Date:");
if (shouldWeightX) {
c.weightx = .5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
pane.add(start, c);

JComboBox MonthLong = new JComboBox();
if (shouldWeightX) {
    c.weightx = 0;
    }
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
for(int i=0; i<MONTHS.length;i++) {
        MonthLong.addItem(MONTHS[i]);
    }
pane.add(MonthLong, c);

JComboBox DayLong = new JComboBox();
if (shouldWeightX) {
    c.weightx = 1.0;
    }
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
for(int i=0; i<DAYS.length;i++) {
    DayLong.addItem(DAYS[i]);
}
pane.add(DayLong, c);

JComboBox YearLong = new JComboBox();
if (shouldWeightX) {
    c.weightx = 1.0;
    }
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
c.gridwidth = 1;
for(int i=0; i<YEARS.length;i++) {
    YearLong.addItem(YEARS[i]);
}
YearLong.setSelectedItem("2013");
pane.add(YearLong, c);
public static final String[]MONTHS={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”、“十月”、“十一月”,
“12月”};
公共静态最终字符串[]天={“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“11”,
"12", "13", "14", "15", "16", "17", "18", "19", "20", 
"21", "22", "23", "24", "25", "26", "27", "28", "29", 
"30", "31"};
公共静态最终字符串[]年={“2015”、“2014”、“2013”、“2012”、“2011”、“2010”};
论坛注意:这不是全部代码。我刚刚提供了这个问题所需的信息。
JLabel start=新的JLabel(“开始日期:”);
if(shouldldwertx){
c、 权重x=0.5;
}
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=0;
c、 gridy=1;
c、 网格宽度=1;
窗格。添加(开始,c);
JComboBox MonthLong=新JComboBox();
if(shouldldwertx){
c、 权重x=0;
}
c、 填充=GridBagConstraints.HORIZONTAL;
c、 gridx=1;
c、 gridy=1;
c、 网格宽度=1;

对于(inti=0;i您应该使用java.util.Calendar类获取当前时间部分

Calendar now = Calendar.getInstance();
// months start with 0 
System.out.println("Year is: " + now.get(Calendar.YEAR));   
System.out.println("Month is: " + (now.get(Calendar.MONTH) + 1));
System.out.println("Date is: " + now.get(Calendar.DATE));
然后可以在初始化步骤中在组合框上设置SelectedIndex

还请记住,您不必使用for循环将这些字符串添加到组合框中,只要尝试一下即可

JComboBox<String> MonthLong = new JComboBox<String>(MONTHS);
JComboBox MonthLong=新JComboBox(月);

我没有测试这段代码

    Calendar now = Calendar.getInstance();
    int month = now.get(Calendar.MONTH);
    int day = now.get(Calendar.DAY_OF_MONTH);
    int year = now.get(Calendar.YEAR);

    MonthLong.setSelectedIndex(month);
    DayLong.setSelectedItem(Integer.toString(day));
    YearLong.setSelectedItem(Integer.toString(year));

在Java中,变量通常以小写字母开头。

这很有效!非常感谢。另外,我知道变量的小写字母,我正在为其他人编辑一些代码,但在这个问题中,我说它是“我的”。哇!再次感谢。