Java FIFO队列显示问题

Java FIFO队列显示问题,java,Java,当向文本字段添加名称时,它是垂直的,但是当我从队列中删除用户时,它会水平变化。其次,我在改变用户的位置方面遇到了问题。当用户从队列中移除时,我希望下面的用户采取他们的立场,例如1。哈里,2岁。汤姆,3岁。格雷格(移除按钮)。 1.汤姆,2岁。Greg.根据我对代码的理解,您没有使用实际索引,而是使用“count”作为参考变量,并在add和remove中显示。检查以下代码以获得预期结果: private static Queue<String> myQ = new LinkedList

当向文本字段添加名称时,它是垂直的,但是当我从队列中删除用户时,它会水平变化。其次,我在改变用户的位置方面遇到了问题。当用户从队列中移除时,我希望下面的用户采取他们的立场,例如1。哈里,2岁。汤姆,3岁。格雷格(移除按钮)。
1.汤姆,2岁。Greg.

根据我对代码的理解,您没有使用实际索引,而是使用“count”作为参考变量,并在add和remove中显示。检查以下代码以获得预期结果:

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);
    }
private静态队列myQ=newlinkedlist();
私有静态JTextField jTextField1=null;
私有静态JTextArea jTextArea1=null;
私有静态void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
String name=jTextField1.getText();//从文本字段获取文本
jTextArea1.setText(“”;//删除文本区域中的所有文本
myQ.add(name);//添加文本字段数据
整数计数=0;
对于(字符串str:myQ){//iterate
jTextArea1.append(count+“”+str+“\n”);//追加到文本区域
计数++;
}
系统输出打印(myQ);
}
私有静态void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
myQ.remove();
系统输出打印(myQ);
jTextArea1.setText(“”);
迭代器it=myQ.Iterator();
整数计数=0;
while(it.hasNext()){
objecte=it.next();
jTextArea1.append(count+“”+e+“\n”);
计数++;
}
}
公共静态void main(字符串[]args){
JFrame f=新的JFrame(“按钮示例”);
jTextField1=新的JTextField();
jTextField1.立根(50,50,150,20);
JButton addUsersButton=新JButton(“添加用户”);
addUsersButton.setBounds(50,100,95,30);
JButton removeUsersButton=新JButton(“删除用户”);
removeUsersButton.setBounds(1501009530);
jTextArea1=新的JTextArea();
jTextArea1.立根(60150200200);
addUsersButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
jButton1ActionPerformed(e);
}
});
removeUsersButton.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
jButton2ActionPerformed(e);
}
});
f、 添加(addUsersButton);
f、 添加(removeUsersButton);
f、 添加(jTextField1);
f、 添加(jTextArea1);
f、 设置大小(400400);
f、 setLayout(空);
f、 setVisible(真);
}

“将名称添加到文本字段时,它是垂直的,但当我从队列中删除用户时,它会水平变化。”-这是什么意思?另外,Javadoc更喜欢在实现队列时使用ArrayQue而不是LinkedList。您可以再添加一点代码吗?我认为垂直->水平是因为您将(“\n”)附加到文本区域的方式不同。jButton2ActionPerformed应该像jButton1ActionPerformed一样迭代元素并附加“\n”。你所说的“改变职位的问题”是什么意思?谢谢大家的回复,所以职位改变的问题是,下面的人应该占据第一个职位,但它所做的是,它将下面的人推到第一个职位,保持他们当前的号码,所以占据第一个职位的人将被删除,人员B将在实际链接列表中占据其位置,但仍将在文本区域中显示为位置2。我想你只有运行上面的程序才能完全理解