如何根据月份和年份填充天组合框?

时间:2018-12-26 20:09:00

标签: java swing date netbeans combobox

private void buildMonthsList(cmbMonth monthsList) {
    for (int monthCount = 0; monthCount < 12; monthCount++)
        monthsList.addItem(Const.MONTHS[monthCount]);
}

public boolean DaysComboBox (int year)
{
    Calendar cal = Calendar.getInstance();
    int months = cal.get(Calendar.MONTH);

    year = (int) cmbYear.getSelectedItem();
    boolean leap = false;

    if(year % 4 == 0)
    {
        if( year % 100 == 0)
        {
            // year is divisible by 400, hence the year is a leap year
            if ( year % 400 == 0)
            {
                leap = true;
            }
            else {
                leap = false;
            }
        }
        else {
            leap = true;
        }
    }
    else {
        leap = false;
    }

    return leap;
}

我在学校里有一个重要的Java Swing程序需要帮助。

如何根据月份和年份(包括leap年)填写天数?我使用了3个单独的组合框,一个组合用于几天,另一个组合用于几个月,另一个组合用于多年。还应该从方法中调用它。

1 个答案:

答案 0 :(得分:3)

“最佳”解决方案是利用可用功能。

Java 8+引入了var tablerows = document.getElementById('table').rows.length; var table = document.getElementById('table'); var cart = document.getElementById('cart'); var subtotal = document.getElementById('subtotal'); var username = document.getElementById('username').innerHTML; var pricesub = document.getElementById('pricefooter'); // On load cart window.onload = function wowzers(){ var array = []; var sum = 0; // Get Data var xhr = new XMLHttpRequest(); xhr.open('GET', 'pricing/orders/' + username +'/api', true); xhr.onload = function(){ var data = JSON.parse(this.response); if(xhr.status >= 200 && xhr.status < 400){ for(x in data){ for(key in data[x]){ array.push(Number(data[x][key])); sum+=Number(data[x][key]); subtotal.innerHTML = sum; row = cart.insertRow(-1); // Delete Data row.addEventListener('click', function deleterow(){ index = this.rowIndex; $.post('pricing/orders/delete', { delete_item: index }); cart.deleteRow(index); subtotal.innerHTML = sum-Number(cart.rows[index].cells[1].innerHTML); }); cell1 = row.insertCell(0); cell2 = row.insertCell(1); cell3 = row.insertCell(2); cell1.innerHTML = key; cell2. innerHTML = data[x][key]; cell3. innerHTML = "<button class='btn btn-danger'>Delete</button>" } } console.log(sum); }else{ console.log(error) } } xhr.send() } //Dynamic Cart for(x = 0; x < tablerows; x++){ table.rows[x].addEventListener('click', addCartItem); } function addCartItem(ev){ var array = []; var priceData = {}; var sum = 0; index = this.rowIndex; equipmentCell = table.rows[index].cells[0]; priceCell = table.rows[index].cells[1]; equipmentName = equipmentCell.innerHTML; equipmentPrice = priceCell.innerHTML; // Post Data $.post('/pricing/orders/' + username + '/api', { javascript_data: JSON.stringify({[equipmentName]:equipmentPrice}) }); cartrow = cart.insertRow(-1); // Delete Data cartrow.addEventListener('click', function deleterow(){ index = this.rowIndex; subtotal.innerHTML = sum-Number(cart.rows[index].cells[1].innerHTML); $.post('pricing/orders/delete', { delete_item: index }); cart.deleteRow(index); }); cell1 = cartrow.insertCell(0); cell2 = cartrow.insertCell(1); cell3 = cartrow.insertCell(2); cell1.innerHTML= equipmentName; cell2.innerHTML = equipmentPrice; cell3.innerHTML = "<button class='btn btn-danger'>Delete</button>"; // Open Api information var xhr = new XMLHttpRequest(); xhr.open('GET', 'pricing/orders/' + username +'/api', true); xhr.onload = function(){ var data = JSON.parse(this.response); if(xhr.status >= 200 && xhr.status < 400){ for(x in data){ for(y in data[x]){ array.push(Number(data[x][y])); sum+=Number(data[x][y]); subtotal.innerHTML = sum; } } }else{ console.log(error); } } xhr.send(); } API,它取代了基于java.timeCalendar的API

例如,使用YearMonth类的类似内容……

Date

将打印...

for (int year = 2010; year <= 2020; year++) {
    YearMonth ym = YearMonth.of(year, Month.FEBRUARY);
    System.out.println(year + " = " + ym.lengthOfMonth());
}

由此,您可以简单地创建一个新的2010 = 28 2011 = 28 2012 = 29 2013 = 28 2014 = 28 2015 = 28 2016 = 29 2017 = 28 2018 = 28 2019 = 28 2020 = 29 ,用所需的值填充并将其应用于ComboBoxModel的实例-有关更多详细信息,请参见How to Use Combo Boxes