1 / 15

Datalogi A 7: 20/10

Datalogi A 7: 20/10. CS A. Event driven programs. Many things may happen: Pressing a button Selecting values with sliders, lists etc. Type text in fields Mouse clicks on a canvas Timers Menus Window closing, resizing. JEventQueue. My little trick…

tiva
Télécharger la présentation

Datalogi A 7: 20/10

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. Datalogi A 7: 20/10 CS A

  2. Event driven programs Many things may happen: • Pressing a button • Selecting values with sliders, lists etc. • Type text in fields • Mouse clicks on a canvas • Timers • Menus • Window closing, resizing

  3. JEventQueue My little trick… Usual event-driven programming in Swing requires: • Inheritance • Interface • Inner classes

  4. JEventQueue JEventQueue events = new JEventQueue(); events.listenTo(button1,"button1"); events.listenTo(button2,"button2"); ... while(true){ EventObject event = events.waitEvent(); String name=events.getName(event); if(name.equals("button1")){ ... }else if(name.equals("button2")){ ... }else ... }

  5. Example

  6. Event loop JEventQueue events=new JEventQueue(); events.listenTo(button1,"celcius"); events.listenTo(button2,"fahrenheit"); while(true){ EventObject event=events.waitEvent(); String name=events.getName(event); if(name.equals("celcius")){ double f=Double.parseDouble(field2.getText()); field1.setText(""+(f-32)/1.8); }else if(name.equals("fahrenheit")){ double c=Double.parseDouble(field1.getText()); field2.setText(""+(c*1.8+32)); } }

  7. Selections: RadioButton: isSelected() CheckBox: isSelected() Spinner: getValue() Slider. getValue() ComboBox: getSelectedItem() List: getSelectedValue()

  8. Text components Generate lots of events: You press ’a’ • Key pressed event • Key typed event • Text field changed event • Key released event

  9. Text Components Ignore most of them except: • Typing a key that corresponds to a character • Pressing a control key: Arrows, Page up/down, function keys etc.

  10. Mouse events Draw circles where you click and lines where you drag. int x0=0,y0=0; while(true){ EventObject event=events.waitEvent(); if(events.isMouseEvent(event)){ int x=events.getMouseX(event); int y=events.getMouseY(event); if(events.isMousePressed(event)){x0=x;y0=y;} if(events.isMouseClicked(event)) canvas.drawOval(x0-5,y0-5,10,10); if(events.isMouseReleased(event)) canvas.drawLine(x0,y0,x,y); } }

  11. Timers startTimer(miliseconds,name) stopTimer(name)

  12. Menus frame.setJMenuBar( events.jmenubar(new Font("Arial",Font.PLAIN,18), events.jmenu("File",'F’, events.jmenuitem("New",'n',events.control('N')), events.jmenuitem("Open",'o',events.control('O')), events.jmenuitem("Save",'s',events.control('S')), events.jmenuitem("Save as",'a', events.control('A')), events.jmenuitem("Exit",'x',events.control('Q')) ), events.jmenu("Edit",'E', .. ), events.jmenu("View",'V', ) ) )

  13. Menus

  14. Menus Mnemonic: Press Alt-F to get File menu then press ’s’ to save Accelerator key: Press Control-s to save

  15. Window events Events when closing or resizing a window Bring up a dialog when you close a window so that the user can confirm whether the user wants to exit the program.

More Related