0

我试图让 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);
            }

    }


    }
4

1 回答 1

2

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

next.addActionListener(this);

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

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

if (titles[nb] == titles[8]) {
   nb = 0;
   ...
于 2013-03-11T00:31:57.180 回答