Download presentation
Presentation is loading. Please wait.
1
Multiple buttons and action calls
Lesson 29: Multiple buttons and action calls
2
More on the Swing library
Any program that uses Swing to create a window enters a separate thread of execution that enters an infinite loop, looking for events such as mouse movements, button clicks or key presses. This is like having to workers executing the program: One for the main() One for watching for events This results in an event driven program When main() is finished, the program still is executing the “listening” part of the library object. RECAP
3
More on the Swing library
All Swing components are event sources that can be observed or listened to. To use the event, we need to tell the source which object to notify when an event occurs. By default the JButton reacts to a mouse click by changing its appearance. JButtons are also the source of java.awt.event.ActionEvent objects. An ActionEvent is generated by a button when you click the button with a mouse. Objects that can receive events are called listeners. Different type of events have different listeners. RECAP
4
The steps Create a button with new Jbutton (“some label”)
Get the Container for the Jframe using getContentPane() Add the button to the content pane of the Jframe with add() Create an ActionEventListener class by Adding implements ActionEventListener to the lcass declaration and Defining an actionPerformed method Add the listener object to the list of listeners for the button by calling button.addActionListener(listener), where the button is the name we gave our button, and listener is an instance of the class we created to be executed when the button was clicked (the GoodBye class). RECAP
5
public static void main (String[] args){
RECAP // HelloGoodBye.java import java.awt.*; import javax.swing.*; class HelloGoodBye{ public static void main (String[] args){ JFrame frame = new JFrame("HelloGoodBye"); Container pane = frame.getContentPane(); JButton hello = new JButton("Hello world!!"); GoodBye listener = new GoodBye(); hello.addActionListener(listener); pane.add(hello); frame.setSize(200,75); frame.show(); }
6
The Result The Files in Notepad The Files in Command Prompt
RECAP The Files in Notepad The Files in Command Prompt Compiling and class files
7
The Result RECAP
8
Many Buttons // ManyButtons.java import java.awt.*;
import javax.swing.*; class ManyButtons{ public static void main (String[] args){ JFrame frame = new JFrame("myCalculator"); Container pane = frame.getContentPane(); JButton exit = new JButton("Exit"); GoodBye listener = new GoodBye(); exit.addActionListener(listener); JButton myAddition = new JButton("2+2"); Addition listener1 = new Addition(); myAddition.addActionListener(listener1); JButton mySubtraction = new JButton("123-12"); Subtraction listener2 = new Subtraction(); mySubtraction.addActionListener(listener2); pane.add(exit); pane.add(myAddition); pane.add(mySubtraction); frame.pack(); frame.show(); } Many Buttons
9
Many Buttons // Addition.java import java.awt.event.*;
class Addition implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("The answer is 4"); System.exit(0); } // Subtraction.java class Subtraction implements ActionListener{ System.out.println("The answer is 111"); // GoodBye.java class GoodBye implements ActionListener{ System.out.println("Goodbye!"); Many Buttons
10
Text input // TextInput.java import java.awt.*; import javax.swing.*;
class TextInput { public static void main (String[] args){ JFrame frame = new JFrame("textInput"); Container pane = frame.getContentPane(); JTextField input = new JTextField("enter text and press return"); Echo listner=new Echo(); input.addActionListener(listener); pane.add(input); frame.pack(); frame.show(); }
11
Text input // Echo.java import java.awt.event.*;
class Echo implements ActionListener{ public void actionPerformed(ActionEvent e) { JTextField source =(JTextField)e.getSource(); String text = source.getText(); System.out.println("text"); System.exit(0); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.