java 单击了 J 按钮
java Jbutton clicked
int buttonNum = size[0] * size[1];
panelForButtons.setLayout(new GridLayout(size[0], size[1]));
JButton[] buttons = new JButton[buttonNum];
buttonsArray = buttons;
for (int i = 0; i < buttonNum; i++) {
buttons[i] = new JButton();
// Change each button size
buttons[i].setPreferredSize(new Dimension(50, 50));
panelForButtons.add(buttons[i]);
buttons[i].setBackground(Color.black);
buttons[i].addActionListener(new Actions());
}
panelOnTheLeft.add(panelForButtons, BorderLayout.WEST);
}
static class Actions implements ActionListener {
public void actionPerformed(ActionEvent e) {0
}
我想让for循环生成的每个按钮都具有单击按钮时更改背景颜色的功能,我该怎么办?
假设您希望每个按钮的背景颜色在单击时更改:
我可能不会将操作 class 设为静态。
在 actionPerformed()
中,获取从 e
中单击的按钮,设置其背景颜色。
在for循环中,不要每次循环都做一个new Actions()
。在循环开始之前创建一个实例,并在循环期间将其传入。
int buttonNum = size[0] * size[1];
panelForButtons.setLayout(new GridLayout(size[0], size[1]));
JButton[] buttons = new JButton[buttonNum];
buttonsArray = buttons;
// The only action handler you need
Actions myActionHandler = new Actions();
for (int i = 0; i < buttonNum; i++) {
buttons[i] = new JButton();
// Change each button size
buttons[i].setPreferredSize(new Dimension(50, 50));
buttons[i].setBackground(Color.black);
// Just pass in the one we already have from above
buttons[i].addActionListener(myActionHandler);
panelForButtons.add(buttons[i]);
}
panelOnTheLeft.add(panelForButtons, BorderLayout.WEST);
class Actions implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get the button that was clicked
JButton theButton = (JButton) e.getSource();
// Set it's background color to white
theButton.setBackground(Color.white);
}
}
int buttonNum = size[0] * size[1];
panelForButtons.setLayout(new GridLayout(size[0], size[1]));
JButton[] buttons = new JButton[buttonNum];
buttonsArray = buttons;
for (int i = 0; i < buttonNum; i++) {
buttons[i] = new JButton();
// Change each button size
buttons[i].setPreferredSize(new Dimension(50, 50));
panelForButtons.add(buttons[i]);
buttons[i].setBackground(Color.black);
buttons[i].addActionListener(new Actions());
}
panelOnTheLeft.add(panelForButtons, BorderLayout.WEST);
}
static class Actions implements ActionListener {
public void actionPerformed(ActionEvent e) {0
}
我想让for循环生成的每个按钮都具有单击按钮时更改背景颜色的功能,我该怎么办?
假设您希望每个按钮的背景颜色在单击时更改:
我可能不会将操作 class 设为静态。
在 actionPerformed()
中,获取从 e
中单击的按钮,设置其背景颜色。
在for循环中,不要每次循环都做一个new Actions()
。在循环开始之前创建一个实例,并在循环期间将其传入。
int buttonNum = size[0] * size[1];
panelForButtons.setLayout(new GridLayout(size[0], size[1]));
JButton[] buttons = new JButton[buttonNum];
buttonsArray = buttons;
// The only action handler you need
Actions myActionHandler = new Actions();
for (int i = 0; i < buttonNum; i++) {
buttons[i] = new JButton();
// Change each button size
buttons[i].setPreferredSize(new Dimension(50, 50));
buttons[i].setBackground(Color.black);
// Just pass in the one we already have from above
buttons[i].addActionListener(myActionHandler);
panelForButtons.add(buttons[i]);
}
panelOnTheLeft.add(panelForButtons, BorderLayout.WEST);
class Actions implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get the button that was clicked
JButton theButton = (JButton) e.getSource();
// Set it's background color to white
theButton.setBackground(Color.white);
}
}