在JAVA中怎么让文本框显示选择框的内容

 我来答
lzs丶灬
2017-01-12 · TA获得超过632个赞
知道小有建树答主
回答量:881
采纳率:100%
帮助的人:517万
展开全部
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class DateSelect extends JFrame {
    JTextField resultText;
    JComboBox yearComboBox, monthComboBox, dayComboBox;
    DefaultComboBoxModel day30ComboBoxModel,day31ComboBoxModel,day28ComboBoxModel,day29ComboBoxModel;

    public DateSelect() {
        this.setTitle("");
        this.setSize(230, 100);

        DefaultComboBoxModel yearComboBoxModel = new DefaultComboBoxModel();
        DefaultComboBoxModel monthComboBoxModel = new DefaultComboBoxModel();
        day30ComboBoxModel = new DefaultComboBoxModel();
        day31ComboBoxModel = new DefaultComboBoxModel();
        day28ComboBoxModel = new DefaultComboBoxModel();
        day29ComboBoxModel = new DefaultComboBoxModel();
        for (int i = 1900; i <= 2999; i++) {
            yearComboBoxModel.addElement(i);
        }
        for (int i = 1; i <= 12; i++) {
            monthComboBoxModel.addElement(i);
        }
        for (int i = 1; i <= 31; i++) {
            day31ComboBoxModel.addElement(i);
            if(i <= 30) day30ComboBoxModel.addElement(i);
            if(i <= 29) day29ComboBoxModel.addElement(i);
            if(i <= 28) day28ComboBoxModel.addElement(i);
        }

        yearComboBox = new JComboBox(yearComboBoxModel);
        yearComboBox.setSelectedIndex(116);
        monthComboBox = new JComboBox(monthComboBoxModel);
        monthComboBox.setSelectedIndex(0);
        dayComboBox = new JComboBox(day31ComboBoxModel);
        dayComboBox.setSelectedIndex(0);

        JPanel mainPanel = new JPanel();

        resultText = new JTextField(12);
        resultText.setEnabled(false);

        ItemListener changeItemListener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    String result = yearComboBox.getSelectedItem()+"年"+monthComboBox.getSelectedItem()+"月"+dayComboBox.getSelectedItem()+"日";
                    resultText.setText(result);
                }
            }
        };

        ItemListener selectItemListener = new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                //如果选中了一个
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    //这里写你的任务 ,比如取到现在的值
                    int daySelectIndex = dayComboBox.getSelectedIndex();
                    switch (monthComboBox.getSelectedIndex()+1) {
                        case 4:
                        case 6:
                        case 9:
                        case 11:{
                            dayComboBox.setModel(day30ComboBoxModel);
                            dayComboBox.setSelectedIndex(0);
                            break;
                        }
                        case 2:{
                            int year = (int)yearComboBox.getSelectedItem();
                            if(year%4==0 && year%100!=0){
                                dayComboBox.setModel(day29ComboBoxModel);
                            }else{
                                dayComboBox.setModel(day28ComboBoxModel);
                            }
                            dayComboBox.setSelectedIndex(0);
                            break;
                        }
                        default:{
                            dayComboBox.setModel(day31ComboBoxModel);
                            dayComboBox.setSelectedIndex(0);
                            break;
                        }
                    }
                    dayComboBox.setSelectedIndex(daySelectIndex);
                }
            }
        };

        //监听选中
        monthComboBox.addItemListener(selectItemListener);
        yearComboBox.addItemListener(selectItemListener);
        //监听修改
        monthComboBox.addItemListener(changeItemListener);
        yearComboBox.addItemListener(changeItemListener);
        dayComboBox.addItemListener(changeItemListener);

        mainPanel.add(yearComboBox);
        mainPanel.add(monthComboBox);
        mainPanel.add(dayComboBox);
        mainPanel.add(resultText);

        this.add(mainPanel);
    }

    public static void main(String[] args) {
        DateSelect dateSelect = new DateSelect();
        dateSelect.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        dateSelect.setVisible(true);
    }
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式