Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lesson 29: Multiple buttons and action calls 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 RECAP • This results in an event driven program • When main() is finished, the program still is executing the “listening” part of the library object. 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 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 // 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(); } } RECAP The Result RECAP The Files in Notepad The Files in Command Prompt Compiling and class files The Result RECAP // 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 // 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 import java.awt.event.*; class Subtraction implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("The answer is 111"); System.exit(0); } } // GoodBye.java import java.awt.event.*; class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println("Goodbye!"); System.exit(0); } } Many Buttons // TextInput.java Text input 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(); } } // Echo.java import java.awt.event.*; Text input 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); } }