import java.awt.*; import java.applet.Applet; import java.awt.event.*; import java.util.Scanner; import java.util.Random; public class HighLow extends Applet implements ActionListener { //declare variables Panel nPanel, sPanel, tcPanel, bcPanel, cPanel; Button one, clear; Label top,lb1, lb2, lb3, lb4, bottom; TextField txt; int number, cnumber,count; final int MAX = 25; public void init() { //create random number Random generator = new Random(); cnumber = generator.nextInt(MAX) + 1; //set north panel nPanel = new Panel(); nPanel.setBackground(Color.blue); top = new Label("High/Low Game ~What Number am I thinking of?~"); //set south panel sPanel = new Panel(); sPanel.setBackground(Color.red); bottom = new Label("Pick a number between 1 and 25"); //set center, topcenter, bottomcenter panel cPanel = new Panel(); tcPanel = new Panel(); bcPanel = new Panel(); //add submit buttonm labels and textfield one = new Button("Submit"); one.addActionListener(this); clear = new Button("Clear"); clear.addActionListener(this); one.setPreferredSize(new Dimension(100,100)); clear.setPreferredSize(new Dimension(20,20)); lb1 = new Label(); lb2 = new Label(); lb3 = new Label(" "); txt = new TextField(); lb4 = new Label(" "); //set topcenter GridLayout tcPanel.setLayout (new GridLayout(3,1)); tcPanel.add(lb3); tcPanel.add(txt); tcPanel.add(lb2); //set bottomcenter GridLayout bcPanel.setLayout (new GridLayout(3,1)); bcPanel.add(lb1); bcPanel.add(one); bcPanel.add(lb4); //combine tc and bc panels cPanel.setLayout(new BorderLayout(7,1)); //cPanel.add(clear); cPanel.add(tcPanel, BorderLayout.WEST); cPanel.add(bcPanel, BorderLayout.EAST); //set layout and label for top nPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); nPanel.add(top); //set layout and label for bottom sPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); sPanel.add(bottom); //set layout for the Applet this.setLayout(new BorderLayout()); add(nPanel, BorderLayout.NORTH); add(cPanel, BorderLayout.CENTER); add(sPanel, BorderLayout.SOUTH); } public void actionPerformed(ActionEvent e) { number = Integer.parseInt(txt.getText()); count++; if (number > cnumber) { lb3.setText("Number is too high"); lb3.setBackground(Color.red); lb4.setText(""); } else if (number < cnumber) { lb2.setText("Number is too low"); lb2.setBackground(Color.yellow); lb3.setText(""); } else if (number == cnumber) { bottom.setText("Guessed it in " + count + " tries!!"); bottom.setBackground(Color.green); sPanel.setBackground(Color.green); top.setText("CONGRATULATIONS!"); top.setBackground(Color.green); nPanel.setBackground(Color.green); //bottom.setText("It only took you " + count + " guesses!!!"); } } }