Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for.

Similar presentations


Presentation on theme: "Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for."— Presentation transcript:

1 Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for a user at a time.  A message dialog can display only one message.  More Common:  Multiple inputs from user at once (name, addr, etc)  Display many values (values of dice, etc).  Use Java's API, Example:  Attach JLabel's to an Application's content pane  use ActionListeners and corresponding actionPerformed

2 Deitel Ch 11-part 1 Java GUIs 2 Example: Craps Application (adapted from Fig 6.8) 76 public class Craps extends JFrame implements 76 public class Craps extends JFrame implements ActionListener { ActionListener {  Although Java supports only single inheritance, a Java class can implement multiple interfaces:  Extends: Craps inherits (data&methods) from JFrame  Implements: Craps implements ALL the behaviors specified in ActionListener: ActionListener specifies an actionPerformed method, Craps must define an actionPerformed method (otherwise, Craps would be abstract)

3 Deitel Ch 11-part 1 Java GUIs 3 *Craps example uses Java API components:

4 Deitel Ch 11-part 1 Java GUIs 4 FlowLayout: Simplest Layout Manager  Method init() (line 106) from Craps example creates GUI component objects and attaches them to a content pane (instance of Container):  GUI components are placed on Container left to right in the order in which attached to Container.  Wrap to next line when edge reached.  Layout set before any components attached to a Container  Other layout managers:  Border (N,S,E,W, Center) and,  Grid (like a 2-D array).  GridBagLayout (most flexible/complex layout manager)

5 Deitel Ch 11-part 1 Java GUIs 5 Init() method that uses Flow Layout public void init(){ public void init(){ Container c = getContentPane(); Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.setLayout( new FlowLayout() ); setTitle("The Sum of all Craps"); setTitle("The Sum of all Craps");//110 die1Label = new JLabel( "Die 1" ); die1Label = new JLabel( "Die 1" ); c.add( die1Label ); c.add( die1Label ); firstDie = new JTextField( 5 ); firstDie = new JTextField( 5 ); firstDie.setEditable( false ); firstDie.setEditable( false ); c.add( firstDie ); c.add( firstDie );  init() creates and attaches GUI components to the user interface  107: c assigned reference to this JFrame’s contentPane  115: c.add() adds component to contentPane as per layout manager.

6 Deitel Ch 11-part 1 Java GUIs 6 Event Driven Programming  Java's use of actionListeners is known as event-driven programming:  User's interaction with GUI "drives" the program  Event handling methods: methods called when an event occurs.  When a Java GUI event occurs: JVM creates object with info about event that occurred program implicitly calls event handling method addActionListener method tells application to listen for action events to respond to an action event, have to define a class that implements ActionListener

7 Deitel Ch 11-part 1 Java GUIs 7 Registering an event handler. //145 roll = new JButton( "Roll Dice" ); //145 roll = new JButton( "Roll Dice" ); roll.addActionListener( this ); roll.addActionListener( this ); c.add( roll ); c.add( roll );  Line 146 registers event handler, specifies that "this" JFrame instance listens for events from JButton "roll". If roll button is pressed by user at runtime:  an ActionEvent instance is created  indicates to JFrame that an action was performed by user on the JButton  program implicitly calls actionPerformed method to process user's interaction, passing the ActionEvent as an argument

8 Deitel Ch 11-part 1 Java GUIs 8 actionPerformed() (implicitly invoked) //166 call method play when button is pressed public void actionPerformed( ActionEvent e ){ public void actionPerformed( ActionEvent e ){ if (e.getSource()== roll) play(); if (e.getSource()== roll) play(); }  JVM implicitly calls actionPerformed method when user presses "roll" button (causing event to occur)  actionPerformed is required to be implemented by Craps class since ActionListener is implemented. //207 public void play() { if ( firstRoll ) { // first roll of the dice if ( firstRoll ) { // first roll of the dice sumOfDice = rollDice(); sumOfDice = rollDice();  play() explicitly called from actionPerformed

9 Deitel Ch 11-part 1 Java GUIs 9 rollDice() //265 roll the dice public int rollDice(){ public int rollDice(){ int die1, die2, workSum; int die1, die2, workSum; die1 = 1 + ( int ) ( Math.random() * 6 ); die1 = 1 + ( int ) ( Math.random() * 6 ); die2 = 1 + ( int ) ( Math.random() * 6 ); die2 = 1 + ( int ) ( Math.random() * 6 ); workSum = die1 + die2; workSum = die1 + die2; firstDie.setText( Integer.toString( die1 ) ); firstDie.setText( Integer.toString( die1 ) ); secondDie.setText( Integer.toString( die2 ) ); secondDie.setText( Integer.toString( die2 ) ); sum.setText( Integer.toString( workSum ) ); sum.setText( Integer.toString( workSum ) ); return workSum; return workSum; }  rollDice(), explicitly called from play(), simulates rolling of two dice, updates GUI textfields and returns to play()  play() completes, and program waits for user to press roll again


Download ppt "Deitel Ch 11-part 1 Java GUIs 1 Java GUIs (Deitel, Chap 14-part1) Focus: Attaching GUI components + event handling  input dialog => only one value for."

Similar presentations


Ads by Google