GUI programming Graphical user interfacebased programming Chapter G
GUI programming Graphical user interface-based programming Chapter G 1 (pages 289 -314) 1
Windchill o Windchill n There are several formulas for calculating the windchill temperature twc n The one provided by U. S. National Weather Service and is applicable for a windspeed greater than four miles per hour n Where o Variable t is the Fahrenheit temperature o Variable v is the windspeed in miles per hour 2
Console-based programming Console program Method main() { statement 1; statement 2; . . . statement m; } Console programs begin and end in method main() 3
In use 4
GUI-based programming GUI program begins in method main(). On completion, the event dispatching loop is begun GUI Program main() { GUI gui = new GUI(); } GUI Constructor() { constructor 1; constructor 2; . . . constructorn; } Action Performer() { action 1; action 2; . . . actionk; } Event-dispatching loop Constructor configures the components of the GUI do if an event occurs then signal its action listeners until program ends The event-dispatching loop watches for user interactions with the GUI The action performer implements the task of the GUI. 5
An old prediction of the future… 7
Java support o o o JFrame n Represents a titled, bordered window JLabel n Represents a display area suitable for one or both of a single-line text or image. JText. Field n Represents an editable single-line text entry component JButton n Represents a push button JText. Area n Represents an editable multiline text entry component 8
Instance variables o private JFrame window n References the window containing the other components of the GUI 9
Instance variables o private JText. Area legend. Area n References the text display for the multiline program legend 10
Instance variables o private JLabel fahr. Tag n References the label for the data entry area supplying the temperature 11
Instance variables o private JText. Field fahr. Text n References the data area supplying the temperature 12
Instance variables o private JLabel wind. Tag n References the label for the data entry area supplying the windspeed 13
Instance variables o private JText. Field wind. Text n References the data area supplying the windspeed 14
Instance variables o private JLabel chill. Tag n References the label for the data area giving the windchill 15
Instance variables o private JText. Field chill. Text n References the data area giving the windchill 16
Instance variables o private JButton run. Button n References the “Run” button 17
Class constants o private static final String LEGEND = "This windchill calculator" + "is intended for velocities greater than 4 mph. “ n Program legend text 18
Class constants o private static final int WINDOW_WIDTH = 350 n Initial width of the GUI 19
Class constants o private static final int WINDOW_HEIGHT = 185 n Initial height of the GUI 20
Class constants o private static final int AREA_WIDTH = 40 n Width of the program legend in characters 21
Class constants o private static final int FIELD_WIDTH = 40 n Number of characters per data entry area 22
Class constants o private static final Flow. Layout LAYOUT_STYLE = new Flow. Laout() n References manager that lays out GUI components in a top-to-bottom, left-to-right manner 23
Class constants o Our GUI without a layout manager 24
Class constants o Increased window width changes the layout 25
Program Windchill. java import javax. swing. *; import java. awt. event. *; public class Windchill implements Action. Listener { // class constants // instance variables with initialization // Windchill(): default constructor // action. Performed(): run button action event handler // main(): application entry point } 26
How much will Christmas cost? o o http: //www. pncbank. com/12 days Flash animation (local) 27
Program Windchill. java – class constants private static final int WINDOW_WIDTH = 350; // pixels private static final int WINDOW_HEIGHT = 185; // pixels private static final int FIELD_WIDTH = 20; // characters private static final int AREA_WIDTH = 40; // characters private static final Flow. Layout LAYOUT_STYLE = new Flow. Layout(); private static final String LEGEND = "This windchill " + "calculator is intended for velocities greater than 4 mph. "; 28
Program Windchill. java – instance variables // window for GUI private JFrame window = new JFrame("Windchill Calculator"); // legend private JText. Area legend. Area = new JText. Area(LEGEND, 2, AREA_WIDTH); // user entry area for temperature private JLabel fahr. Tag = new JLabel("Fahrenheit temperature"); private JText. Field fahr. Text = new JText. Field(FIELD_WIDTH); 29
Program Windchill. java – instance variables // user entry area for windspeed private JLabel wind. Tag = new JLabel(" Windspeed (mph)"); private JText. Field wind. Text = new JText. Field(FIELD_WIDTH); // entry area for windchill result private JLabel chill. Tag = new JLabel(" Windchill temperature"); private JText. Field chill. Text = new JText. Field(FIELD_WIDTH); // run button private JButton run. Button = new JButton("Run"); 30
Program Windchill. java – constructor public Windchill() { // configure GUI // register event listener // add components to container // display GUI } 31
Program Windchill. java – constructor public Windchill() { // configure GUI window. set. Size(WINDOW_WIDTH, WINDOW_HEIGHT); window. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); legend. Area. set. Editable(false); . . . 32
Bad line wrapping 33
Program Windchill. java – constructor public Windchill() { // configure GUI window. set. Size(WINDOW_WIDTH, WINDOW_HEIGHT); window. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); legend. Area. set. Editable(false); legend. Area. set. Line. Wrap(true); legend. Area. set. Wrap. Style. Word(true); . . . 34
Dangers of an editable legend 35
Program Windchill. java – constructor public Windchill() { // configure GUI window. set. Size(WINDOW_WIDTH, WINDOW_HEIGHT); window. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); legend. Area. set. Editable(false); legend. Area. set. Line. Wrap(true); legend. Area. set. Wrap. Style. Word(true); legend. Area. set. Background(window. get. Background()); chill. Text. set. Editable(false); chill. Text. set. Background(Color. WHITE); 36
Bad line wrapping A JLabel is noneditable by the user By default the text field of a JText. Field is editable by the user 37
Program Windchill. java – constructor public Windchill() { // configure GUI … // register event listener run. Button. add. Action. Listener(this); 38
Run button action-event handling An Action. Listener has an action. Performer() method that handles the class-specific activity Action events are sent to registered action listeners GUI : Action Listener action. Performer() Method Action Event When the run button is clicked, it dispatches an action event Ÿ Get data entries from temperature and windspeed data areas Ÿ Compute windchill according to the Weather Service formula Ÿ Display result to windchill data area 39
Program Windchill. java – constructor public Windchill() { // configure GUI … // register event listener … // add components to container Container c = window. get. Content. Pane(); c. set. Layout(LAYOUT_STYLE); c. add(legend. Area); c. add(fahr. Tag); c. add(fahr. Text); c. add(wind. Tag); c. add(wind. Text); c. add(chill. Tag); c. add(chill. Text); c. add(run. Button); 40
Program Windchill. java – constructor public Windchill() { // configure GUI … // register event listener … // add components to container … // make GUI visible window. set. Visible(true); 41
Laying out the GUI components Top to bottom, left to right 42
Program Windchill. java – action performer public void action. Performed(Action. Event e) { // get user’s responses // compute windchill // display windchill } 44
Program Windchill. java – action performer public void action. Performed(Action. Event e) { // get user’s responses String response 1 = fahr. Text. get. Text(); double t = Double. parse. Double(response 1); String response 2 = wind. Text. get. Text(); double v = Double. parse. Double(response 2); // compute windchill // display windchill } 45
Program Windchill. java – action performer 46
Program Windchill. java – action performer public void action. Performed(Action. Event e) { // get user’s responses String response 1 = fahr. Text. get. Text(); double t = Double. parse. Double(response 1); String response 2 = wind. Text. get. Text(); double v = Double. parse. Double(response 2); // compute windchill double windchill. Temperature = 0. 081 * (t - 91. 4) * (3. 71*Math. sqrt(v) + 5. 81 - 0. 25*v) + 91. 4; int perceived. Temperature = (int) Math. round(windchill. Temperature); // display windchill } 47
Today’s demotivators 48
Program Windchill. java – action performer public void action. Performed(Action. Event e) { // get user’s responses String response 1 = fahr. Text. get. Text(); double t = Double. parse. Double(response 1); String response 2 = wind. Text. get. Text(); double v = Double. parse. Double(response 2); // compute windchill double windchill. Temperature = 0. 081 * (t - 91. 4) * (3. 71*Math. sqrt(v) + 5. 81 - 0. 25*v) + 91. 4; int perceived. Temperature = (int) Math. round(windchill. Temperature); // display windchill String output = String. value. Of(perceived. Temperature); 49 chill. Text. set. Text(output); }
Program Windchill. java – action performer 50
Method main() public static void main(String[] args) { Windchill gui = new Windchill(); } 51
Another method main() public static void main(String[] args) { Windchill gui 1 = new Windchill(); Windchill gui 2 = new Windchill(); } 52
53
Text version of Wind. Chill. java public class Wind. Chill { public static void main (String args[]) { Scanner stdin = new Scanner(System. in); System. out. println ("Enter temperature in fahrenheit degrees"); double t = stdin. next. Double(); System. out. println ("Enter wind speed in miles per hour"); double v = stdin. next. Double(); double windtemp = 0. 081 * (t - 91. 4) * (3. 71*Math. sqrt(v) + 5. 81 - 0. 25*v) + 91. 4; System. out. println ("The windchill temperature is " + windtemp); } } o Obviously should include legend, comments, etc. 54
Applets o o Java program run within a browser n Implies an applet is run from a web page Applets may not access or modify the file system running the applet A modern applet has JApplet as its superclass n JApplet is part of the swing package An applet does use JFrame o A JApplet has a content pane 55
Applets o o Important inherited methods n init() o Run when browser loads applet n start() o Run by browser to start applet execution n stop() o Run by browser to stop its execution n destroy() o Run by browser immediately before it its ended n paint(Graphics g) o Run by browser to refresh its GUI By default the inherited methods do nothing 56
A simple applet import java. awt. *; import javax. swing. *; public class Display. Quote extends JApplet { public void paint(Graphics g) { g. draw. String("Anyone who spends their life on a " + " computer is pretty unusual. ", 20, 30); g. draw. String("Bill Gates, Chairman of Microsoft", 25, 45); } } 57
Web page – quote. htm <html> <title> Windchill </title> <applet code="Display. Quote. class" width=400 height=300> </applet> </html> 58
- Slides: 56