动态更改组合框列表,只有一个面板正在工作

时间:2015-04-07 19:06:19

标签: java combobox actionlistener jcombobox

我正在为学校创建一个费用报告计划,我们只能使用书中的内容,所以我不得不妥协。我用comboBoxes制作的日历选择器。我希望日期选项能够随着选择的月份动态变化。由于某种原因,它只适用于returnPanel而不是departPanel。

是的,导入了许多不必要的东西,但我只是保留一个列表,因为事情不断变化。

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class TestWindow extends JFrame {

    private JPanel departPanel;
    private JPanel returnPanel;
    private JComboBox monthsBox;
    private JComboBox daysBox;
    private JComboBox yearsBox;
    private JComboBox departTimeBox;
    private JComboBox returnTimeBox;
    private JLabel calendarLabel;

    private String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                                 "Aug", "Sept", "Oct", "Nov", "Dec" };
    private 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"};
    private String[] years = {"2015"};
    private String[] time = {"1:00AM", "2:00AM", "3:00AM", "4:00AM", "5:00AM",
                                "6:00AM", "7:00AM", "8:00AM", "9:00AM", "10:00AM",
                                "11:00AM", "12:00PM", "1:00PM", "2:00PM", "3:00PM",
                                "4:00PM", "5:00PM", "6:00PM", "7:00PM", "8:00PM",
                                "9:00PM", "10:00PM", "11:00PM", "12:00AM"};

    public TestWindow() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(2,1));

        buildDepartPanel();
        buildReturnPanel();
        add(departPanel);
        add(returnPanel);

        pack();
        setVisible(true);
    }

    private void buildDepartPanel() {

        departPanel = new JPanel();
        monthsBox = new JComboBox(months);
        daysBox = new JComboBox(days);
        yearsBox = new JComboBox(years);
        departTimeBox = new JComboBox(time);

        JLabel departLabel = new JLabel ("Departure");

        departPanel.add(departLabel);
        departPanel.add(monthsBox);
        departPanel.add(daysBox);
        departPanel.add(yearsBox);
        departPanel.add(departTimeBox);

        monthsBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(monthsBox.getSelectedItem().equals("Jan")
                    || monthsBox.getSelectedItem().equals("Mar")
                     || monthsBox.getSelectedItem().equals("May")
                      || monthsBox.getSelectedItem().equals("Jul")
                       || monthsBox.getSelectedItem().equals("Aug")
                        || monthsBox.getSelectedItem().equals("Oct")
                         || monthsBox.getSelectedItem().equals("Dec")) {

                    int items = daysBox.getItemCount();
                    if(items == 29) {
                        daysBox.addItem("30");
                        daysBox.addItem("31");
                    } else if(items == 30) {
                        daysBox.addItem("31");
                    }

                } else if(monthsBox.getSelectedItem().equals("Apr")
                            || monthsBox.getSelectedItem().equals("Jun")
                             || monthsBox.getSelectedItem().equals("Sept")
                              || monthsBox.getSelectedItem().equals("Nov")) {

                    int items = daysBox.getItemCount();
                    if(items == 29) {
                        daysBox.addItem("30");
                    } else if(items == 31) {
                        daysBox.removeItemAt(30);
                    }

                } else if(monthsBox.getSelectedItem().equals("Feb")) {

                    int items = daysBox.getItemCount();
                    if(items == 31) {
                        daysBox.removeItemAt(30);
                        daysBox.removeItemAt(29);
                    } else if(items == 30) {
                        daysBox.removeItemAt(29);
                    }                   
                }
            }
        });

    }

    private void buildReturnPanel() {

        returnPanel = new JPanel();
        monthsBox = new JComboBox(months);
        daysBox = new JComboBox(days);
        yearsBox = new JComboBox(years);
        returnTimeBox = new JComboBox(time);

        JLabel returnLabel = new JLabel ("Return");

        returnPanel.add(returnLabel);
        returnPanel.add(monthsBox);
        returnPanel.add(daysBox);
        returnPanel.add(yearsBox);
        returnPanel.add(returnTimeBox);

        monthsBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                if(monthsBox.getSelectedItem().equals("Jan")
                    || monthsBox.getSelectedItem().equals("Mar")
                     || monthsBox.getSelectedItem().equals("May")
                      || monthsBox.getSelectedItem().equals("Jul")
                       || monthsBox.getSelectedItem().equals("Aug")
                        || monthsBox.getSelectedItem().equals("Oct")
                         || monthsBox.getSelectedItem().equals("Dec")) {

                    int items = daysBox.getItemCount();
                    if(items == 29) {
                        daysBox.addItem("30");
                        daysBox.addItem("31");
                    } else if(items == 30) {
                        daysBox.addItem("31");
                    }

                } else if(monthsBox.getSelectedItem().equals("Apr")
                            || monthsBox.getSelectedItem().equals("Jun")
                             || monthsBox.getSelectedItem().equals("Sept")
                              || monthsBox.getSelectedItem().equals("Nov")) {

                    int items = daysBox.getItemCount();
                    if(items == 29) {
                        daysBox.addItem("30");
                    } else if(items == 31) {
                        daysBox.removeItemAt(30);
                    }

                } else if(monthsBox.getSelectedItem().equals("Feb")) {

                    int items = daysBox.getItemCount();
                    if(items == 31) {
                        daysBox.removeItemAt(30);
                        daysBox.removeItemAt(29);
                    } else if(items == 30) {
                        daysBox.removeItemAt(29);
                    }                   
                }
            }
        });

    }



    public static void main(String[] args) {
        new TestWindow();
    }
}t

2 个答案:

答案 0 :(得分:0)

实例变量(monthsBox,daysBox等等)维护对JComboBox的引用,在调用buildDepartPanel时创建,并再次在buildReturnPanel中创建。调用ActionListener时,它使用当前引用(在此上下文中是最后创建的引用 - 在buildReturnPanel中)。可以通过多种方式修复,例如为表示离开和返回面板的这些对象创建实例变量。鉴于两种构建方法之间发生的代码重复,您最好创建一个通用方法或类来合并重复的代码。例如:

public class DatePanel extend JPanel{
    //instance variables here
    public DatePanel(){
         //construct UI
    }
}

然后只需创建此类的实例并将其添加到您的UI

DatePanel returnPanel = new DatePanel();
add(returnPanel);

答案 1 :(得分:0)

您的组合框是字段,这意味着它们处于对象级别。每个TestWindow实例只有一个。当你调用buildReturnPanel()方法时,它会覆盖你在buildDepartPanel()方法中设置的值,所以当调用你的动作监听器时,它们只会看到返回的组合框。

理想情况下,因为您的离开和返回面板是相同的,我建议为UI项目设置一个单独的类并将其实例化两次。我不知道你的任务是否可行。如果你不能,那么你可以在方法中声明组合框为final:

final JComboBox monthsBox = new JComboBox(months);
...

使用final将让匿名的ActionListener类看到变量。