2015-04-28 25 views
-1

我是完全新的Java Swing,我試圖適合我的字典在組合框中,當我開始在JTextField中輸入,並且它從組合框的開始索引鍵入的組合框中搜索它單詞和顯示我開頭的單詞,現在問題是我不能在組合框中添加我的字典項目,我將字典單詞提取到一個字符串數組,然後在ComboBox中添加,但它沒有顯示任何內容,而我能夠添加其中約20件物品,ComboBox的物品空間是否有限?如果是的話,那麼請參考我可以製作這種類型的其他方法。 這裏的我在做什麼無法在Java中的組合框中的項目

package swing; 

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class swing1 extends javax.swing.JFrame { 

    /** 
    * Creates new form swing1 
    */ 
    public swing1() throws FileNotFoundException { 
     initComponents(); 

     FileReader fileread=null; 
      String array[]=new String[107186]; 
      String input; 
      String FileName="English.txt"; 
      fileread = new FileReader(FileName); 

      BufferedReader br = new BufferedReader(fileread); 
      int i=0; 
      try { 
       while((input=br.readLine())!=null) 
       { 
        array[i]=input; 
        i++; 
       } 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 

      this.jComboBox1.addItem(array); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     textField1 = new java.awt.TextField(); 
     jComboBox1 = new javax.swing.JComboBox(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     textField1.setText("textField1"); 

     jComboBox1.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       jComboBox1ActionPerformed(evt); 
      } 
     }); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(159, 159, 159) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)) 
       .addContainerGap(72, Short.MAX_VALUE)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(55, 55, 55) 
       .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(1, 1, 1) 
       .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(204, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {           
     // TODO add your handling code here: 

    }           

    public static void main(String args[]) { 

     try { 
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
       if ("Nimbus".equals(info.getName())) { 
        javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
        break; 
       } 
      } 
     } catch (ClassNotFoundException ex) { 
      java.util.logging.Logger.getLogger(swing1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(swing1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(swing1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(swing1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } 



     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        new swing1().setVisible(true); 
       } catch (FileNotFoundException ex) { 
        Logger.getLogger(swing1.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 
     }); 
    } 

    // Variables declaration - do not modify 

    private javax.swing.JComboBox jComboBox1; 
    private java.awt.TextField textField1; 

} 

我不能上傳圖片,由於我的名聲小於10,但告訴你答案,這讓我這個在組合框中「[Ljava.lang.String; @ 43376」 我正在使用Netbeans並使用拖放操作來製作GUI,並且我還將字典元素放在組合框屬性的模型中,但它沒有給我所期望的結果

幫幫我!

回答

1

我建議你使用

 JComboBox<String> jComboBox1 = new JComboBox<String>(); 
    //loop over your array and add them one by one 
    for(String s:array) 
     jComboBox1.addItem(s); 
+0

感謝名單的人,它的工作,但它讓我有限的元素,我身邊有一本字典1分LAC的話,確實他們適合在組合框中或者我應該用其他的東西! –

+1

1+,@Farrukh,或者您可以使用Array創建DefaultComboBoxModel,然後將該模型添加到組合框。 '我有一本詞典,大約有1個詞 - '我不確定'lac'是什麼,但我猜想組合框的限制是受限於int值的最大值,因爲你需要能夠通過索引訪問數據。如果您認爲自己的單詞太多,請編寫一個將整數對象添加到組合框的循環,以查看可以添加多少個單詞。 – camickr

+0

你是否在組合框中顯示以「鍵入的單詞」開頭的單詞?如果是這樣,你不需要添加整個數組。你可以在你的textField1中添加一個關鍵字偵聽器,然後用給定的限制(combobox的MAXELTS來確定)在「數組」中選擇匹配的字符串,最後將它們添加到你的組合框中。 – Dien