Hello guys. How do you create an ActionListener. I am confused on how to do it.
Here is my code:
Here is my code:
Code:
/** * Written By: Edward Sanger * Coded in: Java * IDE Used: IntelliJ IDEA * Program Name: Calculator * Program Description: This program will create a GUI window with 4 buttons for 4 mathematical operators and 2 text * fields for the numbers to add, subtract, multiply, and divide. * Date: May 26, 2008 * Time: 2:34:52 PM */ //import declarations import java.awt.*; import javax.swing.*; public class Calculator implements ActionListener { public static void main(String[] args) { //create a new jframe JFrame frame = new JFrame(); frame.setTitle("Calculator"); //create a container Container contentPane = frame.getContentPane(); //create the frame objects JTextField num1 = new JTextField("Number 1"); JTextField num2 = new JTextField("Number 2"); JButton add = new JButton("Add"); JButton subtract = new JButton("Subtract"); JButton multiply = new JButton("Multiply"); JButton divide = new JButton("Subtract"); JLabel result = new JLabel("Result"); //add the objects contentPane.add(num1); contentPane.add(num2); contentPane.add(add); contentPane.add(subtract); contentPane.add(multiply); contentPane.add(divide); contentPane.add(result); //create the layout FlowLayout layout = new FlowLayout(); contentPane.setLayout(layout); //create an action listener //frame settings frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }
Comment