Calculation with clicking on a button within JTextArea

后端 未结 1 822
耶瑟儿~
耶瑟儿~ 2021-01-17 01:01

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 clic

相关标签:
1条回答
  • 2021-01-17 01:50

    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 ****
    }
    

    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.

    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 ****
    

    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); 
    

    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.

    What to do?

    • 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.


    Based on your new code,

    • 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.
    0 讨论(0)
提交回复
热议问题