Event Handling - PowerPoint PPT Presentation

About This Presentation
Title:

Event Handling

Description:

Look at Splash.java and SplashLoop.java in Splash.jar Event Handling ... Model Event Model Event Model Event Model Making the GUI Interactive import java.awt ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 16
Provided by: dukeEdu7
Category:
Tags: event | handling | java

less

Transcript and Presenter's Notes

Title: Event Handling


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
  1. import java.awt.event.
  2. implements ActionListener
  3. write methodpublic void actionPerformed(ActionEve
    nt e)
  4. call addActionListener(this) for all JButtons

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 addend0Textaddend0.getText() d
ouble addend0NumberDouble.parseDouble(addend0Text
) String addend1Textaddend1.getText() doubl
e addend1NumberDouble.parseDouble(addend1Text)
double answeraddend0Numberaddend1Number sum.
setText(""answer)
11
Examples
AdderGUI.java
private void makeComponents() framenew
JFrame("Game Shell") addend0new
JTextField(8) addend1new JTextField(8) sum
new JTextField(8) computenew
JButton("") compute.addActionListener(this)
plusnew JLabel("") plus.setHorizontalAlignme
nt(SwingConstants.CENTER)
12
Examples
GameShell.java
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
causeae.getSource()
if(causepause)
if(pause.getText().equals("Pause"))

pause.setText("Resume")
shell.setText("Paused")
else

pause.setText("Pause")
shell.setText("Game Running")

if(causereset)
pause.setText("Start")
shell.setText("Splash")

GameShell.java
14
Examples
GameShell.java
pausenew JButton("Start")
pause.addActionListener(this) resetnew
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
Write a Comment
User Comments (0)
About PowerShow.com