I need help getting my int guess to appear in my Jpanel so that the user can see the first random guess and their guesses from then on until the guess is correct.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class C06GuessingGame extends JFrame implements ActionListener {
public static final long serialVersionUID = 1L;
JFrame window = null;
JTextField Box = new JTextField("Box");
JButton High = new JButton("High");
JButton Low = new JButton("Low");
JButton Correct = new JButton("Correct");
// Variables for random and guess numbers
int Highest = 1000;
int Lowest = 0;
int Counter = 0;
Random r = new Random();
int random = r.nextInt(1000) + 1;
public C06GuessingGame() {
window = new JFrame();
setSize(250, 150);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Guessing Game");
JPanel pane = new JPanel();
pane.add(High);
pane.add(Low);
pane.add(Correct);
add(pane);
High.addActionListener(this);
Low.addActionListener(this);
Correct.addActionListener(this);
setVisible(true);
}
public static void main(String[] args) {
new C06GuessingGame();
}
public void actionPerformed(ActionEvent e) {
int guess = random;<--HOW CAN I MAKE THIS APPEAR IN MY JPANEL UNDERNEATH HIGH, LOW AND CORRECT?
//System.out.println(guess);
if (e.getSource() == Low) {
Lowest = guess;
random = (Highest + Lowest) / 2;
Counter++;
} else if (e.getSource() == High) {
Highest = guess;
random = (Highest + Lowest) / 2;
Counter++;
} else {
JOptionPane.showMessageDialog(window,("CORRECT!!! The number of guesses taken were: " + Counter ++));
window.setVisible(false);
}
}
}

New Topic/Question
Reply



MultiQuote




|