Results 1 to 4 of 4
Thread: my guess game problem
- 12-28-2012, 12:09 PM #1
Member
- Join Date
- Oct 2012
- Location
- ph
- Posts
- 43
- Rep Power
- 0
my guess game problem
my game is having a problem. i want my jbutton to be red if the user guesses the correct number on my textfield but it shows red for a random number
Java Code:import javax.swing.*; import java.awt.event.*; public class Gridgame extends JFrame { int row =5; int col =5; int guess; int secretenum; JTextArea textfield; JButton[][] button; JPanel panel1; JPanel panel2; Random rand = new Random(); public Gridgame() { super("guess the number"); panel1=new JPanel(); textfield= new JTextArea("\n\n the number",7,25); textfield.setBackground(Color.BLUE); textfield.setForeground(Color.pink); textfield.setFont(new Font("serif",Font.BOLD,23)); panel1.add(textfield); add(panel1,BorderLayout.NORTH); panel2 = new JPanel(); button= new JButton[row][col]; panel2.setLayout(new GridLayout(row,button.length)); for(int i=0;i<=button.length-1;i++) { for(int j=0;j<=button[i].length-1;j++) { button[i][j]=new JButton("$"); button[i][j].setBackground(Color.white); guess=1+rand.nextInt(25); button[i][j].setText(String.valueOf(guess)); panel2.add(button[i][j]); } } add(panel2); actions handler = new actions(); for(int i=0;i<=button.length-1;i++) { for(int j=0;j<=button[i].length-1;j++) { button[i][j].addActionListener(handler); } } } public static void main(String[] args) { Gridgame app = new Gridgame(); app.setSize(400,500); app.setVisible(true); app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public class actions implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String toguess=""; int i; int j; for( i=0;i<=button.length-1;i++) { for(j=0;j<=button[i].length-1;j++) { int randi=1+rand.nextInt(button.length-1); int randj=1+rand.nextInt(button.length-1); toguess =button[randi][randj].getText(); textfield.setText("\n guess the button with "+ toguess +" " +button[randi][randj].getText()); int itoguess =Integer.parseInt(toguess); int ibutton =Integer.parseInt(button[i][j].getText()); if(e.getSource()==button[randi][3]) { button[randi][randj].setBackground(Color.red); } } } } } }joy wouldnt feel so gud if it wasnt for pain.gif)
- 12-28-2012, 05:02 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
Re: my guess game problem
Hello,
please indent your code properly so it's easier to read it.
If you are using Eclipse, use Ctrl+A to select all and then Ctrl+I for indenting it automatically.
Can you elaborate on your problem. I tried your code but I do not understand what the game is about or what you should do.
- 12-28-2012, 10:18 PM #3
Member
- Join Date
- Oct 2012
- Location
- ph
- Posts
- 43
- Rep Power
- 0
Re: my guess game problem
wow....thanks a lot.you just thought me one new thing....ok the game is all about giving the buttons random name and then a random buttons name is placed on the screen for the name to be guessed if the button is clicked, the color should be red.... But instead it is giving a different button the red color
joy wouldnt feel so gud if it wasnt for pain.gif)
-
Re: my guess game problem
You should sprinkle your code with println statements to find out why it is not behaving as you are expecting it to behave.
Similar Threads
-
help needed plz with some loop problem I guess
By Tommeke in forum New To JavaReplies: 6Last Post: 07-21-2011, 07:42 PM -
guess game GUI
By tomandhisjones in forum New To JavaReplies: 4Last Post: 04-04-2011, 06:30 AM -
how to convert string to int in guess number game in jsp
By tahir90 in forum New To JavaReplies: 3Last Post: 12-20-2009, 03:22 PM -
guess number game
By mistah in forum New To JavaReplies: 10Last Post: 11-23-2008, 03:37 AM -
sample of guess high and low game
By pouria62 in forum AWT / SwingReplies: 1Last Post: 10-26-2008, 12:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks