2013-03-11 58 views
0

我試圖讓GUI完全循環通過 ,但在第三個下一個按鈕單擊數組元素[2]後,我遇到了一個錯誤。我需要做的是在點擊下一個按鈕時循環整個事物。一旦到達數組的最後一次迭代就需要回到開始。也就是說,數組按照字母順序排序,所以一旦循環完成,它需要以標題[3]開頭。感謝所有幫助試圖讓GUI循環開始回來的數據

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); 
      } 

    } 


    } 

回答

2

您是通過記錄發展如此迅速的原因是,您已將ActionListenernextJButton兩次

next.addActionListener(this); 

這,反過來,增量您的記錄索引(nb)中的ActionListener以及與您的updateFields方法一樣。從這些位置的一個中刪除此增量。

另外,當您檢查您是否已達到最後一個標題時,您不會重置您的記錄索引nb。你可以這樣做:

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

我看到了這一點,但是刪除它一旦我打冠軍[2]程序崩潰,我發現這很有趣是應該開始的標題[3],而是其他的事情就開始在標題[4]上,所以我開始回到元素0上,第一個顯示的是標題[3],因爲它按字母順序排序,但它跳過其他元素 – Jason 2013-03-11 00:46:47

+0

我明白你在說什麼,並感謝你的支持幫幫我。現在我注意到當我嘗試添加下一個按鈕時,我得到了Actionperformed問題。 – Jason 2013-03-11 00:57:18