//Plays odds and evens with the computer. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class OddsEvens extends JFrame { JLabel userScoreL, compScoreL, sumL, userChoiceL, compChoiceL, oddevenL; JTextField userChoiceTF, compChoiceTF, sumTF, userScoreTF, compScoreTF; JTextArea oddevenTF; 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 OddsEvens() { // Create four labels userScoreL = new JLabel("your Score: ", SwingConstants.LEFT); compScoreL = new JLabel("my Score: ", SwingConstants.LEFT); sumL = new JLabel(" Sum ",SwingConstants.LEFT); oddevenL = new JLabel(" odd/even ", SwingConstants.LEFT); userChoiceL = new JLabel("your choice: ", SwingConstants.LEFT); compChoiceL = new JLabel(" my choice: ", SwingConstants.LEFT); //Create four textfields userScoreTF = new JTextField(4); compScoreTF= new JTextField(4); userChoiceTF = new JTextField(4); compChoiceTF = new JTextField(4); sumTF = new JTextField(4); oddevenTF = new JTextArea(4,20); //Creat the button handlers _0B = new JButton("0"); _0Handler = new ButHandler(0); _0B.addActionListener(_0Handler); _1B = new JButton("1"); _1Handler = new ButHandler(1); _1B.addActionListener(_1Handler); _2B = new JButton("2"); _2Handler = new ButHandler(2); _2B.addActionListener(_2Handler); _3B = new JButton("3"); _3Handler = new ButHandler(3); _3B.addActionListener(_3Handler); _4B = new JButton("4"); _4Handler = new ButHandler(4); _4B.addActionListener(_4Handler); //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(compScoreL); pane.add(compScoreTF); pane.add(sumL); pane.add(oddevenL); pane.add(userChoiceL); pane.add(userChoiceTF); pane.add(compChoiceL); pane.add(compChoiceTF); pane.add(sumTF); pane.add(oddevenTF); //pane.add(new JLabel("")); pane.add(_0B); pane.add(_1B); pane.add(_2B); pane.add(_3B); pane.add(_4B); pane.add(exitB); //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) { value = x; } public void actionPerformed(ActionEvent e) { int rand, sum; userChoiceTF.setText("" + value); //Get a random number for the compChoiceTF rand = (int) Math.floor(Math.random()*5); //if (rand == 5) rand = 4; compChoiceTF.setText("" + rand); sum = value + rand; sumTF.setText("" + sum); if (sum % 2 == 0) { oddevenTF.setText("even\nJoshua"); int uscor = Integer.parseInt(userScoreTF.getText()); userScoreTF.setText("" + (uscor + 1)); } else { oddevenTF.setText("odd"); int cscor = Integer.parseInt(compScoreTF.getText()); compScoreTF.setText("" + (cscor + 1)); } } } private class ExitButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main( String args[] ) { OddsEvens oddevenObject = new OddsEvens(); } }