Java Program - GUI Number Guessing Look at the code, notice that the actionPerformed method is not complete. It just contains code that will print to the console when buttons are pressed. Make the following modifications to the code. When the Higher button is pressed invoke this.guesser.higher(), and then put the new guess into the this.guessField When the Lower button is pressed invoke this.guesser.lower() and then put the new guess into the this.guessField When the Reset button is pressed, invoke this.guesser.reset() and then put the new guess into the ghis.guessField When the Correct button is pressed, exit the app using System.exit(0). Wrap the invocation of lower() and higher() in try catch blocks that catch NumberGuesserIllegalStateExceptions. Show a JOptionPane that alerts the user that you are onto their schemes. Change the guessing algorithm from random-guess to binary search. You can do this by changing the object created for the guesser to a plain old NumberGuesser. It should just be a single line change. Main.java import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextField; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JOptionPane; class Main extends JFrame implements ActionListener { private JTextField guessField; private JButton lowerButton; private JButton higherButton; private JButton correctButton; private JButton resetButton; private NumberGuesser guesser; public Main() { // Used to specify GUI component layout GridBagConstraints positionConst = null; // Set frame's title setTitle("Number Guesser"); // Create the Number Guesser this.guesser = new RandomNumberGuesser(1, 100); // Create the display for the guess JLabel guessLabel = new JLabel("Current Guess: "); this.guessField = new JTextField(15); this.guessField.setEditable(false); // Create the four button objects this.lowerButton = new JButton("Lower"); this.higherButton = new JButton("Higher"); this.correctButton = new JButton("Correct"); this.resetButton = new JButton("Reset"); // Use "this" class to handle button presses this.lowerButton.addActionListener(this); this.higherButton.addActionListener(this); this.correctButton.addActionListener(this); this.resetButton.addActionListener(this); // Use a GridBagLayout setLayout(new GridBagLayout()); positionConst = new GridBagConstraints(); // 10 pixels vert, 5 horizontal around components positionConst.insets = new Insets(10, 5, 10, 5); // Add component using the specified constraints positionConst.gridx = 0; positionConst.gridy = 0; add(guessLabel, positionConst); positionConst.gridx = 1; positionConst.gridy = 0; add(this.guessField, positionConst); positionConst.gridx = 2; positionConst.gridy = 0; add(this.resetButton, positionConst); positionConst.gridx = 0; positionConst.gridy = 2; add(this.lowerButton, positionConst); positionConst.gridx = 1; positionConst.gridy = 2; add(this.higherButton, positionConst); positionConst.gridx = 2; positionConst.gridy = 2; add(this.correctButton, positionConst); } /* * Method is automatically called when a button is pressed * * It needs some work. The logic here doesn't play a guessing game. It just * provides some examples that you can use. * * inside the action performed method you have access to this class's * member fields. So you can access each of these: * - this.lowerButton * - this.higherButton * - this.resetButton * - this.cancelButton * - this.numberGuesser */ @Override public void actionPerformed(ActionEvent event) { // A reference to the button that was pressed Object buttonPressed = event.getSource(); // Here is how to check to see if the button is // the "lower" button. Use can use similar if // statements to check for the other buttons if (buttonPressed == this.lowerButton) { // printint to the console is just for debugging when you // are writing a GUI. The user won't see it. System.out.println("Lower"); } // Here is a similar if. This one looks for the "reset" // button. It displays a JOptionPane. if (buttonPressed == this.resetButton) { // This is how you can show a JOptionPane. You should use this // to let the user know when you catch them cheating. JOptionPane.showInternalMessageDialog( null, "Cheating!", "Notice", JOptionPane.INFORMATION_MESSAGE); } } public static void main(String[] args) { Main myFrame = new Main(); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.pack(); myFrame.setVisible(true); } }
Java Program - GUI Number Guessing
Look at the code, notice that the actionPerformed method is not complete. It just contains code that will print to the console when buttons are pressed. Make the following modifications to the code.
- When the Higher button is pressed invoke this.guesser.higher(), and then put the new guess into the this.guessField
- When the Lower button is pressed invoke this.guesser.lower() and then put the new guess into the this.guessField
- When the Reset button is pressed, invoke this.guesser.reset() and then put the new guess into the ghis.guessField
- When the Correct button is pressed, exit the app using System.exit(0).
- Wrap the invocation of lower() and higher() in try catch blocks that catch NumberGuesserIllegalStateExceptions. Show a JOptionPane that alerts the user that you are onto their schemes.
- Change the guessing
algorithm from random-guess to binary search. You can do this by changing the object created for the guesser to a plain old NumberGuesser. It should just be a single line change.
Main.java
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
class Main extends JFrame implements ActionListener {
private JTextField guessField;
private JButton lowerButton;
private JButton higherButton;
private JButton correctButton;
private JButton resetButton;
private NumberGuesser guesser;
public Main() {
// Used to specify GUI component layout
GridBagConstraints positionConst = null;
// Set frame's title
setTitle("Number Guesser");
// Create the Number Guesser
this.guesser = new RandomNumberGuesser(1, 100);
// Create the display for the guess
JLabel guessLabel = new JLabel("Current Guess: ");
this.guessField = new JTextField(15);
this.guessField.setEditable(false);
// Create the four button objects
this.lowerButton = new JButton("Lower");
this.higherButton = new JButton("Higher");
this.correctButton = new JButton("Correct");
this.resetButton = new JButton("Reset");
// Use "this" class to handle button presses
this.lowerButton.addActionListener(this);
this.higherButton.addActionListener(this);
this.correctButton.addActionListener(this);
this.resetButton.addActionListener(this);
// Use a GridBagLayout
setLayout(new GridBagLayout());
positionConst = new GridBagConstraints();
// 10 pixels vert, 5 horizontal around components
positionConst.insets = new Insets(10, 5, 10, 5);
// Add component using the specified constraints
positionConst.gridx = 0;
positionConst.gridy = 0;
add(guessLabel, positionConst);
positionConst.gridx = 1;
positionConst.gridy = 0;
add(this.guessField, positionConst);
positionConst.gridx = 2;
positionConst.gridy = 0;
add(this.resetButton, positionConst);
positionConst.gridx = 0;
positionConst.gridy = 2;
add(this.lowerButton, positionConst);
positionConst.gridx = 1;
positionConst.gridy = 2;
add(this.higherButton, positionConst);
positionConst.gridx = 2;
positionConst.gridy = 2;
add(this.correctButton, positionConst);
}
/*
* Method is automatically called when a button is pressed
*
* It needs some work. The logic here doesn't play a guessing game. It just
* provides some examples that you can use.
*
* inside the action performed method you have access to this class's
* member fields. So you can access each of these:
* - this.lowerButton
* - this.higherButton
* - this.resetButton
* - this.cancelButton
* - this.numberGuesser
*/
@Override
public void actionPerformed(ActionEvent event) {
// A reference to the button that was pressed
Object buttonPressed = event.getSource();
// Here is how to check to see if the button is
// the "lower" button. Use can use similar if
// statements to check for the other buttons
if (buttonPressed == this.lowerButton) {
// printint to the console is just for debugging when you
// are writing a GUI. The user won't see it.
System.out.println("Lower");
}
// Here is a similar if. This one looks for the "reset"
// button. It displays a JOptionPane.
if (buttonPressed == this.resetButton) {
// This is how you can show a JOptionPane. You should use this
// to let the user know when you catch them cheating.
JOptionPane.showInternalMessageDialog(
null,
"Cheating!",
"Notice", JOptionPane.INFORMATION_MESSAGE);
}
}
public static void main(String[] args) {
Main myFrame = new Main();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.pack();
myFrame.setVisible(true);
}
}

Trending now
This is a popular solution!
Step by step
Solved in 3 steps









