k * *q3: Write a public class named ActressListener that implements the ActionLi
ID: 3698770 • Letter: K
Question
k * *q3: Write a public class named ActressListener that implements the ActionListener interface. This class will have a public constructor that takes a 3TextField and a JLabel * as parameters and stores these in instance variables. Override the actionPerformed method * to interpret the text on the JTextField as an x-value (as a double) and display the corresponding y-value on the 3Label for the line with slope -3.03 and y-intercept 1.73. Note that creating classes that implement listener interfaces we can add the same functionality to multiple components by creating multiple instances of this class. For * example, if we want 3 buttons that will compute y-values for this line with x-values from * different text fields we can instantiate this class 3 times and attach each instance to a * different button.Explanation / Answer
import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class ActressListener implements ActionListener { private JTextField textField; private JLabel label; public ActressListener(JTextField textField, JLabel label) { this.textField = textField; this.label = label; } @Override public void actionPerformed(ActionEvent e) { double x = Double.parseDouble(textField.getText()); label.setText(Double.toString((-3.03 * x) + 1.73)); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.