I have a problem in my game about button how I can display the
random number example "3 + 5" in the upper button ...I try one button here but you need to click it in order to see the random number and only one number will appear..please any idea..

import javax.swing.*;
import java.util.Random;
import java.awt.event.*;

public class Pros extends JApplet implements ActionListener{

    //up button
    JButton button1, button2,
            button3, button4,
            button5, button6,
            button7, button8,
            button9,button10,
     //down button
            button11, button12,
            button13, button14,
            button15, button16,
            button17, button18,
            button19, button20;

    JButton PlayA, Start, lvl1, lvl2, lvl3;
    JLabel label1, label2, label3, label4, label5, label6, label7;
    JTextField tf1, tf2, tf3, tf4;
    int n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10, n11;

    @Override
    public void init() {
            setSize(700, 660);
            setLayout(null);

  //label
   label1 = new JLabel("Level 1: Addition and Subtraction. Match each problem to its solution \n");
        label1.setBounds(180,10,500,30);

   label2 = new JLabel("Level:");
        label2.setBounds(30,540,100,30);

   label3 = new JLabel("Score:");
        label3.setBounds(30,600,100,30);

   label4 = new JLabel("matches in");
        label4.setBounds(140,600,100,30);

   label5 = new JLabel("tries");
        label5.setBounds(140,620,100,30);

   label6 = new JLabel("Timer");
        label6.setBounds(350,550,100,30);

   label7 = new JLabel(" ");
        label7.setBounds(350,580,100,30);

  //textfield
         tf1 = new JTextField(" ",20);
            tf1.setBounds(400,60,250,200);
         tf2 = new JTextField(" ",20);
            tf2.setBounds(400,310,250,200);
         tf3 = new JTextField(" ",20);
            tf3.setBounds(80,600,50,30);
         tf4 = new JTextField(" ",20);
            tf4.setBounds(80,630,50,30);


   //Up button
         button1 = new JButton("click");
            button1.setBounds(50,60,100,30);
         button2 = new JButton("");
            button2.setBounds(170,60,100,30);
         button3 = new JButton("");
            button3.setBounds(50,100,100,30);
         button4 = new JButton("");
            button4.setBounds(170,100,100,30);
         button5 = new JButton("");
            button5.setBounds(50,140,100,30);
         button6 = new JButton("");
            button6.setBounds(170,140,100,30);
         button7 = new JButton("");
            button7.setBounds(50,180,100,30);
         button8 = new JButton("");
            button8.setBounds(170,180,100,30);
         button9 = new JButton("");
            button9.setBounds(50,220,100,30);
         button10 = new JButton("");
            button10.setBounds(170,220,100,30);
    //down button
         button11 = new JButton("0");
            button11.setBounds(50,310,100,30);
         button12 = new JButton("5");
            button12.setBounds(170,310,100,30);
         button13 = new JButton("1");
            button13.setBounds(50,350,100,30);
         button14 = new JButton("6");
            button14.setBounds(170,350,100,30);
         button15 = new JButton("2");
            button15.setBounds(50,390,100,30);
         button16 = new JButton("7");
            button16.setBounds(170,390,100,30);
         button17 = new JButton("3");
            button17.setBounds(50,430,100,30);
         button18 = new JButton("8");
            button18.setBounds(170,430,100,30);
         button19 = new JButton("4");
            button19.setBounds(50,470,100,30);
         button20 = new JButton("9");
            button20.setBounds(170,470,100,30);

//Play Again Button
  PlayA = new JButton("Play Again!");
            PlayA.setBounds(500,590,100,30);

  Start = new JButton("Start");
            Start.setBounds(500,550,100,30);


//Level Button
  lvl1 = new JButton("1");
            lvl1.setBounds(80,550,50,30);
  lvl2 = new JButton("2");
            lvl2.setBounds(140,550,50,30);
  lvl3 = new JButton("3");
            lvl3.setBounds(200,550,50,30);


//display
   add(label1);
   add(label2);
   add(label3);
   add(label4);
   add(label5);
   add(label6);
   add(label7);
   add(tf1);
   add(tf2);
   add(tf3);
   add(tf4);
   add(PlayA);
   add(Start);
   add(lvl1);
   add(lvl2);
   add(lvl3);
   add(button1);
   add(button2);
   add(button3);
   add(button4);
   add(button5);
   add(button6);
   add(button7);
   add(button8);
   add(button9);
   add(button10);
   add(button11);
   add(button12);
   add(button13);
   add(button14);
   add(button15);
   add(button16);
   add(button17);
   add(button18);
   add(button19);
   add(button20);

//action listener
     button1.addActionListener(this);
     button2.addActionListener(this);
     button3.addActionListener(this);
     button4.addActionListener(this);
     button5.addActionListener(this);
     button6.addActionListener(this);
     button7.addActionListener(this);
     button8.addActionListener(this);
     button9.addActionListener(this);
     button10.addActionListener(this);
     button11.addActionListener(this);
     button12.addActionListener(this);
     button13.addActionListener(this);
     button14.addActionListener(this);
     button15.addActionListener(this);
     button16.addActionListener(this);
     button17.addActionListener(this);
     button18.addActionListener(this);
     button19.addActionListener(this);
     button20.addActionListener(this);
     PlayA.addActionListener(this);
     Start.addActionListener(this);
    }

   
    public void actionPerformed(ActionEvent e) {

  //down button function
    JButton source = (JButton)e.getSource();
      if(source.getActionCommand() == "0") {
           n0 = 0;
           tf2.setText(String.valueOf(n0));
           n1=0; n2=0; n3=0; n4=0; n5=0; n6=0; n7=0; n8=0; n9=0;
      }
      if(source.getActionCommand() == "1"){
            n1 = 1;
            tf2.setText(String.valueOf(n1));
            n0=0; n2=0; n3=0; n4=0; n5=0; n6=0; n7=0; n8=0; n9=0;
      }
      if(source.getActionCommand() == "2"){
            n2 = 2;
            tf2.setText(String.valueOf(n2));
            n1=0; n1=0; n3=0; n4=0; n5=0; n6=0; n7=0; n8=0; n9=0;
      }
      if(source.getActionCommand() == "3"){
            n3 = 3;
            tf2.setText(String.valueOf(n3));
            n1=0; n1=0; n2=0; n4=0; n5=0; n6=0; n7=0; n8=0; n9=0;
      }
      if(source.getActionCommand() == "4"){
            n4 = 4;
            tf2.setText(String.valueOf(n4));
            n1=0; n1=0; n2=0; n3=0; n5=0; n6=0; n7=0; n8=0; n9=0;
      }
      if(source.getActionCommand() == "5"){
            n5 = 5;
            tf2.setText(String.valueOf(n5));
            n1=0; n1=0; n2=0; n3=0; n4=0; n6=0; n7=0; n8=0; n9=0;
      }
      if(source.getActionCommand() == "6"){
            n6 = 6;
            tf2.setText(String.valueOf(n6));
            n1=0; n1=0; n2=0; n3=0; n4=0; n5=0; n7=0; n8=0; n9=0;
      }
      if(source.getActionCommand() == "7"){
            n7 = 7;
            tf2.setText(String.valueOf(n7));
            n1=0; n1=0; n2=0; n3=0; n4=0; n5=0; n6=0; n8=0; n9=0;
      }
      if(source.getActionCommand() == "8"){
            n8 = 8;
            tf2.setText(String.valueOf(n8));
            n1=0; n1=0; n2=0; n3=0; n4=0; n5=0; n6=0; n7=0; n9=0;
      }
      if(source.getActionCommand() == "9"){
            n9 = 9;
            tf2.setText(String.valueOf(n9));
            n1=0; n1=0; n2=0; n3=0; n4=0; n5=0; n6=0; n7=0; n8=0;
      }

    //don't diplay the random number directly need to click in able to
    //see the random number
      if(source.getActionCommand() == "click"){
             int randomNumber1 = (int)(Math.random()*10);
             n10 = randomNumber1;
             button1.setText(String.valueOf(n10));

      }

    }
  }
