gpt4 book ai didi

java - 数组中的下一个和上一个按钮每次按下都会在数组中循环太多次(超过一次)

转载 作者:太空宇宙 更新时间:2023-11-04 06:50:44 24 4
gpt4 key购买 nike

这两个按钮在数组中循环,按下时会增加多次,具体取决于最大索引是什么。我认为这可能是因为(至少向后我有“我设置为最大索引”,但更改为当前索引会停止按钮的全部功能。

btnprev.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = dataArrayMaxIndex; i >= 0; i-- ) {
System.out.println("backwards");
dataArrayCurrentIndex = i;
websitetxt.setText(dataArray[i].getWebsitename());
usernametxt.setText(dataArray[i].getUsername());
passwordtxt.setText(dataArray[i].getPassword());
notestxt.setText(dataArray[i].getNotes());
}
}
});

btnnext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for (int i = 0; i<dataArrayMaxIndex; i++) {
System.out.println("foward");
dataArrayCurrentIndex = i;
websitetxt.setText(dataArray[i].getWebsitename());
usernametxt.setText(dataArray[i].getUsername());
passwordtxt.setText(dataArray[i].getPassword());
notestxt.setText(dataArray[i].getNotes());
}
}
});

我不确定是否可以解决此问题,并且可以使用一些帮助和建议。我觉得不给自己答案会更有帮助,但有一些建设性的批评来引导自己找到答案,也就是说,如果这是你的事,请随意发布一个直接的、有效的答案。

最佳答案

我认为您的 for 循环不属于此代码,因为每次按下按钮时您都会循环到末尾或一直循环到开头。相反,只需增加或减少索引变量并使用该索引即可。我假设您将使用 dataArrayCurrentIndex 来实现此功能。

即,

btnprev.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dataArrayCurrentIndex--;

// if you want to do a cyclic loop of the data
if (dataArrayCurrentIndex < 0) {
dataArrayCurrentIndex= maxIndex - 1;
}

System.out.println("backwards");
websitetxt.setText(dataArray[dataArrayCurrentIndex].getWebsitename());
usernametxt.setText(dataArray[dataArrayCurrentIndex].getUsername());
passwordtxt.setText(dataArray[dataArrayCurrentIndex].getPassword());
notestxt.setText(dataArray[dataArrayCurrentIndex].getNotes());
}
});

btnnext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dataArrayCurrentIndex++;

// if you want to do a cyclic loop of the data
if (dataArrayCurrentIndex >= maxIndex) {
dataArrayCurrentIndex = 0;
}

// etc...
}
});

关于java - 数组中的下一个和上一个按钮每次按下都会在数组中循环太多次(超过一次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com