2012-12-09 46 views
1

誰能告訴我爲什麼我的更改數量按鈕不改變數量? buttonlistener2應該將arrayCount [x] 中的項目數更改爲計數JTextfield中的數字,但它不是,可以使用一組新的眼睛。 感謝所有任何人都可以告訴我爲什麼我的更改數量按鈕不改變數量?

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.ArrayList; 

public class ProduceInventory extends JFrame 
{ 
ArrayList<String> arrayName = new ArrayList<String>(); 
ArrayList<Integer> arrayCount = new ArrayList<Integer>(); 
// declares panels and items 
private final int WINDOW_WIDTH = 450; 
private final int WINDOW_HEIGHT = 350; 

private JPanel panel1; 
private JPanel panel2; 
private JPanel panel3; 

private JLabel messageLabel1; 
private JLabel messageLabel2; 

private JTextField name; 
private JTextField count; 
private JTextArea output; 

private JButton add; 
private JButton changeQTY; 
private JButton delete; 
private JButton clear; 
private JButton report; 
private JButton save; 
private JButton load; 

public ProduceInventory() 
{ 
//creates Jframe 
setTitle("Produce Inventory"); 

setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

setLayout(new BorderLayout()); 

panel1(); //build panels 
panel2(); 
panel3(); 

add(panel1, BorderLayout.NORTH); //add panels 
add(panel2, BorderLayout.SOUTH); 
add(panel3, BorderLayout.CENTER); 
setVisible(true); 
} 
private void panel1() //builds panel 1 
{ 
messageLabel1 = new JLabel("Name"); 
messageLabel2 = new JLabel("Count"); 

name = new JTextField(20); 
count = new JTextField(5); 

    panel1 = new JPanel(); 


panel1.add(messageLabel1); 
panel1.add(name); 
panel1.add(messageLabel2); 
panel1.add(count); 
} 
private void panel2() //builds panel 2 
{ 
add = new JButton("Add"); 
changeQTY = new JButton("CHANGE QTY"); 
delete = new JButton("Delete"); 
clear = new JButton("Clear"); 
report = new JButton("Report"); 
save = new JButton("Save File"); 
load = new JButton("Load File"); 

add.addActionListener(new ButtonListener()); 
changeQTY.addActionListener(new ButtonListener2()); 

panel2 = new JPanel(); 
panel2.setLayout(new GridLayout(2,4)); 

panel2.add(add);  
panel2.add(changeQTY); 
panel2.add(delete); 
panel2.add(clear); 
panel2.add(report); 
panel2.add(save); 
panel2.add(load); 
} 
private void panel3() //builds panel 3 
{ 
output = new JTextArea(10,30); 

panel3 = new JPanel(); 

panel3.add(output); 
} 
private class ButtonListener implements ActionListener //add listener 
{ 
    String nameIn; 
    String countIn; 
    int number; 
    public void actionPerformed(ActionEvent e) 
    { 

     String actionCommand = e.getActionCommand(); 
     if (actionCommand.equals("Add")) 
     { 
     nameIn = name.getText(); 
     arrayName.add(name.getText()); 
     countIn = count.getText(); 
     number = Integer.parseInt(countIn); 
     arrayCount.add(number); 

output.setText(name.getText() + " " + count.getText() + " Item  Added"); 

     for(int x = 0; x <= arrayName.size() - 1; x++) 
      { 

System.out.print((x+1)+ ". " + arrayName.get(x) + " " + arrayCount.get(x) + "\n"); 
      } 

     } 
    } 
} 
private class ButtonListener2 implements ActionListener //add listener 
{ 

    public void actionPerformed(ActionEvent e) 
    { 
     String nameIn; 
     String countIn; 
     int number; 

     String actionCommand = e.getActionCommand(); 
     if (actionCommand.equals("CHANGE QTY")) 
     { 
     nameIn = name.getText(); 
     countIn = count.getText(); 
     number = Integer.parseInt(countIn); 

     for(int x = 0; x <= arrayName.size() - 1; x++) 
     { 
       if(arrayName.get(x) == nameIn) 
       { 
       arrayCount.set(x, number); 
       } 
     } 
     output.setText("Qty is updated to: " + number); 
     for(int x = 0; x <= arrayName.size() - 1; x++) 
      { 

System.out.print((x+1)+ ". " + arrayName.get(x) + " " + arrayCount.get(x) + "\n"); 
      } 

     } 
    } 
} 

public static void main(String[] args) 
{ 
new ProduceInventory(); //runs program 
} 
} 

回答

2

在動作監聽器上,如果兩個變量指向同一個對象,你正在檢查線路

  if(arrayName.get(x) == nameIn) 

。而不是你必須檢查他們所指的對象是否相等:

  if(nameIn.equals(arrayName.get(x))) 
相關問題