source.getActionCommand() == "click")

Use the equals method when comparing the contents of objects

Please add some comments in your code where you are having the problem.
Something like:
//************* HERE IS THE PROBLEM. I CAN"T Get the ddd to ssss .....*****************

Here...

243 //don't diplay the random number directly need to click in able to
244 //see the random number
245 if(source.getActionCommand() == "click"){
246 int randomNumber1 = (int)(Math.random()*10);
247 n10 = randomNumber1;
248 button1.setText(String.valueOf(n10));
249 
250 }
251  
252 }
253 }


I attach some picture of result..Please help me with this..this will pass this friday..:( This game has 3 levelAddition and Subtraction
Multiplication and Division
Mix ALL

but I cannot proceed because I cannot get the right output in the upper button and I think my code is too manual lots of declaration..is there alternative way?

Attachments 1.png 119.84 KB 2.png 134.25 KB math.png 27.45 KB

Sorry, I can not understand what you are trying to do.
Can you explain step by step what the user does and what you want the GUI to display?
The code you posted checks if the clicked button's label is "click" and immediately changes the label. The comment says to display the number when the button is clicked. That is what the code does.

What is the explanation for the three images you posted?
Is there a wrong one and a right one? Or ???

Edited 5 Years Ago by NormR1: n/a

Did you not read Norm's post re equals method? Or did you just decide to ignore it?

i read it....still trying..

Sorry for confusion ok here's the step..

1. The user will choice what level he want..
2. If he is done then he will click the start button then will start the timer (level1: 10 secs, level2: 20 secs, level3:30secs
3. The lower button is a fix button that only have 0-9..
4. The upper button will have a random number
// this part I was confuse because I do not know how to put two random number in one button with the sign either '+' or '-'
5. If the user click the problem solving that display in the upper button then it will display in the textfield in the right side...
6. If the user want to answer then he will click the lower button then it will also display in the textfield in the right side...
7. There's a counter in the lower part that will display the correct and tries of the user..
8. If the timer ends it will stop the game and display the number of correct and tries..


Hope you understand it..

correction:
1. The user will choose what level he want..

The first and second image is the wrong one..the result should be like in the image three..........:)

sounds like you should have method that populates all the upper buttons with random problems, and that method should be executed when the user selects a level (or one method for each level...).
You can set up a problem with something like this (pseudocode)...

int op1 = random number 1-9, eg 7
int op2 = random number 1-9, eg 2
boolean plus = random boolean (nextBoolean method) eg false
// true for addition, false for subtraction
if (plus)  
  text = op1 + "+" + op2;
  // uses + operator to concatenate Strings, ints are converted to strings automatically
  correct = op1 + op2  // right answer for checking
  // uses + operator to add two ints numerically
else 
  text = op1 + "-" + op2   eg "7-2"
  correct = op1 - op2  // right answer for checking

set button with text

Your code currently sets the text of the button to the value of n10.
Change that to a String that is built as in James's post.

ok i get it..how i can compare textfield1 with textfield2 so it will display in the counter of correct answer..

how i can compare textfield1 with textfield2 so it will display in the counter of correct answer.

To compare the contents of two textfields, get the text from each and see if they are equal.
I don't know what you mean by "the counter"? Is that one of the components like a label or text field?

Thanks to both of you..your idea is a big help for me..i already finish my project...:D

This question has already been answered. Start a new discussion instead.