2012-03-29 81 views
0

所以,我創建了一個接受數組並從中計算值的類。然後我決定(不知不覺地)將它合併到GUI界面中。一切順利,直到我注意到這個奇怪的錯誤;其中一個jtextfields(prarie)不會存儲文本,而另一個(碼)會存儲文本。一個JTextField存儲和更新文本,另一個不是

我環顧四周,發現我的問題與本網站上的類似; Updating text in a JTextField

但他有一個完全不工作,在那裏我有一個工程,另一個沒有。

的代碼是在這裏(這是一個有點長,但大部分是GUI),所以屏住呼吸!:

import java.awt.GridLayout; 
import javax.swing.Box; 
import javax.swing.JCheckBox; 
import javax.swing.JLabel; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 


public class Window { 

/** 
* @param args 
*/ 
private static int numb; 
private static double micro, centi; 
private static JTextField[] yard,prarie; 
private static double[] charges,distances; 

public static void main(String[] args) 
{ 
    //create a small dialog window to take in number of charged objects 
    JPanel startup = new JPanel(); 
    JTextField many = new JTextField(5); 
    startup.add(many); 

    int result = JOptionPane.showConfirmDialog(null,startup , "Please Enter How Many Charged Objects are Being Evaluated", JOptionPane.OK_CANCEL_OPTION); 
    many.requestFocusInWindow(); 

    //once ok is clicked, then the number input will be stored under 'numb' 
    //then proceed to inputFields 
    if (result == JOptionPane.OK_OPTION) 
    { 
     numb = Integer.parseInt(many.getText()); 
     inputFields(); 
    } 
} 

//this window opens the various JTextFields for input 
public static void inputFields() 
{ 


    //top JTextFields 
    yard = new JTextField[numb]; 
    JPanel chargePanel = new JPanel(); 
    for(int x=0;x<numb;x++) 
    { 
     yard[x] =new JTextField(5); 
     chargePanel.add(new JLabel("Charge "+ Integer.toString(x+1)+":")); 
     chargePanel.add(yard[x]); 
     chargePanel.add(Box.createVerticalStrut(15)); // a spacer 
    } 

    //bottom JTextFields 
    prarie = new JTextField[numb-1]; 
    JPanel meterPanel = new JPanel(); 
    for(int x=0;x<numb-1;x++) 
    { 
     prarie[x]=new JTextField(5); 
     meterPanel.add(new JLabel("Meters "+ Integer.toString(x+1)+":")); 
     meterPanel.add(new JTextField(5)); 
     meterPanel.add(Box.createVerticalStrut(15)); // a spacer 
    } 

    //JCheckBoxes 
    JCheckBox isMicro = new JCheckBox("Charges are in terms of microCoulombs"); 
    JCheckBox isCm = new JCheckBox("Distances are in terms of centiMeters"); 

    JPanel chechBox = new JPanel(); 
    chechBox.add(isMicro); 
    chechBox.add(Box.createVerticalStrut(20)); 
    chechBox.add(isCm); 

    //Paste them all together into one window 
    GridLayout gufi = new GridLayout(3,1); 
    JPanel host = new JPanel(gufi); 
    host.add(chargePanel); 
    host.add(meterPanel); 
    host.add(chechBox); 
    int result1 = JOptionPane.showConfirmDialog(null, host, "Please Enter Charge and Distance Values", JOptionPane.OK_CANCEL_OPTION); 

    //if ok is clicked, then go to 'printArr()' to print the JTextFields 
    //then go to assign the values from the JTextFields to private double arrays 'yard' and 'prarie' 
    if (result1 == JOptionPane.OK_OPTION) 
    { 
     micro = (isMicro.isSelected())? Math.pow(10, -6): 1; 
     centi = (isCm.isSelected())? .01: 1; 


     printArr(); 
     assign(); 
    } 
} 

//a makeshift method to print the value from the JTextFields 
//to fix the problem of why prarie wouldn't store numbers 
public static void printArr() 
{ 
    System.out.println("Charges are:"); 
    for(int x=0;x<numb;x++) 
     System.out.print(yard[x].getText() + " "); 
    System.out.println("Distances are:"); 
    for(int x=0;x<numb-1;x++) 
     System.out.print(prarie[x].getText() + " "); 
} 

//assigns values from JTextFields to the private double arrays 'yard' and 'prarie' 
public static void assign() 
{ 

    try { 
     charges = new double[numb]; 
     for(int x=0;x<numb;x++) 
      charges[x]=micro*Double.parseDouble(yard[x].getText().trim()); 

     distances = new double[numb-1]; 
     for(int x=0;x<numb-1;x++) 
      distances[x]=centi*Double.parseDouble(prarie[x].getText().trim()); 

    } catch (NumberFormatException e) { 
     e.printStackTrace(); 
     //inputFields(); 
    } 
    calculate(); 
} 
public static void calculate() 
{ 


    JPanel sample = new JPanel(); 
    JTextField whichOne = new JTextField(5); 
    sample.add(whichOne); 

    int result = JOptionPane.showConfirmDialog(null,sample , "Please Enter Which Charged Object thy Wishs For", JOptionPane.OK_CANCEL_OPTION); 
    whichOne.requestFocusInWindow(); 
    if (result == JOptionPane.OK_OPTION) 
    { 
     int target = Integer.parseInt(whichOne.getText()); 



    } 
} 

}

任何人誰運行代碼並花時間輸入虛擬值將會看到'yard'存儲值,'prarie'則不存儲值。爲什麼是這樣?

*我很確定我忽略了明顯的(一如既往)。

回答

0

變化:

meterPanel.add(new JTextField(5)); 

到:

meterPanel.add(prarie[x]); 

for循環的普拉里文本框

+0

我想這是一件這麼簡單。感謝您指出! – Zchpyvr 2012-03-29 23:54:24

相關問題