单击JTextArea中的按钮进行计算 [英] Calculation with clicking on a button within JTextArea

查看:119
本文介绍了单击JTextArea中的按钮进行计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在texArea中创建这样的计算,我在一个类中进行计算,然后将其传递到另一个类,仅使用单击按钮执行下一个计算。因此,如果我点击m10py,我在TextArea中的任何内容都将减少0.10。我尝试了一切,但它似乎没有工作。它不会抛出任何错误,但TextArea中的总数保持不变。
我执行基本计算的主类。

i am trying to create such calculation within a texArea which i calculate in one class, and i pass this onto a different class to perform the next calculation with only clicking buttons. So if i click on 'm10py' , whatever i have in the TextArea will be decreased by 0.10. I tried everything but it just doesnt seem to work. It doesnt throw any errors but the total in the TextArea stays still. My main class where it performs the basic calculation.

 public void actionPerformed(ActionEvent e) {

    double frontM = 9.50;
    double middleM = 7.30;
    double backM = 6.70;
    double vipM = 12.90;

    //double total1 = Double.parseDouble(output.getText());


    if (frontR.isSelected() && e.getSource() == calculate) {

        double total = Integer.parseInt(frontT.getText()) * frontM;

        output.setText(pounds.format(total));

    } else if (middleR.isSelected() && e.getSource() == calculate) {

        double total = Integer.parseInt(middleT.getText()) * middleM;

        String total2 = String.valueOf(total);

        output.setText(total2);

    } else if (backR.isSelected() && e.getSource() == calculate) {

        double total = Integer.parseInt(backT.getText()) * backM;
        output.setText(pounds.format(total));

    } else if (vipR.isSelected() && e.getSource() == calculate) {

        double total = Integer.parseInt(vipT.getText()) * vipM;
        output.setText(pounds.format(total));

    } else if (e.getSource() == cancel) {

        frontT.setText("1");
        middleT.setText("1");
        backT.setText("1");
        vipT.setText("1");
        output.setText("");

    }
    if (e.getSource() == payment) {
        Payment paymentJ = new Payment();
        paymentJ.output.setText(output.getText());

    }

}

第二类计算将传递给另一个textArea。我没有为每个按钮做这个,因为我无法计算..

Second class which the calculation is passed on to a different textArea. I didnt do it for every button because i couldnt manage to the calculation.. ;

public void actionPerformed(ActionEvent e) {
    Main main = new Main();
    //  double totalR = Double.parseDouble(output.getText());

    String cost = main.output.getText();
    double cost2 = Double.parseDouble(cost);
    double total;
    total = cost2;

    double m10py = 0.10;
    double m20py = 0.20;
    double m50py = 0.50;
    double m1p = 1.00;
    double m2p = 2.00;
    double m5p = 5.00;
    double m10po = 10.00;
    double m20po = 20.00;

    if (e.getSource() == m10p) {

        total = total - m10py;

        String total2 = String.valueOf(total);

        output.setText(total2);

    }

}

我是这是相当新的所以请不要去找我。我只需要知道这有什么问题。谢谢

I'm fairly new to this so please dont go have a go at me. I just need to know what is wrong with this. Thanks

推荐答案

知道的一件事是你有一个严重的参考问题正在进行中。例如,在您发布的第一个actionPerformed方法中:

One thing I do know is that you've got a serious reference problem going on. For example in your first posted actionPerformed method:

if (e.getSource() == payment) {
    Payment paymentJ = new Payment(); // ***** line 1 ****
    paymentJ.output.setText(output.getText());  // ***** line 2 ****
}

第1行在上面你创建了一个名为paymentJ的支付对象,在第2行你通过调用 output.setText(...)来改变它的状态。我猜测输出是一些文本组件,你试图更改它显示的文本,但这是问题 - 虽然paymentJ指的是Payment对象,但它不是正在显示的Payment对象,这是一个完全独特的独立对象,并通过尝试更改其显示的文本来更改此处创建的未显示的对象的状态,将对实际显示的Payment对象中的输出文本组件没有任何影响。

On line 1 above you create a new Payment object, called paymentJ, and on line 2 you change its state, by calling output.setText(...). I'm guessing that output is some text component, and you're trying to change the text that it displays, but here's the problem -- while paymentJ refers to a Payment object, it's not the Payment object that is being displayed, which is a completely distinct separate object, and changing the state of the non-displayed one created here by trying to change the text it displays, will have no effect on the output text component in the actualy displayed Payment object.

