Help with Java please Assume you have a GUI program with two text fields, a + bu
ID: 3607532 • Letter: H
Question
Help with Java please
Assume you have a GUI program with two text fields, a + button and a - button. The user types a number into each text field. When they click one of the buttons, either the sum or difference is then calculated and displayed. The user can then no longer click either button. Write the code that would inside of a single handler method used for both buttons. Use the following objects: TextField inputNumber1TextField TextField inputNumber2TextField Button plusButton Button minusButton Text resultText For full credit, reduce duplicated code. private void handleBothButtons(ActionEvent event) { // your code here }
Explanation / Answer
Answer:
TextField inputNumber1TextField;
TextField inputNumber2TextField;
Button plusButton;
Button minusButton;
Text resultText;
private void handleBothButtons(ActionEvent event) {
// your code here
int num1 = Integer.parseInt(inputNumber1TextField.getText());
int num2 = Integer.parseInt(inputNumber2TextField.getText());
if(event.getSource() == plusButton) {
resultText.setText(""+(num1 + num2));
minusButton.setEnabled(false);
} else {
resultText.setText(""+(num1 - num2));
plusButton.setEnabled(false);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.