Too-high / too-low guessing game in Swing

I have created this program and it works fine. It’s just that its too wonky and huge so I was wondering if anyone knew how I could shorten the program so I didn’t have to put all of these into each button

if(guess == number){
    l1.setText("You have won the number was " + number);
}
if(guess > number){
    l1.setText("Too high");
}
if(guess < number){
    l1.setText("Too low");
}

Here is the full program

public class Main {
    public static int number, guess;
    public static Random rand;
    public static Scanner scan;
    public static JButton b1 = new JButton("1");
    public static JButton b2 = new JButton("2");
    public static JButton b3 = new JButton("3");
    public static JButton b4 = new JButton("4");
    public static JButton b5 = new JButton("5");
    public static JButton b6 = new JButton("6");
    public static JButton b7 = new JButton("7");
    public static JButton b8 = new JButton("8");
    public static JButton b9 = new JButton("9");
    public static JButton b10 = new JButton("10");
    public static JLabel l1 = new JLabel("Guess a number between 1 and 10!");


    public Main(){
        frame();
    }

    public void frame(){
        rand = new Random();
        number = rand.nextInt(10);

        JFrame f = new JFrame();
        f.setResizable(false);
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        JPanel p  = new JPanel();
        f.add(p);
        p.add(b1);
        p.add(b2);
        p.add(b3);
        p.add(b4);
        p.add(b5);
        p.add(b6);
        p.add(b7);
        p.add(b8);
        p.add(b9);
        p.add(b10);
        p.add(l1);

        b1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 1;
                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }
                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b2.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 2;

                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 3;

                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b4.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 4;

                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b5.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 5;

                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b6.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 6;

                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b7.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 7;


                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b8.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 8;

                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b9.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 9;

                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });

        b10.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                guess = 10;


                if(guess == number){
                    l1.setText("You have won the number was " + number);
                }
                if(guess > number){
                    l1.setText("Too high");
                }

                if(guess < number){
                    l1.setText("Too low");
                }
            }
        });
    }   

    public static void main(String[] args){
        new Main();
    }
}

Answer

Create a new Actionlistener class and add it to all Buttons:

b1.addActionListener(new MyActionListener(1))
b2.addActionListener(new MyActionListener(2))
b3.addActionListener(new MyActionListener(3))
...

class MyActionListener implements ActionListener {

    private int guess ;
    public MyActionListener(int guess){
        this.guess = guess;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(guess == number){
            l1.setText("You have won the number was " + number);
        }
        if(guess > number){
            l1.setText("Too high");
        }

        if(guess < number){
            l1.setText("Too low");
        }

    }

}

Attribution
Source : Link , Question Author : Community , Answer Author : Community

Leave a Comment