简体   繁体   English

我如何获取要读取和计算的第二个文本字段值

[英]How do I get a second textfield value to be read and calculated

I have been stuck for days trying to figure out why I cannot get the value from the inurancevaluetextfield . 我已经被困了好几天,试图弄清楚为什么我不能从inurancevaluetextfield获得值。 I have attached both files so that if anyone wants to run it they can see my problem. 我已经附上了两个文件,因此,如果有人要运行它,他们可以看到我的问题。 I get to the stage where I select Insurance checkbox and enter a value between 100 and 2000, but I keep getting the error message stating my value is not between those amounts. 我进入选择“保险”复选框并输入一个介于100和2000之间的值的阶段,但是我不断收到错误消息,指出我的值不在这些数量之间。 So I don't think the textfield is being read at all. 因此,我认为根本不会读取文本字段。

    public void setInsurancecost(double insurancecost) 
{
    this.insurancecost = insurancecost;    
}

public double getInsurancecost()
{
    if (value < 100)
    {   JOptionPane.showMessageDialog(null, "Insurance can only be purchased for items valued between $100 - $2000.");
    }
    else if (value >= 100)
    {  insurancecost = value * 0.05;
    }
    else if (value > 2000)
    {   JOptionPane.showMessageDialog(null, "Insurance can only be purchased for items valued between $100 - $2000.");
    }
    return insurancecost;
} 

public class TextFieldHandler implements ActionListener
{
    @Override
    public void actionPerformed (ActionEvent e)
    {
        DecimalFormat df = new DecimalFormat("0.00");

        String valueStr=insurancevaluetextField.getText();
        value=Double.parseDouble(valueStr);
    }
}
 private class DoListener implements ActionListener
{
     @Override
                public void actionPerformed(ActionEvent e)
                {
                  DecimalFormat df = new DecimalFormat("0.00");

                  String arg = e.getActionCommand();
                   if (arg.equals("Calculate Insurance"))
                    {
                     System.out.println( "calculate insurance button selected");

                      add (insuranceDisplayArea);  
                      insuranceDisplayArea.setText("The insurance charge for your package is:  $"+df.format(getInsurancecost())+"\n");
                      }                         

You are assigning the variable value at TextFieldHandler . 您正在分配变量valueTextFieldHandler The action performed event of a JTextField will fire at the Enter key event. JTextField的操作执行事件将在Enter键事件时触发。 It will be solved if you assign the variable at the beginning of DoListener.actionPerformed() . 如果您在DoListener.actionPerformed()的开头分配变量,则将解决该问题。 So that only thing you need is to move the code in TextFieldHandler.actionPerformed() to DoListener.actionPerformed() 因此,您只需要将TextFieldHandler.actionPerformed()的代码移动到DoListener.actionPerformed()

I looked at your code. 我看着你的代码。 TheTextFiled handler you have registered with textField only invoked when you press enter key on text field. 您在textField中注册的TextFiled处理程序仅在您在文本字段上按Enter键时才调用。

    TextFieldHandler handler = new TextFieldHandler();
    textField.addActionListener(handler);

Therefore actionPerformed in TextFieldHandler only invoked if you press enter on textField and that moment only your insurancevaluetextField read and set to value. 因此,仅当您在textField上按Enter键时,才会在textFieldHandler中调用actionPerformed,而此时只有您的insurancevaluetextField读取并设置为value。

You can either change the listener so that text filed key change will capture value or just add textfiled read logic into calculate button listener. 您可以更改侦听器,以便文本字段键更改将捕获值,也可以将文本字段读取逻辑添加到计算按钮侦听器中。 ie DoListener 即DoListener

private class DoListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        DecimalFormat df = new DecimalFormat("0.00");

        String weightStr = textField.getText();
        weight = Double.parseDouble(weightStr);

        String valueStr = insurancevaluetextField.getText();
        value = Double.parseDouble(valueStr);
   .... }

I would suggest the changing DoListener. 我建议更改DoListener。

when you are using API read about it. 使用API​​时请阅读有关内容。 Know what they do exactly. 知道他们到底在做什么。 Also debug the program using good IDE. 还可以使用良好的IDE调试程序。 You will spot the error very quickly. 您将很快发现错误。

ActionListener event will occur on TextField only when you type something and hit enter. 仅当您键入内容并按Enter时,ActionListener事件才会在TextField上发生。

this handler will not be raised, insurancevaluetextField.addActionListener (handler); 不会引发此处理程序,insurancevaluetextField.addActionListener(处理程序); its value will be 0, that why the error message was displayed if you dont hit enter after typing 它的值将为0,这就是为什么如果您在键入后不按Enter键就显示错误消息的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM