Event handling
Download
1 / 15

Event Handling - PowerPoint PPT Presentation


  • 115 Views
  • Updated On :

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Event Handling' - orly


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

The plan l.jpg
The Plan

  • Sequential (Single Thread) Model

  • Event Model

  • Making the GUI interactive

  • Examples

  • Practice


Sequential single thread model l.jpg
Sequential (Single Thread) Model

Program

Start

Program

End


Event model l.jpg
Event Model

Program

Thread

AWT

Event

Loop


Event model5 l.jpg
Event Model

Program

Thread


Event model6 l.jpg
Event Model

AWT

Event

Loop


Making the gui interactive l.jpg
Making the GUI Interactive

  • import java.awt.event.*

  • implements ActionListener

  • write methodpublic void actionPerformed(ActionEvent e)

  • calladdActionListener(this)for allJButtons


Examples l.jpg
Examples

GameShell.java

AdderGUI.java


Examples9 l.jpg
Examples

AdderGUI.java

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class AdderGUI

extends JApplet

implements ActionListener


Examples10 l.jpg
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);

}


Examples11 l.jpg
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);

}


Examples12 l.jpg

GameShell.java

Examples

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class GameShell

extends JApplet

implements ActionListener


Examples13 l.jpg
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


Examples14 l.jpg

GameShell.java

Examples

pause=new JButton("Start"); pause.addActionListener(this);

reset=new JButton("Start New Game"); reset.addActionListener(this);


Practice l.jpg
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