2017-04-04 197 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); 
    }