A Java programmer is designing a user interface and has devised a class that looks like public class MyInterface { public MyInterface() { ... JButton button = new JButton("Click Here!"); } ... void doSomething() { ... } ... } The programmer wants to be sure that doSomething is called whenever someone clicks on the button. Add the necessary code to accomplish this. Use an ActionListener.
Added by Kimberly B.
Step 1
At the top of your Java file, add the following import statements: ```java import javax.swing.JButton; import javax.swing.JFrame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ``` Show more…
Show all steps
Your feedback will help us improve your experience
Paul Gabriel and 78 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
To use the ActionListener interface, it must be implemented by a class. There are several ways to do that. Find them in the following: - Creating a new class - Using the class the graphical component - Using an anonymous inner class - All mentioned above
Paul G.
Akash M.
Write a JavaFX application that presents two buttons and a circle. Every time the button is pushed, the circle should be moved to a new random location within the window.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
Transcript
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD