patternjavaModerate
Too-high / too-low guessing game in Swing
Viewed 0 times
lowhightoogameswingguessing
Problem
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
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){
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){
Solution
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");
}
}
}Code Snippets
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");
}
}
}Context
StackExchange Code Review Q#61171, answer score: 11
Revisions (0)
No revisions yet.