for(gbc.gridy = 3; gbc.gridy > 0; gbc.gridy--)
for(gbc.gridx = 0; gbc.gridx < 3;gbc.gridx++)
{
grid[btnNum] = new JButton("" + (btnNum+1));
grid[btnNum].setPreferredSize(new Dimension(75,75));
frame.add(grid[btnNum], gbc);
grid[btnNum].addActionListener(this);
grid[btnNum].addKeyListener(this);
btnNum++;
}
I have an array of buttons displayed in a 3x3 grid and each one has an action listener.
public void actionPerformed(ActionEvent e){
String output = "";
if(e.getSource() == grid[0]){
System.out.println("button 1");
}
}
Is this not correct?
Complete example in context:
public class ButtonGrid implements ActionListener, KeyListener{
JFrame frame=new JFrame(); //creates frame
JButton[] grid; //names the grid of buttons
public ButtonGrid(){ //constructor
frame.setTitle("MPC");
frame.setLayout(new GridBagLayout()); //set layout
JButton[] grid=new JButton[12]; //allocate the size of grid
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
int btnNum = 0;
grid[9] = new JButton("0");
grid[9].setPreferredSize(new Dimension(75,75));
grid[10] = new JButton("-");
grid[10].setPreferredSize(new Dimension(75,75));
grid[11] = new JButton("=");
grid[11].setPreferredSize(new Dimension(75,75));
frame.add(grid[9], gbc);
gbc.gridx++;
frame.add(grid[10], gbc);
gbc.gridx++;
frame.add(grid[11], gbc);
for(gbc.gridy = 3; gbc.gridy > 0; gbc.gridy--)
for(gbc.gridx = 0; gbc.gridx < 3;gbc.gridx++){
grid[btnNum] = new JButton("" + (btnNum+1));
grid[btnNum].setPreferredSize(new Dimension(75,75));
frame.add(grid[btnNum], gbc);
grid[btnNum].addActionListener(this);
grid[btnNum].addKeyListener(this);
btnNum++;
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack(); //sets appropriate size for frame
frame.setVisible(true); //makes frame visible
}
public static void main(String[] args) {
new ButtonGrid();//makes new ButtonGrid with 2 parameters
}
public void actionPerformed(ActionEvent e){
String output = "";
if(e.getSource() == grid[0]){
System.out.println("button one");
//playSound(abc.kick);
}}
Your grid variable is null in the ActionListener. My bet -- you're shadowing the variable.
Solution: make sure that you initialize the grid field, not a local grid variable
public class ButtonGrid implements ActionListener, KeyListener{
JFrame frame=new JFrame();
JButton[] grid; // grid field remains null
public ButtonGrid(){
frame.setTitle("MPC");
frame.setLayout(new GridBagLayout());
JButton[] grid=new JButton[12]; // ****** a LOCAL variable ******
At the indicated line you create the shadowed variable. This means because you redeclare grid within the constructor, you initialize only the local variable and not the field that was declared in the class leaving the field null. Solution: Don't re-declare grid. Change it to:
public class ButtonGrid implements ActionListener, KeyListener{
JFrame frame = new JFrame();
JButton[] grid;
public ButtonGrid(){
frame.setTitle("MPC");
frame.setLayout(new GridBagLayout());
grid = new JButton[12]; // ***** this initializes the field ******
Try this.
public void actionPerformed(ActionEvent e){
String output = "";
for(int i=0; i<=grid.length; i++){
if(e.getSource() == grid[i]){
System.out.println("button "+i);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.