2017-04-04 198 views
-1

向文本字段添加名稱時,它是垂直的,但是當我從隊列中刪除用戶時,它會水平變化。其次,我遇到了改變用戶位置的問題。當用戶從隊列中移出時,我希望下面的用戶採取他們的立場,例如1.哈利,2.湯姆,3.格雷格(刪除按鈕)。 1. Tom,2. Greg。FIFO隊列顯示問題

+0

「當添加一個名字文本字段是垂直然而,當我從隊列中刪除一個用戶,它水平的變化。」 - 這是什麼意思?另外,Javadoc在實現隊列時更喜歡使用ArrayList到LinkedList。 –

+0

您可以添加更多的代碼,我認爲垂直 - >水平正在發生,因爲您對文本區域添加了不同的方式(「\ n」)。 jButton2ActionPerformed應該迭代元素,並以與jButton1ActionPerformed相同的方式附加「\ n」。 「改變立場的問題」是什麼意思? –

+0

感謝您的回覆球員,所以換位置的問題是,下面的人應該佔據第一位置,但是它所做的是將下面的人推到第一位置並保持當前號碼 –

回答

0

根據我對你的代碼的理解,你沒有采取實際的索引,你已經把「count」作爲參考變量,並在添加和刪除中顯示。請檢查下面的代碼爲您預期的結果:

private static Queue<String> myQ = new LinkedList<String>(); 

    private static JTextField jTextField1 = null; 
    private static JTextArea jTextArea1 = null; 

    private static void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 

     String name = jTextField1.getText(); // get text from text field 
     jTextArea1.setText(""); // remove all text in text area 
     myQ.add(name);// add text field data 
     int count = 0; 
     for (String str : myQ) { // iterate 
      jTextArea1.append(count + " " + str + "\n");// append into text area 
      count++; 
     } 
     System.out.print(myQ); 
    } 

    private static void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 

     myQ.remove(); 
     System.out.print(myQ); 
     jTextArea1.setText(""); 
     Iterator it = myQ.iterator(); 
     int count = 0; 
     while (it.hasNext()) { 
      Object e = it.next(); 

      jTextArea1.append(count + " " + e + "\n"); 
      count++; 
     } 
    } 

    public static void main(String[] args) { 

     JFrame f = new JFrame("Button Example"); 
     jTextField1 = new JTextField(); 
     jTextField1.setBounds(50, 50, 150, 20); 

     JButton addUsersButton = new JButton("Add User"); 
     addUsersButton.setBounds(50, 100, 95, 30); 

     JButton removeUsersButton = new JButton("Remove User"); 
     removeUsersButton.setBounds(150, 100, 95, 30); 

     jTextArea1 = new JTextArea(); 
     jTextArea1.setBounds(60, 150, 200, 200); 

     addUsersButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       jButton1ActionPerformed(e); 

      } 
     }); 

     removeUsersButton.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       jButton2ActionPerformed(e); 

      } 
     }); 

     f.add(addUsersButton); 
     f.add(removeUsersButton); 
     f.add(jTextField1); 
     f.add(jTextArea1); 
     f.setSize(400, 400); 
     f.setLayout(null); 
     f.setVisible(true); 
    }