Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;“上一次”;按钮不在阵列中循环,“循环”;“下一步”;按钮工作正常_Java - Fatal编程技术网

Java &引用;“上一次”;按钮不在阵列中循环,“循环”;“下一步”;按钮工作正常

Java &引用;“上一次”;按钮不在阵列中循环,“循环”;“下一步”;按钮工作正常,java,Java,我试图让“下一步”和“上一步”按钮在我的程序中工作。“下一步”按钮按预期工作,但我无法获得上一步按钮的正确逻辑。我得到一个错误: 线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中的异常:-1 在书店8$3.actionPerformed(Bookstore8.java:257)” 这是我的书店课程,也许有人可以给我一些建议,如何让“上一个”按钮的逻辑正确?其他三个按钮工作正常。我就是想不出最后一个按钮 /* Declare

我试图让“下一步”和“上一步”按钮在我的程序中工作。“下一步”按钮按预期工作,但我无法获得上一步按钮的正确逻辑。我得到一个错误:

线程“AWT-EventQueue-0”java.lang.ArrayIndexOutOfBoundsException中的异常:-1 在书店8$3.actionPerformed(Bookstore8.java:257)”

这是我的书店课程,也许有人可以给我一些建议,如何让“上一个”按钮的逻辑正确?其他三个按钮工作正常。我就是想不出最后一个按钮

/* Declare Bookstore Class */
public class Bookstore8 extends JFrame
{
  /* Declare Index and Assign to First Part of the Array */
 private static int bookstoreIndex = 0;

 private static Book[] bookInventory = new Book[5]; 

     public static JTextArea prepareDisplay(Book myBook, JTextArea myTextArea){

     myTextArea.setText("");

     myTextArea.append(myBook.toString());

     return myTextArea;
    }

     /* Method to Sort Books */
  public static Book [] sortArray(Book[] books) {

   String[] titles = new String[books.length];

   Book[] sortedBooks = new Book [books.length];

   for (int i = 0; i < books.length; i++){
    titles[i] = books[i].getBookTitle();
   }

   Arrays.sort(titles, String.CASE_INSENSITIVE_ORDER);

   for (int i = 0; i < books.length; i++){
        for (int j = 0; j < titles.length; j++){
             if (books[i].getBookTitle().equalsIgnoreCase(titles[j])) {
                        sortedBooks[j] = books[i];
                        break;
             }
        }
   }

   return sortedBooks;
  }

  /* Method to Calculate Inventory */
  public static double calculateInventoryTotal(Book[] books){
   double total = 0;

   for (int i = 0; i < books.length; i++) {

    total += books[i].getBookPrice();
   }
   return total;
  }


   /* Main Method */
  public static void main (String[] args){

       /* Create Book Objects */
       Book one = new Book ("TD45454545", "My First Java Stuff", "Dan Zaleski", 2014, "UPX", 19.99);
       Book two = new Book ("KY67676767", "Hello From Boston", "Joe Smith", 1999, "Random House", 19.99);
       Book three = new Book ("NCC7890987", "I Hate Onions", "John Cooper", 1990, "Random House", 9.99);
       Book four = new Ebook ("YY00000000", "Yo Dawg", "Jim Fix", 1980, "Penguin", 29.99, "http://amazon.com", 2.99);
       Book five = new Ebook ("HB12345678", "Lottsa Books", "Wille Sargent", 2010, "Wherever", 129.99, "http://amazon.com", 12.99);

       bookInventory[0] = two;
       bookInventory[1] = three;
       bookInventory[2] = five;
       bookInventory[3] = one;
       bookInventory[4] = four;

    /* Declare Number Format */ 
  NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);

     /* Sort Items
   bookInventory = sortArray(bookInventory);*/

     /* Declare JText Class */
    final JTextArea textArea = new JTextArea(10,20);
    textArea.setText("");
    textArea.setEditable(false);


