【问题标题】:Java GUI with Arraylist带有 Arraylist 的 Java GUI
【发布时间】:2015-03-13 22:19:37
【问题描述】:

Java Arraylist 有一个问题:当我单击上一个按钮时,它不会获得最后一个元素的索引。

注意:InfoStudent 是一个单独的类,其中包含所有学生信息,例如 id、姓名和电子邮件;并且 arraylist student 包含新学生的信息。我不确定我的问题是否是像 student.get 这样的索引(获取当前学生的 id 和负 1,因为索引从 0 开始)。我的截图:

我的代码:

JButton btnFirst = new JButton("First");
    btnFirst.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textFieldId.setText(Integer.toString(student.get(0).getId()));
            textFieldName.setText(student.get(0).getName());
            txtEmailbox.setText(student.get(0).getEmail());
        }
    });
    btnFirst.setBounds(10, 215, 89, 22);
    frame.getContentPane().add(btnFirst);

    JButton btnPrev = new JButton("Prev");
    btnPrev.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            current = Integer.parseInt(textFieldId.getText());
            if(current== student.size()){
                current= 0;
                }
            else {
                current = current-1;
            }
                textFieldId.setText(Integer.toString(student.get(current).getId()));
                textFieldName.setText(student.get(current).getName());
                txtEmailbox.setText(student.get(current).getEmail());

            //student.get(current).getId()  is not working 

        }
    });
    btnPrev.setBounds(122, 215, 89, 22);
    frame.getContentPane().add(btnPrev);

    JButton btnNext = new JButton("Next");
    btnNext.setBounds(230, 215, 89, 23);
    frame.getContentPane().add(btnNext);

    JButton btnLast = new JButton("Last");
    btnLast.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textFieldId.setText(Integer.toString(student.get(student.size()-1).getId()));
            textFieldName.setText(student.get(student.size()-1).getName());
            txtEmailbox.setText(student.get(student.size()-1).getEmail());
        }
    });
    btnLast.setBounds(329, 215, 89, 23);
    frame.getContentPane().add(btnLast);
}

【问题讨论】:

  • 如果 current=0 (current = current -1;) 怎么办?您还应该解决这种情况。
  • 您发布的代码中没有ArrayList 甚至没有List 接口。

标签: java arraylist


【解决方案1】:

当前变量是学生 ID,不是数组中的索引 解决它最好的解决方案是在学生数组中添加保存当前索引的变量

【讨论】:

    【解决方案2】:

    改变

            if(current== student.size()){
                current= 0;
                }
            else {
                current = current-1;
            }
    

            if(current < 0 || current >= student.size()){
                current = 0;
                }
            else {
                //Do Not Change the value since it is valid
            }
    

    你可以使用一个字段来存储当前的索引值

    【讨论】:

    • 我去下一个索引的时候怎么样
    • 你需要先检查索引是否有效,就像上面的例子一样
    【解决方案3】:

    首先,不要这样做,btnFirst.setBounds(10, 215, 89, 22);。您应该避免使用空布局和使用setBounds(...) 进行组件放置,因为这会导致非常不灵活的 GUI,虽然它们在一个平台上看起来不错,但在大多数其他平台或屏幕分辨率上看起来很糟糕,并且很难更新和维护.

    接下来,将您当前的索引存储在一个字段中,并根据需要简单地更改其值。

    例如,在下一个:

    currentIndex++;  // increment it
    currentIndex %= myCollection.size(); // mod it so that you don't go out of bounds
    

    在以前,类似:

    currentIndex--;
    currentIndex += myCollection.size(); // mod doesn't work well w/ neg numbers
    currentIndex %= myCollection.size();
    

    【讨论】:

      猜你喜欢
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-28
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2018-10-16
      相关资源
      最近更新 更多