代码之家  ›  专栏  ›  技术社区  ›  paulaxa1

我的程序有一个国家和州组合框,无论你选择什么,它都只从州组合框中获取第一个值[关闭]

  •  -2
  • paulaxa1  · 技术社区  · 9 年前

    所以我为我的作业创建了一个程序,它创建一个包含学生数据的顺序文件。我制作了一个国家和州组合框,根据国家来对应的州,但在顺序文件(.dat文件)中,无论我选择什么州,在顺序文件中,州总是第一个。例如,如果选择澳大利亚和塔斯马尼亚州,则顺序文件中的州将是新南威尔士州。这是我的代码,提前谢谢

    public assignment1st() 
    {
        super("create student file");
    
        try{
            output=new DataOutputStream(new FileOutputStream("studentRec.dat"));
    
        }
          catch ( IOException e )  {
               System.err.println( "File won't open properly/n" +
                 e.toString( ) );
               System.exit( 1 );
        }
    
        initialize();
    
        //*******HERE STARTS THE COUNTRY/STATE COMBOBOX BUILD**************************************
        String[] countries = {"-CHOOSE","Australia","Belgium","Brazil","Canada","Georgia","Greece",
            "India","Lithuania","Macedonia"};
        comboBox_1 = new JComboBox<Object>(countries);
        comboBox_1.addActionListener(this);
        comboBox_1.setBounds(278, 142, 92, 20);
        frame.getContentPane().add(comboBox_1);
    
    
        //  Create sub combo box with multiple models
        //State Combobox
    
        comboBox_2 = new JComboBox<String>();
        comboBox_2.addItem("-CHOOSE-");
        comboBox_2.setBounds(452, 142, 109, 20);
        frame.getContentPane().add(comboBox_2);
        comboBox_2.setPrototypeDisplayValue("XXXXXXXXXX");
    
        String[] Australia = { "New South Wales", "Tasmania", "Queensland" ,"Victoria"};
        states.put(countries[1], Australia);
    
        String[] Belgium = { "Louxembourg", "Hainaut", "Flemish" };
        states.put(countries[2], Belgium);
    
        String[] Brazil = { "Amazonas", "Mato Grosso" };
        states.put(countries[3], Brazil);
    
        String[] Canada = { "Vancouver", "Quebec" };
        states.put(countries[4], Canada);
    
        String[] Georgia = {"Tbilisi", "S.Ossetia" };
        states.put(countries[5], Georgia);
    
        String[] Greece = { "Pelloponisos", "Chalchidikis", "Thesprotias" };
        states.put(countries[6], Greece);
    
        String[] India = {  "Jalpur", "Kolkata", "New Delhi" };
        states.put(countries[7], India);
    
        String[] Lithuania = { "Akmene", "Kretinga", "Varena" };
        states.put(countries[8],Lithuania);
    
        String[] Macedonia = {  "Bitola", "Struga", "Veles" };
        states.put(countries[9], Macedonia);
    
    
    }
    

    导入数据的代码

       if ( studentID > 0 )  {
    
    
    
                    //PLACE FOR COMBOBOXEZ
    
    
                    String sex=(String) comboBox.getSelectedItem();
                    output.writeUTF(sex);
    
                    String country=(String) comboBox_1.getSelectedItem();
                    output.writeUTF(country);
    
    
                    String state=(String) comboBox_2.getSelectedItem();
                    output.writeUTF(state);
    
    
    
                    String month=(String) comboBox_3.getSelectedItem();
                    output.writeUTF(month);
    
                    String day=(String) comboBox_4.getSelectedItem();
                    output.writeUTF(day);
    
                    String year=(String) comboBox_5.getSelectedItem();
                    output.writeUTF(year);
    
    
                    output.writeInt(maths);
                    output.writeInt(buisness);
                    output.writeInt(programming);
                    output.writeInt(accounting);
                    output.writeInt(art);
                    output.writeInt(music);
    

    最后是为国家/地区组合框执行的操作

    public void actionPerformed( ActionEvent e )  { 
    
        //*************FOR STATE AND COUNTRY COMBOBOXEZ*********************
        String country = (String)comboBox_1.getSelectedItem();
            Object o = states.get( country );
    
            if (o == null)
            {
                comboBox_2.setModel( new DefaultComboBoxModel<String>() );
            }
            else
            {
                comboBox_2.setModel( new DefaultComboBoxModel<String>( (String[])o ) );
            }
           //**********DONE WITH THE STATE AND COUNTRY COMBOBOXEZ**********
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   dryairship    8 年前

    这是因为您的这部分代码:

    if (o == null)
    {
        comboBoxstate.setModel( new DefaultComboBoxModel<String>() );
    }
    else
    {
        comboBoxstate.setModel( new DefaultComboBoxModel<String>( (String[])o ) );
    }
    

    在此之前:

    if (e.getSource()==btnEnter){
        addRecord( ) ;
    }
    

    因此,首先重新排列组合框,然后添加记录。

    如果你把第二部分放在第一部分之前,它会完美地工作。就是这样。

    但是,我建议您检查是哪个组件导致了该操作,然后执行您想要执行的操作,否则某些组件上的操作可能会导致其他组件执行的操作。如果你想要,你可以用 e.getActionCommand()