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));
}
}
}