Question: Complete the program, as shown below, that displays head (H) or tail (T) for each of nine coins (represented by a group of JButton). When
Complete the program, as shown below, that displays head (H) or tail (T) for each of nine coins
(represented by a group of JButton). When a cell is clicked, the coin is flipped. A cell is a JButton.
When the program starts, all cells initially display H. (10 marks)
import java.awt.*;
import javax.swing.*;
____1____; //import the package for event handling
public class CoinButtonDemo ____2____ { //inherit JFrame and implements the listener
private JButton[] buttons;
public CoinButtonDemo() {
super("Flipping coins");
____3____; //set the size to 300 * 200
____4____; //set the close operation to EXIT_ON_CLOSE
setLocationRelativeTo(null);
setLayout(____5____); //set the layout manager to GridLayout
buttons = new JButton[9];
for(int i=0; i
buttons[i] = ____6____; //initialize the buttons
add(buttons[i]);
____7____;
//register the listener to the buttons
}
setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
JButton clicked = ____8____; //get the source component
if(Math.random() < 0.5) {
clicked.setText("T");
} else {
clicked.setText("H");
}
}
____9____ {
//method header for the main
____10____;
//create the GUI object
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
