Download presentation
Presentation is loading. Please wait.
Published byMarshall Hall Modified over 8 years ago
1
Lesson 28: More on the GUI button, frame and actions
2
// helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main (String[] args){ JFrame frame = new JFrame("HelloButton"); Container pane = frame.getContentPane(); JButton hello = new JButton("HelloWorld"); pane.add(hello); // for wrapping the frame we could use this command // frame.pack(); frame.setSize(300,200); frame.show(); } The Java Button RECAP
3
The new result The source fileCompiling and running the program The output RECAP
4
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. As a result the program in Lesson 27 does not stop executing but “hangs”. And we have to exit using the program commands in Windows
5
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.
6
An Example // 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(); } Importing the abstract windows toolkit and the Swing library Creating a class called HelloGoodBye this has to be stored in a file named HelloGoodBye.java (case sensitive).
7
An Example // 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(); } The main section. This means we can execute this program Creating a java frame named frame with the label HelloGoodBye The keyword (new) to identify a constructor statement where objects are created
8
An Example // 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(); } A container is a component to which we can add other components. A JFrame is actually a container, but we should not add components directly to a JFrame. Instead, we should add all components to a special container in the JFrame. We use the method getContentPane() to get a reference to JFrame’s special container.
9
An Example // 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(); } Here we create a Javax.swing.JButton object and give it a string which is used as a label. The object is named hello and is a Java button (Jbutton)
10
An Example // 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(); } Creating an object named listener based on the class GoodBye (which we have not created yet). This class will contain the events we want to execute when the button is clicked. Linking the listener object (we could have called this something more descriptive), to our button through the method addActionListener which was inherited from JButton to our button called hello.
11
An Example // 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(); } The method add in the class Container is used to add a component to the JFrame’s content pane (later we will add many components to a frame). Note: For now, we do not tell the container how to arrange this component, nor where this component is to be displayed (we will do this later).
12
An Example // 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(); } The show method from JFrame is used to display the frame. We can set the size of the frame by using the method setSize from JFrame. This method accepts 2 parameters (width, height), which passed in terms of number of pixels.
13
An Example // 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(); } Now we have a button, but no action for the object named listener from the GoodBye class
14
Creating the GoodBye class // GoodBye.java import java.awt.event.*; class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println(“Goodbye !”); System.exit(0); } Importing the awt.event package from the library that contains the various event listing interfaces. This example use the interface ActionListener to indicate that GoodBye contains the method actionPerformed().
15
An Example // GoodBye.java import java.awt.event.*; class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println(“Goodbye !”); System.exit(0); } Here we indicate that the class GoodBye contains all methods specified in the interface ActionListener. That is to say that the class implements the interface ActionListener
16
An Example // GoodBye.java import java.awt.event.*; class GoodBye implements ActionListener{ public void actionPerformed(ActionEvent e) { System.out.println(“Goodbye !”); System.exit(0); } This method is called when the button is clicked. It is executed through the actionPerformed method, which, as we covered before, for a JButton is a mouseclick. In this example we do not use the ActionEvent parameter passed to the method. We will use it in later examples. Prints Goodbye on the screen (not in the frame) Exits the program
17
The Files in NotepadThe Files in Command Prompt Compiling and class files The Result
19
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).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.