2013-03-10 82 views
0

我正試圖獲得下一組值,並在點擊下一個按鈕後顯示在JTextFields中。我是編程新手,所以我不確定我錯過了什麼。我想要發生的是,當我點擊下一個按鈕,當窗口顯示下一組值時,現在將顯示在適當的JTextField中,不幸的是,最終發生的事情是什麼都沒有。我嘗試了幾種不同的方式來實現這一目標,至今沒有任何結果。我知道這不是它自己的按鈕,因爲如果我改變旁邊的actionPerformed來說setTitle(「5000」),它會改變窗口內的標題。任何幫助表示讚賞。正在嘗試使用按鈕更改JTextFields

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; 

    int nb = 0; 
    char x = 'y'; 
    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.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(Float.toString(titles[nb].getprice())); 
         rsFee.setText(Double.toString(titles[nb].getRestkFee())); 
         prodValue.setText(Double.toString(titles[nb].getprodValue())); 
         tValue.setText("2636"); 





     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() 
    { 
     itemNumber.getText(); 
     prodName.getText(); 
     year.getText(); 
     unitNumber.getText(); 
     price.getText(); 
     rsFee.getText(); 
     prodValue.getText(); 
     tValue.getText(); 
    } 




    public void actionPerformed(ActionEvent evt){ 
     Object source = evt.getSource(); 
     if (source == next) 
     { 
      nb++; 

     } 

    } 

    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

當你編譯並運行代碼,會發生什麼? – 2013-03-10 21:24:19

回答

2

ActionListener只有一個分配

nb = 1; 

repaint。這不會使用Movies數組titles中的值更新JTextFields。你需要明確地做到這一點。

itemNumber.setText(Double.toString(titles[nb].getItemNum())); 
... 

要做到這一點,你需要讓你的Moviestitles數組類的成員變量,以便它可以從您的ActionListener進行訪問。

此外,你也從來沒有真的更改nb在您的ActionListener的值。因爲它是一個 「下一步」 按鈕,你可能會想增加它:

nb++; 

旁註:

  • Java使用駝峯這將使該方法getitemNumgetItemNum
  • 一個ArrayList會給你更大的靈活性,以便在大小固定的數組上添加Movie標題。
+0

謝謝你的幫助,但我遇到的問題是,當我切換到.setText標題[nb]該程序不再運行,你知道是什麼原因造成的? – Jason 2013-03-10 22:48:59

+0

嗨Reimeus感謝您的幫助,這是我得到的。 線程「main」中的異常java.lang.NullPointerException \t at GUI。 (GUI.java:85) \t at InventoryP5.main(InventoryP5.java:10) – Jason 2013-03-10 23:18:36

+0

好的,我看到你的問題,使'titles'成爲一個類成員變量。查看更新。 – Reimeus 2013-03-10 23:18:37

1

我認爲chaning JTextFields的邏輯是在GUI類的構造函數內。所以除非你創建另一個GUI類的對象,我可以看到它沒有被創建。你的JTextFields不會被更新。

所以基本上解決了這個問題,你必須將你的改變JTextField中的邏輯內的另一個方法例如像

public void updateFields(){ 


    //place your logic code here 

} 

,然後從actionPerformed方法調用此updateFields()方法

public void actionPerformed(ActionEvent evt){ 
    Object source = evt.getSource(); 
    if (source == next) 
    { 
     nb =1; 
    } 
    updateFields(); 
    ///repaint(); /// you don't need the repaint method 
} 

根據你在評論中的問題和據我的理解,把你的標題數組放在構造函數之外,並做下列修改,使它看起來像這樣:

private JTextField tValue; 
private double toValue; 
int nb = 0; //note initialize this to 0 
Movies[] titles = new Movies[9];//this is the line that comes out of the Constructor 
public GUI() 
{ 

///inside the constructor do the following changes 



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


       toValue += (titles[i].gettotalVal()); 
      } 



    //note I will be accessing the 0th position of the array assuming you need the contents of the first objects to be displayed 
    itemNumber = new JTextField(Double.toString(titles[0].getitemNum())); 
    prodName = new JTextField(titles[0].getprodName()); 
    year = new JTextField(Integer.toString(titles[0].getYear())); 
    unitNumber = new JTextField (Integer.toString(titles[0].getunitNum())); 
    price = new JTextField (Float.toString(titles[0].getprice())); 
    rsFee = new JTextField (Double.toString(titles[0].getRestkFee())); 
    prodValue = new JTextField(Double.toString(titles[0].getprodValue())); 
    tValue = new JTextField ("2636"); 
    nb = 0; 
    //if (nb == 0) you don't need a if statement 
    //{ 

    next.addActionListener(this); 

    //} 

///現在的updateFields()方法

public 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(titles[nb].getprice())); 
    rsFee.setText(Double.toString(titles[nb].getRestkFee())); 
    prodValue.setText(Double.toString(titles[nb].getprodValue())); 
    } 
+0

是的,他是正確的,他不需要repaint()方法。這是一個複製粘貼,我會更新我的答案感謝指出。 – 2013-03-10 21:42:44

+0

感謝您的編輯。 1+給你和Reimeus。 – 2013-03-10 21:44:28

+0

由於我正在處理一個數組,我不能在另一個方法中重新顯示當前的[nb]元素,那麼我將如何解決這個事實? – Jason 2013-03-10 22:56:59

相關問題