JAVA HOMEWORK HELP THANK YOU! Question 1 Assume you have a GUI program where the
ID: 3688690 • Letter: J
Question
JAVA HOMEWORK HELP THANK YOU!
Question 1
Assume you have a GUI program where the user types text into one text box, clicks a button, and the text disappears from that text box and appears in a second text box. Write the code that would go inside of the handler/listener for the button. Use these references:
userInputTextField
outputTextField
submitButton
Question 2
Assume you have a GUI program with two text fields, a + button and a - button. The user should type 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/listener for both buttons. Use the following objects:
JTextField inputNumber1TextField
JTextField inputNumber2TextField
JButton plusButton
JButton minusButton
JTextField resultTextField
Explanation / Answer
JAVA HOMEWORK HELP THANK YOU!
Question 1
Assume you have a GUI program where the user types text into one text box, clicks a button, and the text disappears from that text box and appears in a second text box. Write the code that would go inside of the handler/listener for the button. Use these references:
userInputTextField
outputTextField
submitButton
submitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String input = userInputTextField.getText();
userInputTextField.setText (‘’);
submitButton.setText (input);
}
});
Question 2
Assume you have a GUI program with two text fields, a + button and a - button. The user should type 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/listener for both buttons. Use the following objects:
JTextField inputNumber1TextField
JTextField inputNumber2TextField
JButton plusButton
JButton minusButton
JTextField resultTextField
public void actionPerformed(ActionEvent e) {
JTextField resultTextField = new JTextField();
int number1 = Integer.parseInt(inputNumber1TextField.getText());
int number2 = Integer.parseInt(inputNumber2TextField.getText());
if (e.getSource() == plusButton) {
int result = number1 + number 2;
resultTextField.setText (result);
plusButton.setEnabled(false);
minusButton.setEnabled(false);
}
if (e.getSource() == minusButton) {
int result = number1 - number 2;
resultTextField.setText (result);
plusButton.setEnabled(false);
minusButton.setEnabled(false);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.