//Plays odds and evens with the computer. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class OddsEvensEmpty extends JFrame { JLabel userScoreL, compScoreL, sumL, userChoiceL, compChoiceL, oddevenL; JTextField userChoiceTF, compChoiceTF, sumTF, oddevenTF, userScoreTF, compScoreTF; JButton _0B, _1B, _2B, _3B, _4B, exitB; ButHandler _0Handler, _1Handler,_2Handler; ButHandler _3Handler; ButHandler _4Handler; ExitButtonHandler exitHandler; private static final int WIDTH = 400; private static final int HEIGHT = 300; public OddsEvensEmpty() { // Create labels userScoreL = new JLabel("your Score: ", SwingConstants.LEFT); //you need to assign compScoreL, sumL, oddevenL, userChoiceL and compChoiceL similarly. //Create four textfields userScoreTF = new JTextField(4); //declare compScoreTF, userChoiceTF, compChoiceTF, sumTF, and oddevenTF //Create the button handlers _0B = new JButton("0"); _0Handler = new ButHandler(0); _0B.addActionListener(_0Handler); //Declare the other buttons and handlers. //Create Exit Button exitB = new JButton("Exit"); exitHandler = new ExitButtonHandler(); exitB.addActionListener(exitHandler); //Set the title of the window setTitle("Odds and Evens (you're even)"); //Get the container Container pane = getContentPane(); //Set the layout pane.setLayout(new GridLayout(3,6)); pane.add(userScoreL); pane.add(userScoreTF); //pane.add the other elements of the game, in the correct order. //set the size of the window and display it setSize(WIDTH,HEIGHT); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); //Set Beginning values; userScoreTF.setText("0"); compScoreTF.setText("0"); } private class ButHandler implements ActionListener { int value; public ButHandler(int x) //we need this since there are mutliple buttons. { value = x; } public void actionPerformed(ActionEvent e) { int rand, sum; //set the userChoice text field with the value of this button. userChoiceTF.setText(value); //Set rand to be a random number for the compChoiceTF. //Make it 0,1,2,3, or 4 //set the compChoice text field accordingly. sum = value + rand; //put the sum in the sum text field if (sum % 2 == 0) { //the user won since they are even, so we must //update their score. //the number is even, so set the oddeven text field. //set the user score to be 1 plus the current user score. } //else the sum is odd. Do something similar. } } private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main( String args[] ) { OddsEvensEmpty oddevenObject = new OddsEvensEmpty(); } }