一个JTextField存储和更新文本,另一个不';T

一个JTextField存储和更新文本,另一个不';T,text,jtextfield,Text,Jtextfield,因此,我创建了一个类,它接受数组并从中计算值。然后我决定(不知不觉地)将其合并到GUI界面中。一切顺利,直到我注意到这个奇怪的错误;其中一个jtextfields(prarie)不存储文本,而另一个(yard)存储文本 我环顾四周,发现我的问题与我在这个网站上的问题相似; 但他有一个根本不起作用,我有一个起作用,一个不起作用 代码就在这里(虽然有点长,但大部分都是GUI),所以请屏住呼吸 import java.awt.GridLayout; import javax.swing.Box; i

因此,我创建了一个类,它接受数组并从中计算值。然后我决定(不知不觉地)将其合并到GUI界面中。一切顺利,直到我注意到这个奇怪的错误;其中一个jtextfields(prarie)不存储文本,而另一个(yard)存储文本

我环顾四周,发现我的问题与我在这个网站上的问题相似;

但他有一个根本不起作用,我有一个起作用,一个不起作用

代码就在这里(虽然有点长,但大部分都是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());



    }
}
导入java.awt.GridLayout;
导入javax.swing.Box;
导入javax.swing.JCheckBox;
导入javax.swing.JLabel;
导入javax.swing.JOptionPane;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
公共类窗口{
/**
*@param args
*/
私有静态int-numb;
专用静电双微,厘;
私人静态JTextField[]码,prarie;
私人静态双[]电荷,距离;
公共静态void main(字符串[]args)
{
//创建一个小对话框窗口,以接收带电对象的数量
JPanel startup=newjpanel();
JTextField many=新的JTextField(5);
启动。添加(多个);
int result=JOptionPane.showConfirmDialog(null,启动,“请输入正在评估的带电对象的数量”,JOptionPane.OK\u CANCEL\u选项);
many.requestFocusInWindow();
//单击“确定”后,数字输入将存储在“麻木”下
//然后继续输入字段
if(result==JOptionPane.OK\u选项)
{
numb=Integer.parseInt(many.getText());
输入字段();
}
}
//此窗口打开各种JTextFields以进行输入
公共静态void inputFields()
{
//顶级JTextFields
码=新JTextField[numb];
JPanel chargePanel=新的JPanel();
对于(int x=0;x变化:

meterPanel.add(new JTextField(5));
致:


在prarie文本字段的for循环中

我想它就是这么简单的。谢谢你指出!
meterPanel.add(prarie[x]);