Java中出生日期选择器错误

时间:2014-12-10 14:54:09

标签: java swing jbutton jcombobox

在过去的几周里,我花了一些时间在一个小型Java项目的出生日期选择器上。我有一切正常,但似乎有一个错误。当我选择一些日期然后切换JComboBoxes去同步并且不会自动更改其他JComboBox中的选定值,直到按下按钮由于某种原因。我认为这与我的JComboBoxes具有不同大小的索引这一事实有关。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*; 
import java.text.SimpleDateFormat;
import javax.swing.event.*;

public class dateTest extends JFrame implements ActionListener,ItemListener{

    JFrame myMainWindow = new JFrame("This is my title");
    JTabbedPane myTabs = new JTabbedPane();
    JPanel  firstPanel = new JPanel(); //a panel for first tab

    //first panel components
    String Day2_tmp[]={"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"};
    JComboBox Day2 = new JComboBox(Day2_tmp);
    String Day3_tmp[]={"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"};
    JComboBox Day3 = new JComboBox(Day3_tmp);
    String Month_tmp[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
    JComboBox Month = new JComboBox(Month_tmp);
    String Day4_tmp[]={"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"};
    JComboBox Day4 = new JComboBox(Day4_tmp);
    Calendar cal = Calendar.getInstance();
    JComboBox Y;
    JComboBox X;
    JButton btnBornOn = new JButton();
    Object dd;
    Object mm;
    Object yyyy;
    //end first panel

    JLabel tst = new JLabel();

    public void runGUI(){
        myMainWindow.setBounds(500, 10, 430, 230); //set position, then dimensions
        myMainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        myMainWindow.setLayout(new GridLayout(1,1));

        createFirstPanel(); //call method to create each panel
        setupYears();   

        myTabs.addTab("First Panel", firstPanel); //add panel //title, image?, panel, tooltip?      
        myMainWindow.getContentPane().add(firstPanel); //adds the tabbedpane to mainWindow      
        myMainWindow.setVisible(true); //make the GUI appear
    }

    public void createFirstPanel(){
        firstPanel.setLayout(null);
        firstPanel.setBackground( new Color(-1) );

        Day2.setLocation(20,50);
        Day2.setSize(100,25);
        Day2.setVisible(false);
        Day2.setEditable(false );
        Day2.addItemListener(this);
        Day2.addActionListener(this);
        Day2.setBackground( new Color(-1) );
        firstPanel.add(Day2);

        Day3.setLocation(20,50);
        Day3.setSize(100,25);
        Day3.setVisible(true);
        Day3.setEditable(false );
        Day3.addItemListener(this);
        Day3.addActionListener(this);
        Day3.setBackground( new Color(-1) );
        firstPanel.add(Day3);

        Day4.setLocation(20,50);
        Day4.setSize(100,25);
        Day4.setVisible(false);
        Day4.setEditable(false );
        Day4.addItemListener(this);
        Day4.addActionListener(this);
        Day4.setBackground( new Color(-1) );
        firstPanel.add(Day4);

        Month.setLocation(160,50);
        Month.setSize(100,25);
        Month.setEditable(false );
        Month.addItemListener(this);
        Month.setVisible(true);
        Month.setBackground( new Color(-1) );
        firstPanel.add(Month);

        btnBornOn.setLocation(250,100);
        btnBornOn.setSize(150,50);
        btnBornOn.setText("DOB DD/MM/YYYY");
        btnBornOn.addActionListener(this);
        btnBornOn.setOpaque(true);
        btnBornOn.setContentAreaFilled(true);
        btnBornOn.setBorderPainted(false);
        btnBornOn.setVisible(true);
        firstPanel.add(btnBornOn);
    }

    public void setupYears(){
        ArrayList<String> years_tmp = new ArrayList<String>();
        for(int years = Calendar.getInstance().get(Calendar.YEAR) ; years>=Calendar.getInstance().get(Calendar.YEAR)-90;years--){
            if(years % 4 == 0){
                years_tmp.add(years+"");
            }
        }

        Y = new JComboBox(years_tmp.toArray());
        Y.setLocation(300,50);
        Y.setSize(100,25);
        Y.setEditable(false );
        Y.setVisible(false);
        Y.addItemListener(this);
        Y.setBackground( new Color(-1) );
        firstPanel.add(Y);

        ArrayList<String> years2_tmp = new ArrayList<String>();

        for(int years2 = Calendar.getInstance().get(Calendar.YEAR) ; years2>=Calendar.getInstance().get(Calendar.YEAR)-90;years2--){
            years2_tmp.add(years2+"");
        }

        X = new JComboBox(years2_tmp.toArray());

        X.setLocation(300,50);
        X.setSize(100,25);
        X.setEditable(false );
        X.addItemListener(this);
        X.setBackground( new Color(-1) );
        firstPanel.add(X);
    }

    public void setDateInBoxes(){
        if(Day2.isVisible()== true){
            Day3.setSelectedItem(Day2.getSelectedItem());
            Day4.setSelectedItem(Day2.getSelectedItem());
        }

        if(Day3.isVisible()== true){
            Day2.setSelectedItem(Day3.getSelectedItem());
            Day4.setSelectedItem(Day3.getSelectedItem());
        }

        if(Day4.isVisible()== true){
            Day3.setSelectedItem(Day4.getSelectedItem());
            Day2.setSelectedItem(Day4.getSelectedItem());
        }

        if(X.isVisible()== true){
            Y.setSelectedItem(X.getSelectedItem());
        }

        if(Y.isVisible()== true){
            X.setSelectedItem(Y.getSelectedItem());
        }
    }

    public void actionPerformed(ActionEvent e){ 
        setDateInBoxes();

        if(e.getSource() == btnBornOn){
            dd =Day3.getSelectedItem();
            mm =Month.getSelectedItem();
            yyyy =X.getSelectedItem();
            System.out.println("This person was born on: "+dd+"/"+mm+"/"+yyyy);
        }
    }

    public void itemStateChanged(ItemEvent e){
        if(Month.getSelectedItem().equals("February")){
            Day2.setVisible(false);
            Day3.setVisible(false);
            Day4.setVisible(true);
        }

        if(Month.getSelectedItem().equals("April")){
            Day2.setVisible(true);
            Day3.setVisible(false);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("June")){
            Day2.setVisible(true);
            Day3.setVisible(false);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("September")){
            Day2.setVisible(true);
            Day3.setVisible(false);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("November")){
            Day2.setVisible(true);
            Day3.setVisible(false);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("January")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("March")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("May")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("July")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("August")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("October")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Month.getSelectedItem().equals("December")){
            Day2.setVisible(false);
            Day3.setVisible(true);
            Day4.setVisible(false);
        }

        if(Day4.getSelectedItem().equals("29")){
            Y.setVisible(true);
            X.setVisible(false);
        }else {
            Y.setVisible(false);
            X.setVisible(true);
        }

        if(Day2.isVisible()== true){
            Y.setVisible(false);
            X.setVisible(true);
        }

        if(Day3.isVisible()== true){
            Y.setVisible(false);
            X.setVisible(true);
        }
    }

    public static void main(String[] args){
        dateTest gt = new dateTest();
        gt.runGUI();
    }
}

我希望有人能告诉我问题是什么以及如何解决问题。此外,任何改进建议都会受到欢迎,也会受到批评。另外我很抱歉我输入了大量的代码,但我不知道问题在哪里,所以我不知道要放下什么部分。

0 个答案:

没有答案