同样在你的第二个发布的actionPerformed方法中:

Similarly in your second posted actionPerformed method:

Main main = new Main();  
//  double totalR = Double.parseDouble(output.getText());

String cost = main.output.getText(); // ***** line 1 ****
double cost2 = Double.parseDouble(cost); // ***** line 2 ****

在上面的第1行,你创建一个 new 主要对象,称为cost,在第2行,通过调用 output.getText()来查询其状态。但是,此处创建的Main实例再次与显示的Main对象不同,这意味着您至少有两个(或更多)Main对象,其中只显示其中一个,从这里本地创建的数据中提取的数据不会反映对显示的数据所做的更改。您可以在提取文本后通过放置println来测试它,例如:

On line 1 above you create a new Main object, called cost, and on line 2 you query its state, by calling output.getText(). But again the Main instance created here is not the same Main object that is being displayed, and again this means that you have at least two (or more) Main objects, only one of which is being displayed, and the data that your extracting from the one created locally here will not reflect the changes made to the one that's displayed. You can test this by placing a println after you extract the text, for example:

Main main = new Main();  
//  double totalR = Double.parseDouble(output.getText());

String cost = main.output.getText(); 
System.out.println("cost is currently: " + cost); // ***** add this ****
double cost2 = Double.parseDouble(cost); 

我敢打赌,你会看到返回的文本组件所保留的默认值,以及不是用户输入的值或显示在当前可视化的主GUI中的值。

I will bet that you'll see a default value that is held by the text component returned, and not a value that was entered by the user or was displaying in the currently visualized Main GUI.

怎么办?


  • 一个人可以使输出字段静态。这将是一个快速简单的解决方案,但不幸的是它会快速,简单和非常错误,因为这会破坏OOP原则,使您的代码很难测试,增强和继承。

  • 最好是在需要的地方传递引用,例如将对显示的 Payment对象的引用传递给具有第一个actionPerformed方法的对象,然后在该对象上调用适当的方法,同样将对显示的 Main对象的有效引用传递给其代码显示在较低代码段中的对象。这将允许您查询和修改有效显示对象的状态。这该怎么做?如果没有更好的工作代码示例(根据我的评论),我无法具体告诉您如何做到这一点。通常,您可以使用构造函数和setter方法参数传递引用。

  • 最好是使您的代码更像MVC或模型 - 视图 - 控制器,但这对于此程序可能有点过头了并且目前可能超出了您当前的编码级别。

  • Well for one, you could make the output fields static. That would be a quick and easy solution, but unfortunately it would be quick, easy and very very wrong, since this would break OOPs principles, making your code very difficult to test, enhance and inherit.
  • Better would be to pass references in where needed, for instance pass a reference to the displayed Payment object into the object that has that first actionPerformed method, and then call the appropriate methods on that object, and likewise pass a valid reference to the displayed Main object into the object whose code is displayed in your lower code snippet. This will allow you to query and modify the states of valid displayed objects. How to do this? I can't tell you specifically how to do this without a better and working code example from you (as per my comments). Generally, you could pass references around using constructor and setter method parameters.
  • Best would be to make your code more M-V-C or Model-View-Controller like, but this may be overkill for this program and may be beyond your current level of coding at this time.

如需更多帮助,请提出更好的帮助。

For more help, for better help, please improve your question.

根据您的新代码,


  • 您的Payment类应该扩展JDialog,而不是JFrame GUI只应有一个主窗口

  • 您需要通过付款paymenetJ =新付款(此); 将Main传递给付款

  • 您需要更改付款构造函数以接受此操作:公共付款(主要主要)

  • 在构造函数中使用参数来设置字段: this.main = main;

  • 然后使用此主要字段而不是创建一个新的Main对象。

  • your Payment class should extend JDialog, not JFrame since a GUI should only have one main window
  • You will want to pass Main into Payment via Payment paymenetJ = new Payment(this);
  • You will need to change the Payment constructor to accept this: public Payment(Main main)
  • And inside the constructor use the parameter to set a field: this.main = main;
  • Then use this main field instead of creating a new Main object.

这篇关于单击JTextArea中的按钮进行计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