Creating Button Array Error
Rookie here, I'm attempting to create a button array but I get the following errors:
import javax.swing.*;
import java.awt.*;
import java.text.*;
publicclass CreateButtonArrayextends JFrame{
publicstaticvoid main(String[] args){
CreateButtonArray calcPad =new CreateButtonArray();
calcPad.setVisible(true);
}
public CreateButtonArray(){
super("Calculator");
setSize(200, 300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel numPad =new JPanel();
numPad.setLayout(new GridLayout(4, 4, 5, 5));
add(numPad.CENTER);
privatestaticfinal String[] buttons ={// CreateButtonArray.java:23: illegal start of expression
"7","8","9","/",
"4","5","6","*",
"1","2","3","-",
"0","CE","C","+"
};
privatestaticfinal JButton [] calcButtons =new JButton[buttons.length];
for(int i = 0; i < buttons.length; i++){// "for" <-- CreateButtonArray.java:34: illegal start of type
JButton b =new JButton(buttons[i]);// buttons.length; <-- CreateButtonArray.java:34: > expected
calcButton[i] = b;/ i++; <-- CreateButtonArray.java:34: <identifier> expected
numPad.add(calcButton[b]);//
}
}
}//CreateButtonArray.java:41: class, interface, or enum expecte
You are trying to create a static variable inside a constructor? That doesn't make any sense. Remove the "private static final" from that one line. You also have one too many closing parentheses.
I got the idea of creating a variable inside a constructor from this forum. As you can probably see I'm attempting to label my buttons with the String [] button array. I've removed "private static final" . Other errors have generated. I'll continue to plug away at it. Thank you for your input.
You can create a variable inside the constructor, but you can't use access modifiers or declare them static. You could declare it final if you wanted to. If yo post the other errors here, we can still help you out.
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/variables.html
Thank you. Here is the current state.
import javax.swing.*;
import javax.swing.JButton;
import java.awt.*;
import java.text.*;
public class CreateButtonArray extends JFrame{
public static void main(String[] args){
CreateButtonArray calcPad = new CreateButtonArray();
calcPad.setVisible(true);
}
public CreateButtonArray(){
super("Calculator");
setSize(200, 300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel numPad = new JPanel();
numPad.setLayout(new GridLayout(4, 4, 5, 5));
add(numPad, BorderLayout.CENTER);
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "CE", "C", "+"
};
JButton [] calcButton = new JButton[buttons.length];
for(int i = 0; i < buttons.length; i++){
JButton b = new JButton(buttons);
calcButton = b;
numPad.add(calcButton);
/*CreateButtonArray.java:38: incompatible types
found: javax.swing.JButton; equired: int*/
}
}
}
for(int i = 0; i < buttons.length; i++){ JButton b = new JButton(buttons[i]); calcButton[i] = b; numPad.add(calcButton);
Thank you, AnanSmriti . See I told you I was a rookie. During my travel back home today I read up on Methods and Arrays. It seems I fix (with the forums feedback), I find another one. Here is my latest error. Again, thanks all for your assistance. This is very educational.
Error: CreateButtonArray.java:40: cannot find symbol
symbol : method add(javax.swing.JButton[])
location: class javax.swing.JPanel
numPad.add(calcButton);
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.*;
import java.text.*;
public class CreateButtonArray extends JFrame{
public static void main(String[] args){
CreateButtonArray calcPad = new CreateButtonArray();
calcPad.setVisible(true);
}
public CreateButtonArray(){
super("Calculator");
setSize(200, 300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel numPad = new JPanel();
numPad.setLayout(new GridLayout(4, 4, 5, 5));
String[] buttons = {
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", "CE", "C", "+"
};
add(numPad);
JButton [] calcButton = new JButton[buttons.length];
for(int i = 0; i < buttons.length; i++){
JButton b = new JButton(buttons[i]);
calcButton[i] = b;
numPad.add(calcButton);//Error is here
}
}
}
I channged the following code and it worked:
JButton [] calcButton = new JButton[buttons.length];
for(int i = 0; i < buttons.length; i++){
JButton b = new JButton(buttons[i]);
calcButton[i] = b;
numPad.add(calcButton);//This errored
for(int i = 0; i < buttons.length; i++){
JButton b = new JButton(buttons[i]);
calcButton[i] = b;
numPad.add(b);//This fixed it
}
Any suggestions on why the top one errored and the bottom did not?
What error did you get? post your error message
It means you cannot add array of buttons into your numPad. So just add one by one to your numPadeg numPad.add(calcButton[i]);