• 120 likes • 324 Views
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)
E N D
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
Example: Craps Application (adapted from Fig 6.8) 76 public class Craps extends JFrame implements 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)
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)
Init() method that uses Flow Layout public void init(){ Container c = getContentPane(); c.setLayout( new FlowLayout() ); setTitle("The Sum of all Craps"); //110 die1Label = new JLabel( "Die 1" ); c.add( die1Label ); firstDie = new JTextField( 5 ); firstDie.setEditable( false ); 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.
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
Registering an event handler. //145 roll = new JButton( "Roll Dice" ); roll.addActionListener( this ); 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
actionPerformed() (implicitly invoked) //166 call method play when button is pressed public void actionPerformed( ActionEvent e ){ 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 sumOfDice = rollDice(); • play() explicitly called from actionPerformed
rollDice() //265 roll the dice public int rollDice(){ int die1, die2, workSum; die1 = 1 + ( int ) ( Math.random() * 6 ); die2 = 1 + ( int ) ( Math.random() * 6 ); workSum = die1 + die2; firstDie.setText( Integer.toString( die1 ) ); secondDie.setText( Integer.toString( die2 ) ); sum.setText( Integer.toString( 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