GUI Chapter 10 Graphics context and objects Creating
GUI Chapter 10 Graphics context and objects Creating a window based application JFrame, JText. Field, JButton Containers and Controls Graphics commands Layout Managers Event Handlers
Terms GIU component container coordinates graphics context
Types of interfaces Console applications n Using print and Scanner A simple GUI n Using Joption. Pane A complete GUI using a Frame n n Can be created using AWT Can be create using Swing
The swing versus the AWT part of JDK v 1 n n Mapped to platform specific components Heavyweight Swing introduced in V 2 n n Platform independent components Lightweight
Components JButton tn. OK = new JButton(“OK”); JLabel tn. OK = new JLabel(“OK”); JText. Field tf. NAME = new JText. Field(“Enter Name”); JCheck. Box cb. OK = new JText. Box(“OK”); JCombo. Box cmb = new JCombo. Box(new String[] {“Red”, ”Green”, Blue”});
The Class chart JComponent n Abstract. Button w Jbutton w JToggle. Button n JCheck. Box JText. Component n JText. Field JLabel JPanel … many others …
Classes Container Classes JFrame JDialog JApplet JPanel Helper Classes n n Graphics, Color, Font. Metrics Layout. Manager
Frames Public class myframe { public static void main(String[] args) { JFrame yframe = new JFrame(“My. Frame”); myframe. setsize(300, 300); myframe. setvisible=true; myframe. setdefaultcloseoperation(Jframe. EXIT_ ON_CLOSE); } }
Frames with components (limited example) Import javax. swing. *; Public class myframe { public static void main(String[] args) { JFrame yframe = new JFrame(“My. Frame”); java. awt. Container mycnt = frame. get. Content. Pane(); Jbutton bt. OK = new JButton(“OK”); mycnt. add(bt. OK); } } myframe. setsize(300, 300); myframe. setvisible=true; myframe. setdefaultcloseoperation(Jframe. EXIT_ON_CLOSE);
Getting the screen size Dimension Scr. S = Toolkit. get. Default. Toolkit(). get. Screen. Size(); int wid = screensize. width; int hgt = screensize. height; This can be used to center or dynamically size frame. Frame. setlocation(x, y); Frame. setwidth(wid); Frame. sethight(hgt);
Layout Managers Use to determine how components will be positioned on the frame or panel. To create and apply a layout manager to a component. Layout. Manager Lm = new Flow. Layout(); Frame. set. Layout(LM);
Type of layout managers Flow Grid Border Others …. Properties. set. HGap(10); . set. VGap(10); . set. Alignment(Flow. Layout. RIGHT);
Creating a subclass of Frame Import javax. swing. *; Import java. awt. *; Public class theframe extends Jframe { Public theframe() { Container mycnt = get. Content. Pane(); mtcnt. set. Layout(new Flow. Layout()); Jbutton bt. OK = new JButton(“OK”); mycnt. add(bt. OK); } public static void main(String[] args) { theframe myframe = new theframe(); } } myframe. settitle(“Frame subclass”); myframe. setsize(300, 300); myframe. setvisible=true; myframe. setdefaultcloseoperation(Jframe. EXIT_ON_CLOSE);
The color Class Color c = new Color(100, 100) The color constructor expects: Color(R, G, B) Red, Green, Blue 0 -255 btn. OK. set. Foreground( c );
The Font class Font ( name, Style, size) Font F = new Font(“Serif”, Font. BOLD + Font. ITALIC, 12); btn. OK. set. Font(F);
Drawing shapes drawline drawrect fillrect drawarc fillarc drawstring draw 3 drect drawroundedrect drawpolygon
Making a window based application import javax. awt. *; import javax. swing. *; public class My. Window extends JFrame { public My. Window() { super(“My Window”); set. Size(400, 200); set. Visible(true); } public void paint(Graphics g) { super. paint(g); g. set. Color(Color. red); g. fill. Rect(20, 100, 100); } public static void main( String args[]) { My. Window app 1=new My. Window(); app 1. set. Default. Close. Operation( JFrame. EXIT_ON_CLOSE); } }
Event Handlers Event-driven programming A change in the state of a GUI component triggers a call to an event handler method Event source is the object that the user interacts with to trigger the event. Event handler is an object with an event handler method designed to handle a specific event type.
Implementing Event Handlers 1 – Code a class for the event handler. The class must implement the correct listener. n This can be the same class file as out application runs in. 2 – Create an event handler object from the class. 3 – Register the handler object with the GUI component.
Event Handlers – Step 1 Create a class to implement the Handler public class My. Program extends JFrame implements Action. Listener { … /** This method will be invoked when a button is clicked */ public void action. Performed(Action. Event e) { System. out. println("The " + e. get. Action. Command() + " button is " + "clicked atn " + new java. util. Date(e. get. When())); } … }
Event Handlers – Step 2 Create event handler object //Button. Listener bt. Listener = new Button. Listener(); This step may be skipped if we are using the Program class as the event object.
Event Handlers – Step 3 Register the event handler object with the source object. In this example a button. … private JButton jbt. Ok = new JButton("OK"); jbt. Ok. add. Action. Listener(this); …
Example import javax. swing. *; import java. awt. event. *; public class Test. Action. Event extends JFrame implements Action. Listener { // Create two buttons private JButton jbt. Ok = new JButton("OK"); private JButton jbt. Cancel = new JButton("Cancel"); public Test. Action. Event() { // Set the window title set. Title("Test. Action. Event"); // Set Flow. Layout manager to arrange the components inside the frame get. Content. Pane(). set. Layout(new Flow. Layout()); // Add buttons to the frame get. Content. Pane(). add(jbt. Ok); get. Content. Pane(). add(jbt. Cancel); // Create a listener object //Button. Listener bt. Listener = new Button. Listener(); // Register listeners jbt. Ok. add. Action. Listener(this); jbt. Cancel. add. Action. Listener(this); } /** Main method */ public static void main(String[] args) { Test. Action. Event frame = new Test. Action. Event(); frame. set. Default. Close. Operation(JFrame. EXIT_O N_CLOSE); frame. set. Size(100, 80); frame. set. Visible(true); } /** This method will be invoked when a button is clicked */ public void action. Performed(Action. Event e) { System. out. println("The " + e. get. Action. Command() + " button is " + "clicked atn " + new java. util. Date(e. get. When())); } }
Getting and Setting the value of a Text. Field Getting a value from a Textfield n n String s; s= The. Text. Field. get. Text(); Setting the value in a Text. Field n The. Text. Field. set. Text(“A value”);
Summary AWT and Swing API Containers – JFrame Labels, Text. Fields and Buttons Layout Managers Graphics commands Handling Events Layout Managers
- Slides: 25