1 / 15

Writing GUI Applications

Writing GUI Applications. An introduction with Java. 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

mort
Download Presentation

Writing GUI Applications

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 Interrupt OS Pass message to the appropriate app Key pressed App1 App2 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! Mouse moves Right Click ‘A’ pressed Your application Double Click Escape pressed Your application must be ready for anything and REACT!

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

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

  8. So you write the handlersfor the events! events Your application ‘A’ pressed Double Click Mouse moves onMouseMove() onDoubleClick() onKeyPress() (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 ‘A’ pressed Button 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.

  11. How does that impact you? • 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. • You have to “add” the widgets to the panel. • 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.

More Related