gpt4 book ai didi

java - 尝试让 GUI 从头开始​​循环数据

转载 作者:行者123 更新时间:2023-12-02 07:12:33 25 4
gpt4 key购买 nike

我正在尝试让 GUI 完全循环但在第三个下一个按钮单击数组元素 [2] 后遇到错误。我需要做的是在单击“下一步”按钮时让整个过程循环完成。一旦到达数组的最后一次迭代,就需要返回到开头。问题还在于该数组首先按字母顺序排序,因此一旦循环结束,就需要从标题 [3] 开始。感谢大家的帮助

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Arrays;
import javax.swing.*;
import java.awt.*;



public class GUI extends JFrame implements ActionListener {
JButton next;
JButton previous;
JButton first;
JButton last;

private JLabel itmNum = new JLabel("Item Number: ", SwingConstants.RIGHT);
private JTextField itemNumber;
private JLabel proNm = new JLabel ("Product Name: ", SwingConstants.RIGHT);
private JTextField prodName;
private JLabel yr = new JLabel("Year Made: ", SwingConstants.RIGHT);
private JTextField year;
private JLabel unNum = new JLabel("Unit Number: ", SwingConstants.RIGHT);
private JTextField unitNumber;
private JLabel prodPrice = new JLabel("Product Price: ", SwingConstants.RIGHT);
private JTextField price;
private JLabel restkFee = new JLabel("Restocking Fee", SwingConstants.RIGHT);
private JTextField rsFee;
private JLabel prodInValue = new JLabel("Product Inventory Value", SwingConstants.RIGHT);
private JTextField prodValue;
private JLabel totalValue = new JLabel("Total Value of All Products", SwingConstants.RIGHT);
private JTextField tValue;
private double toValue;
Movies[] titles = new Movies[9];
int nb = 0;

public GUI()
{

super ("Inventory Program Part 5");
setSize(800,800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLookAndFeel();




first = new JButton("First");
previous = new JButton("Previous");
next = new JButton("Next");
last = new JButton("Last");
first.addActionListener(this);
previous.addActionListener(this);
next.addActionListener(this);
last.addActionListener(this);




//Movies[] titles = new Movies[9];

titles [0] = new Movies(10001, "King Arthur", 25 , 9.99, 2004, .05);

titles [1] = new Movies(10002,"Tron", 25, 7.99, 1982, .05);

titles [2] = new Movies(10003, "Tron: Legacy",25,24.99,2010,.05);

titles [3] = new Movies(10004,"Braveheart", 25,2.50,1995,.05);

titles [4] = new Movies(10005,"Gladiator",25,2.50,2000,.05);

titles [5] = new Movies(10006,"CaddyShack SE",25,19.99,1980,.05);

titles [6] = new Movies (10007,"Hackers",25,12.50,1995,.05);

titles [7] = new Movies (10008,"Die Hard Trilogy",25,19.99,1988,.05);

titles [8] = new Movies (10009,"Terminator",25,4.99,1984,.05);

Arrays.sort (titles, DVD.prodNameComparator);






itemNumber = new JTextField(Double.toString(titles[3].getitemNum()));
prodName = new JTextField(titles[3].getprodName());
year= new JTextField(Integer.toString(titles[3].getYear()));
unitNumber= new JTextField(Integer.toString(titles[3].getunitNum()));
price= new JTextField(Float.toString(titles[3].getprice()));
rsFee= new JTextField(Double.toString(titles[3].getRestkFee()));
prodValue= new JTextField(Double.toString(titles[3].getprodValue()));
tValue= new JTextField("2636");
nb=0;
next.addActionListener(this);



setLayout(new GridLayout(8,4));
add(itmNum);
add(itemNumber);
add(proNm);
add(prodName);
add(yr);
add(year);
add(unNum);
add(unitNumber);
add(prodPrice);
add(price);
add(restkFee);
add(rsFee);
add(prodInValue);
add(prodValue);
add(totalValue);
add(tValue);
add(first);
add(previous);
add(next);
add(last);
setLookAndFeel();
setVisible(true);


}



public void updateFields()
{
nb++;
itemNumber.setText(Double.toString(titles[nb].getitemNum()));
prodName.setText(titles[nb].getprodName());
year.setText(Integer.toString(titles[nb].getYear()));
unitNumber.setText(Integer.toString(titles[nb].getunitNum()));
price.setText(Double.toString(titles[nb].getprice()));
rsFee.setText(Double.toString(titles[nb].getRestkFee()));
prodValue.setText(Double.toString(titles[nb].getprodValue()));

}




public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if (source == next)
{
if (titles[nb]== titles[8])
{
titles[nb] = titles[0];
}
else {
nb++;
}
updateFields();
}

}

private void setLookAndFeel()
{
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
System.err.println("couln't use the system"+ "look and feel: " + e);
}

}


}

最佳答案

您在记录中进步如此之快的原因是您已将 ActionListener 添加到 next JButton 两次:

next.addActionListener(this);

这反过来会增加 ActionListener 以及 updateFields 中的记录索引 (nb)。方法。从其中一个位置删除此增量。

此外,当您检查是否已到达最后一个标题时,您永远不会重置记录索引nb。你可以这样做:

if (titles[nb] == titles[8]) {
nb = 0;
...

关于java - 尝试让 GUI 从头开始​​循环数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15329392/

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