Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing GUI Applications An introduction with Java.

Similar presentations


Presentation on theme: "Writing GUI Applications An introduction with Java."— Presentation transcript:

1 Writing GUI Applications An introduction with Java

2 Why is it complicated? First the timing of the input is completely different from command-line programming Second the operating system is doing more complicated processing of the data before you get it Third the complexity and variation of the window controls is quite large

3 What is an event? Key pressed, Mouse moves, Mouse Clicks ….. OS App1App2 Key pressed Interrupt OS Pass message to the appropriate app Once the OS decides who should get the event, how does it alert the application?

4 Why is the timing complicated? Previously YOU dictated the input you expected cin << name; cin << address; cin << phonenum; Data had to show up at the input device (standard input) when you wanted it. If it didn’t, your program waited until the data was entered.

5 Now the user decides! Your application ‘A’ pressedRight Click Mouse moves Double Click Escape pressed Your application must be ready for anything and REACT!

6 OS filters what applies to you! Application 1 ‘A’ pressedRight Click Mouse moves Double ClickEscape pressed OS ‘A’ pressed Mouse moves Double Click Right Click Escape pressed Application 2

7 What does your application do now? OS selects what application to contact. Your application decides what handler to contact. Your application ‘A’ pressed Mouse moves Double Click onKeyPress() ‘A’ pressed onMouseMove() Mouse moves onDoubleClick() Double Click

8 So you write the handlers for the events! Your application onKeyPress() ‘A’ pressed onMouseMove() Mouse moves onDoubleClick() Double Click events (actual handler names vary by the software you are using)

9 Application handling of events It is actually more complicated than this. This model will be sufficient to get started. Input is handled in the order it occurs, by the handlers. Your application has to store information to aid it in determining what events have previously occurred if that is relevant Example: a menu “Save” button is not enabled until something has been typed in the input field. The application has to save the current state (edited or not) and react to certain events like clicking “Save” based on other events (a key being pressed in the input field.

10 More on event handling Application Panel Button TextField Input fields and buttons belong to panels, which belong to applications. Events get passed through to the proper “widget” handler via the application and parent widget. ‘A’ pressed

11 How does that impact you? 1.You have to create the widget and handlers by writing the functions to instruct the application on what to so when the key is pressed. 2.You have to “add” the widgets to the panel. 3.You have to activate the panel in the application.

12 Sample java window app import javax.swing.*; public class HelloWorldSwing { public static void main(String[] args) { // 2 create the panel for display JFrame frame = new JFrame("HelloWorldSwing"); // 1 create the “label” widget // no handlers in this example final JLabel label = new JLabel("Hello World"); // 2 add the “label” widget to the panel frame.getContentPane().add(label); // 3 program and activate the frame in the app frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); }

13 Other issues How big is the widget? Where is it positioned? What color is it? What type of fields are possible? There are lots of functions for customizing the widget and it’s behavior.

14 An event handler // Implementation of ActionListener interface. public void actionPerformed(ActionEvent event) { // Parse degrees Celsius as a double and convert to Fahrenheit. int tempFahr = (int) ((Double.parseDouble(tempCelsius.getText())) * 1.8 + 32); fahrenheitLabel.setText(tempFahr + " Fahrenheit"); } Parameter “event” is not used directly (in this example) but can communicate info to the handler: Things like what key was pressed, where was the mouse positioned when the mouse was clicked, etc.

15 Observations Concerning the Lab Much of the code is setting up the windows and the relationships as discussed. Little code required to do the calculation, as you would expect for celsius to fahrenhiet conversion. Pay attention to the creation of widgets, definition of the handler, placement of widgets in the frame, and activation of the frame.


Download ppt "Writing GUI Applications An introduction with Java."

Similar presentations


Ads by Google