public class ButtonGrid extends JFrame{
private final JButton[][] grid;
private int length,width;
public ButtonGrid(int width,int length){
JPanel panel = new JPanel();
JButton start = new JButton("Start");
JButton reset = new JButton("Reset");
panel.add(start);
//start.addActionListener(this);
panel.add(reset);
add(panel,BorderLayout.PAGE_START);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayout(width,length));
grid = new JButton[width][length];
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
grid[x][y]=new JButton();
grid[x][y].setBackground(Color.WHITE);
panel1.add(grid[x][y]);
grid[x][y].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
JButton b1 = (JButton)source;
if(b1 == source){
b1.setBackground(Color.BLACK);
}
}
});
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
JButton b1 = (JButton)source;
if(b1 == source){
b1.setBackground(Color.WHITE);
}
}
});
}
}
add(panel1,BorderLayout.CENTER);
panel1.setBackground(Color.RED);
setSize(600,600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
ButtonGrid bg = new ButtonGrid(25,25);
}
});
}
}
在此代码中,我可以更改网格中按钮的背景,但我想通过再次将背景更改为WHITE来重置网格。我无法做到这一点,因为我的actionListener不允许使用grid [x] [y]设置背景。请帮助我如何处理此事。
答案 0 :(得分:1)
我无法执行此操作,因为我的actionListener不允许使用grid [x] [y]设置背景。
当然可以这样做。 grid
变量是一个实例变量,因此您可以直接在ActionListener中访问它。因此,ActionListener中的代码将是一个循环,遍历数组的两个维度,然后设置每个按钮的背景
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
grid[x][y].setBackground(Color.WHITE);
}
}
此外,您不应为每个按钮创建单独的ActionListener。您可以使用以下代码共享侦听器:
ActionListener black = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
JButton b1 = (JButton)source;
b1.setBackground(Color.BLACK);
}
};
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
grid[x][y]=new JButton();
grid[x][y].setBackground(Color.WHITE);
panel1.add(grid[x][y]);
grid[x][y].addActionListener(black);
}
}
答案 1 :(得分:0)
请注意,ActionListener
只会被注册的按钮调用。所以你不必比较事件的来源。
以下代码将通过注册多个ActionListener
来重置所有按钮,这些按钮在单击重置按钮时都会被调用:
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
final JButton b=new JButton();
grid[x][y]=b;
b.setBackground(Color.WHITE);
panel1.add(b);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b.setBackground(Color.BLACK);
}
});
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b.setBackground(Color.WHITE);
}
});
}
}
或者你可以使用一个监听器来设置背景,一个复位监听器遍历所有按钮:
ActionListener black=new ActionListener(){
public void actionPerformed(ActionEvent e){
((JButton)e.getSource()).setBackground(Color.BLACK);
}
};
for(int y=0; y<length; y++){
for(int x=0; x<width; x++){
final JButton b=new JButton();
grid[x][y]=b;
b.setBackground(Color.WHITE);
panel1.add(b);
b.addActionListener(black);
}
}
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
for(JButton[] buttons:grid)
for(JButton b:buttons)
b.setBackground(Color.WHITE);
}
});