CSEN401 - Computer Programming Lab: Graphical User Interface Window Interfaces Using Swing
CSEN401 - Computer Programming Lab: Graphical User Interface Window Interfaces Using Swing
S. Abdennadher
Swing
S. Abdennadher
Two basic sets of components to implement a graphical user Interface (GUI): Abstract Window Toolkit (AWT) Swing Swing can be viewed as an improved version of AWT. Swing implements a set of GUI components that build on AWT technology. We will use classes from both Swing and AWT.
S. Abdennadher
Event-Driven Programming
A widget (component of a window) can get an event and can call the corresponding processing program. Main implementation issues: How to pass an event to widget? How to specify the event-processing program that will be called? How the event processing program can get details about the event that called it? GUIs are event driven: Generate events when user interacts with GUI, e.g. mouse click, mouse movement, typing in a text eld, . . . Event information stored in object that extends AWTEvent.
S. Abdennadher
S. Abdennadher
S. Abdennadher
What should happen when the user clicks the close-window button? The window should re an event and send it to a listener object. A listener object should have methods that should specify what should happen when events of various kinds are sent to the listener. These methods are called event handlers. The programmer has to dene or redene these event-handler methods.
S. Abdennadher
S. Abdennadher
S. Abdennadher
S. Abdennadher
10
S. Abdennadher
11
import javax.swing.*; public class FirstWindowDemo { public static void main(String[] args)
FirstWindow window1 = new FirstWindow(); window1.setVisible(true); FirstWindow window2 = new FirstWindow(); window2.setVisible(true); } }
S. Abdennadher
12
Inherited Methods from the class JFrame: To give title to a window: setTitle("Second Window"); To give the window a background color: getContentPane().setBackground(Color.BLUE); The class Color contains constants for many of the common colors, e.g. YELLOW, MAGENTA, BLACK , . . . The class Color is in the AWT package.
S. Abdennadher
13
Layout Manager
We can add more than one label to the content pane. How are the labels arranged? The arranging is done by a special kind of object known as a layout manager. Dierent layout managers follow dierent rules. FlowLayout is the simplest layout manager that arranges the components one after the other, going from the left to the right. BorderLayout is a layout manager that places labels into the ve regions NORTH, SOUTH, EAST, WEST and CENTER. GridLayout is a layout manager that arranges components in rows and columns. The add method has only one argument, items are placed from left to right. getContentPane().setLayout(new GridLayout(2,3));
S. Abdennadher
14
S. Abdennadher
15
Adding Buttons
import javax.swing.*; import java.awt.*; public class ButtonD extends JFrame { public static void main(String[] args) ButtonD buttonGUI = new ButtonD(); buttonGUI.setVisible(true); }
public ButtonD() { super(); setSize(400,100); JButton button = new JButton("Red"); getContentPane().add(button); setTitle("Second Window"); getContentPane().setBackground(Color.BLUE); WindowDestroyer myListener = new WindowDestroyer(); addWindowListener(myListener); } }
c
S. Abdennadher
16
S. Abdennadher
17
button1.addActionListener(this); registers this (ButtonDemo) as listener to receive events from the button called button1. An action listener is an object of type ActionListener. It is not A class but it is a property (interface). To make a class into an ActionListener, we need add the statement implements ActionListener to the beginning of the class denition. dene a method named actionPerformed.
S. Abdennadher
18
actionPerfomed Method
In order to be an action listener a class must have a method named actionPerformed. The actionPerformed method is the only method required by the interface ActionListener. The code is typically a branching statement. Often the branching depends on getActionCommand(). public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Red")) getContentPane().setBackground(Color.RED); else if (e.getActionCommand().equals("Black")) getContentPane().setBackground(Color.BLACK); }
S. Abdennadher
19
Mouse Events
The mouse listeners allow you to receive events to process: Button clicks, presses, or releases by the left, middle, or right buttons. Moves and drags. Which Modier keys (shift, control, alt) were down when the event occurred. Notication when the mouse enters or exits the component. Scroll wheel movements. Normally handled for you. Sometimes used with graphics. If you are are drawing your own graphics (eg, on a JComponent or JPanel) and need to know where the user clicks, then you need to know about mouse events. You can easily add a mouse listener to a JComponent or JPanel.
S. Abdennadher
20
MouseListener
The actions that a MouseListener catches: press: one of the mouse buttons is pressed. release: one of the mouse buttons is released. click: a mouse button was pressed and released without moving the mouse. This is perhaps the most commonly used. enter: mouse cursor enters the component. Often used to change cursor. exit: mouse cursor exits the component. Often used to restore cursor. To listen for these events you will use addMouseListener.
S. Abdennadher
21
To get the mouse coordinates: All coordinates are relative to the upper left corner of the component with the mouse listener. int getX() // returns the x coordinate of the event. int getY() // returns the y coordinate of the event. To check for double clicks: Use the following MouseEvent method int getClickCount() // number of mouse clicks
c
S. Abdennadher
22
JPanel
S. Abdennadher
23
JPanel
S. Abdennadher
24
JPanel Class
A GUI is often organized in a hierarchical fashion, with windowlike containers inside of other windowlike containers. JPanel is used to dene subparts of a window. It is a very simple container class that does little more than grouping objects. A JPanel object is analogous to the braces used to combine a number of simpler Java statements into a single larger Java statement. A JPanel objects groups smaller objects, such as buttons and labels into a larger component.
S. Abdennadher
25
S. Abdennadher
26
S. Abdennadher
27
S. Abdennadher
28
S. Abdennadher
29
g.drawPolygon(xCoords, yCoords, pointCt) will draw the outline of the polygon with vertices at (xCoords[0],yCoords[0]), (xCoords[1],yCoords[1]), and ..., (xCoords[pointCt-1],yCoords[pointCt-1]). The third parameter, pointCt, is an integer that species the number of vertices of the polygon. Its value should be 3 or greater. The polygon automatically includes a line from the last point, (xCoords[pointCt-1],yCoords[pointCt-1]), back to the starting point (xCoords[0],yCoords[0]). g.fillPolygon(xCoords, yCoords, pointCt) lls the interior of the polygon with the current drawing color. The parameters have the same meaning as in the drawPolygon() method.
S. Abdennadher
30