1 / 15

Event Handling

Event Handling. The Plan. Sequential (Single Thread) Model Event Model Making the GUI interactive Examples Practice. Sequential (Single Thread) Model. Program Start. Program End. Event Model. Program Thread. AWT Event Loop. Event Model. Program Thread. Event Model. AWT Event

orly
Download Presentation

Event Handling

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. Event Handling

  2. The Plan • Sequential (Single Thread) Model • Event Model • Making the GUI interactive • Examples • Practice

  3. Sequential (Single Thread) Model Program Start Program End

  4. Event Model Program Thread AWT Event Loop

  5. Event Model Program Thread

  6. Event Model AWT Event Loop

  7. Making the GUI Interactive • import java.awt.event.* • implements ActionListener • write methodpublic void actionPerformed(ActionEvent e) • calladdActionListener(this)for allJButtons

  8. Examples GameShell.java AdderGUI.java

  9. Examples AdderGUI.java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AdderGUI extends JApplet implements ActionListener

  10. Examples AdderGUI.java public void actionPerformed(ActionEvent ae) { String addend0Text=addend0.getText(); double addend0Number=Double.parseDouble(addend0Text); String addend1Text=addend1.getText(); double addend1Number=Double.parseDouble(addend1Text); double answer=addend0Number+addend1Number; sum.setText(""+answer); }

  11. Examples AdderGUI.java private void makeComponents() { frame=new JFrame("Game Shell"); addend0=new JTextField(8); addend1=new JTextField(8); sum=new JTextField(8); compute=new JButton("="); compute.addActionListener(this); plus=new JLabel("+"); plus.setHorizontalAlignment(SwingConstants.CENTER); }

  12. GameShell.java Examples import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GameShell extends JApplet implements ActionListener

  13. Examples public void actionPerformed(ActionEvent ae) { Object cause=ae.getSource(); if(cause==pause) { if(pause.getText().equals("Pause")) { pause.setText("Resume"); shell.setText("Paused"); } else { pause.setText("Pause"); shell.setText("Game Running"); } } if(cause==reset) { pause.setText("Start"); shell.setText("Splash"); } } GameShell.java

  14. GameShell.java Examples pause=new JButton("Start"); pause.addActionListener(this); reset=new JButton("Start New Game"); reset.addActionListener(this);

  15. Practice • Make a 2x2 tic-tac-toe board out of initially blank Jbuttons. • Make the JButton text change to X when the user clicks on it. • Make the JButton text change to X and O alternatively as the user clicks on the buttons.Hint: use a boolean instance variable. • Make the fonts larger, and maybe add images. • Look at Splash.java and SplashLoop.java in Splash.jar

More Related