Page 742 - Introduction to Programming with Java: A Problem Solving Approach
P. 742

                708 Chapter 17 GUI Programming—Component Layout, Additional GUI Components
 Initial display: After 2 moves: After the final move:
   Figure 17.6 Sample session for the TicTacToe program
Player X wins by getting three X’s in a row, 3 X’s in a column, or 3 X’s in a diagonal. Player O wins in the same manner except that O’s are looked at instead of X’s. To get a better handle on all this, see the sample session in Figure 17.6.
Program Details
See the TicTacToe program listinAg ipn Faiggureos 17.P7aDandF17.7Eb.nMohstaofnthce ceodre should make sense already since its structure parallels the structure in our previous GUI programs. We’ll skip the familiar code and focus on the more difficult code.
Note the setLayout method call in Figure 17.7a. It contains a GridLayout constructor call that specifies three rows and three columns. The constructor call does not include horizontal-gap and vertical- gap arguments, so the tic-tac-toe buttons display with no gaps between them.
Now let’s take a look at the Listener class in Figure 17.7b. In particular, note the statement where we get the clicked button and save it in a local variable:
JButton btn = (JButton) e.getSource();
The (JButton) cast operator is used because if there were no cast operator, the compiler would generate an error. Why? Because the compiler would see an Object at the right being assigned into a JButton at the left (it sees an Object at the right because getSource is defined with an Object return type). In this case, since getSource really returns a JButton, it’s legal to cast its returned value to JButton, and that satisfies the compiler and eliminates the error.
Let’s examine the Listener class’s if statement: if (btn.getText().isEmpty())
{
}
btn.setText(xTurn ? "X" : "O");
xTurn = !xTurn;
We first check to ensure that the button is a blank button. We then reassign the button’s label by using a conditional operator. If xTurn holds true, then X is assigned to the button label. Otherwise, O is as- signed to the button label. We then change the value of xTurn by assigning its negated value into it. More



















































































   740   741   742   743   744