    /* Making Buttons */
    JPanel  buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(1, 3));

    JButton firstButton = new JButton("First");
    buttonPanel.add(firstButton);
    firstButton.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
        bookstoreIndex = 0;
        prepareDisplay(bookInventory[bookstoreIndex], textArea);
       }
  });

    JButton lastButton = new JButton("Last");
    buttonPanel.add(lastButton);
    lastButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
            bookstoreIndex = (bookInventory.length - 1);
            prepareDisplay(bookInventory[bookstoreIndex], textArea);
           }
  });

    JButton prevButton = new JButton("Previous");
    buttonPanel.add(prevButton);
    prevButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bookstoreIndex--;
        if (bookstoreIndex == bookInventory.length - 1) {
            bookstoreIndex=0;
        }
        prepareDisplay(bookInventory[bookstoreIndex], textArea);
      }
    });

    JButton nextButton = new JButton("Next");
    buttonPanel.add(nextButton);
    nextButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        bookstoreIndex++;
        if (bookstoreIndex == bookInventory.length) {
            bookstoreIndex=0;
        }
        prepareDisplay(bookInventory[bookstoreIndex], textArea);
      }
    });

    /* Total Value */
    String totalValue = "Total Value = " + nf.format(calculateInventoryTotal(bookInventory));
    JLabel valuePanel = new JLabel(totalValue);

  /* Add Logo */
    JLabel logoLabel = new JLabel (new ImageIcon("cmdroundlogo600.png"));
    JPanel logoPanel = new JPanel();
    logoPanel.add(logoLabel);

    JPanel centerPanel = new JPanel();
    centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
    centerPanel.add(prepareDisplay(bookInventory[bookstoreIndex], textArea));

    /* Assemble the GUI */
    JFrame frame = new JFrame();
    frame.setLayout(new BorderLayout());
    frame.add(logoPanel, BorderLayout.NORTH);
    frame.add(buttonPanel, BorderLayout.SOUTH);
    frame.add(centerPanel, BorderLayout.CENTER);
    frame.add(valuePanel, BorderLayout.LINE_END);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

     }

}
/*声明书店类*/
公共类书店8扩展JFrame
{
/*声明索引并分配给数组的第一部分*/
私有静态int书店索引=0;
私有静态书籍[]书籍库存=新书[5];
公共静态JTextArea prepareDisplay(Book myBook,JTextArea myTextArea){
myTextArea.setText(“”);
myTextArea.append(myBook.toString());
返回myTextArea;
}
/*图书分类方法*/
公共静态书籍[]sortArray(书籍[]书籍){
String[]titles=新字符串[books.length];
Book[]sortedBooks=新书[books.length];
for(int i=0;i
您在上一个ActionListener中检查了错误的边界,即顶部边界,如下所示:

  public void actionPerformed(ActionEvent e) {
    bookstoreIndex--;

    // this checks if the index is **ABOVE** the range of acceptable
    if (bookstoreIndex == bookInventory.length - 1) {
        bookstoreIndex=0;
    }
    prepareDisplay(bookInventory[bookstoreIndex], textArea);
  }
相反,由于你的递减索引,你需要检查它是否低于底部边界,以检查它是否<0。例如:

  public void actionPerformed(ActionEvent e) {
    bookstoreIndex--;

    // You want to check if the index is **BELOW** the range of acceptable
    if (bookstoreIndex < 0) {
        bookstoreIndex = bookInventory.length - 1;
    }
    prepareDisplay(bookInventory[bookstoreIndex], textArea);
  }
public void actionPerformed(ActionEvent e){
书店索引;
//您要检查索引是否**低于**可接受的范围
如果(书店指数<0
  public void actionPerformed(ActionEvent e) {
    bookstoreIndex--; // ++ for the next button's listener

    bookstoreIndex += bookInventory.length; // in case it's < 0
    bokstoreIndex %= bookInventory.length; // put it in correct range

    prepareDisplay(bookInventory[bookstoreIndex], textArea);
  }