ITEC 109 Lecture 27 GUI Interaction Review GUI

  • Slides: 19
Download presentation
ITEC 109 Lecture 27 GUI Interaction

ITEC 109 Lecture 27 GUI Interaction

Review • • • GUI basics JFrame JPanel JLabel, JButton, JText. Field Layout managers

Review • • • GUI basics JFrame JPanel JLabel, JButton, JText. Field Layout managers – Flow / Box – Border / Grid GUI Interaction

Objectives • Add interactivity GUI Interaction

Objectives • Add interactivity GUI Interaction

Gotcha • Interactivity is required with GUIs • print. Now stops working when GUIs

Gotcha • Interactivity is required with GUIs • print. Now stops working when GUIs are introduced in your program… • Labels are the only way to get info GUI Interaction

Need • When you click on a button… • How does your code get

Need • When you click on a button… • How does your code get called – Push – Pull – Both GUI Interaction

Problem Censored • Python doesn’t know how to call your code • Have to

Problem Censored • Python doesn’t know how to call your code • Have to connect the two worlds • When python was written vs when your program was written GUI Interaction

Solution You get a message from whatever got pressed def button. Pressed(event): if (event.

Solution You get a message from whatever got pressed def button. Pressed(event): if (event. get. Source() == a. Button): print. Now(“You pressed a. Button”); a. Button = swing. Jbutton() a. Button. action. Performed=button. Pressed GUI Interaction

Problem • Need to access variable inside of function when it isn’t passed in…

Problem • Need to access variable inside of function when it isn’t passed in… • Solution: global def my. Function(): global x x=4 x=3 my. Function() print. Now(x) GUI Interaction Need god mode

Structure Function that handles presses GUI Components Code that creates the GUI Allows Code

Structure Function that handles presses GUI Components Code that creates the GUI Allows Code that links buttons with function GUI Interaction Code that links buttons with function

Example import javax. swing as swing import java frame = swing. JFrame() frame. set.

Example import javax. swing as swing import java frame = swing. JFrame() frame. set. Size(200, 200) def pressed(event): pane = swing. JPanel() global one frame. set. Title("Press demo") global two one = swing. JButton() global three one. set. Text("+") if (event. get. Source() == one): two = swing. JButton() three. set. Text("+") two. set. Text("-") elif (event. get. Source() == two): three = swing. JLabel() three. set. Text("-") three. set. Text("Hmm. . ") pane. add(one) pane. add(two) pane. add(three) frame. set. Content. Pane(pane) GUI Interaction one. action. Performed = pressed two. action. Performed = pressed frame. show()

Decisions • One handler function or many Function that handles presses Button 1 Button

Decisions • One handler function or many Function that handles presses Button 1 Button 2 Button 3 Button 4 GUI Interaction Press 1 Press 2 Press 3 Button 1 Button 2 Button 3

Design • • • Starts with a conversation Paper prototypes Refined into Java components

Design • • • Starts with a conversation Paper prototypes Refined into Java components Skeleton system Fully functional system GUI Interaction

Warning • GUIs add a lot of complexity to projects • 5000 LOC just

Warning • GUIs add a lot of complexity to projects • 5000 LOC just for a GUI is rather common • Separate part, but it takes on a life of its own GUI Interaction

Tools • What all can you use? – Buttons – Text entry • What

Tools • What all can you use? – Buttons – Text entry • What are other GUI elements? GUI Interaction

Popups • Easy to use, nice for the user swing. JOption. Pane. show. Message.

Popups • Easy to use, nice for the user swing. JOption. Pane. show. Message. Dialog(null, "Eggs are not supposed to be green. "); • Can get input = swing. JOption. Pane. show. Input. Dialog(“Enter an integer: ”); • Can customize buttons GUI Interaction

Radio Buttons • Only one matters • Action listener • JRadio. Button / Button

Radio Buttons • Only one matters • Action listener • JRadio. Button / Button group t = swing. JRadio. Button(“True”, true); f = swing. JRadio. Button(“False”); bg = swing. Button. Group(); bg. add(t); bg. add(f); //Add bg to a panel GUI Interaction You can add a listener to t, f just like with regular JButtons

Images • Stuff inside of a JLabel Same directory as where the. java files

Images • Stuff inside of a JLabel Same directory as where the. java files are icon = swing. Image. Icon(“filename. jpg”); test = swing. JLabel(“Image description”, icon); //Add test to a panel Constants: Swing. Constants. CENTER/LEFT/RIGHT/BOTTOM Image: 3 rd parameter Label text: set. Horizontal/Vertical. Position GUI Interaction

Others • • Sliders/Checkboxes Look at the Javadocs! Learn to learn on your own

Others • • Sliders/Checkboxes Look at the Javadocs! Learn to learn on your own Not easy, but worthwhile GUI Interaction

Summary • • • Interactivity Global Event handler function How to design GUIs Other

Summary • • • Interactivity Global Event handler function How to design GUIs Other components GUI Interaction