Java 我想通过重置按钮重置网格 公共类ButtonGrid扩展JFrame{ 专用最终JButton[][]网格; 私有整数长度、宽度; 公共ButtonGrid(整数宽度、整数长度){ JPanel面板=新的JPanel(); JButton start=新JButton(“start”); JButton reset=新JButton(“reset”); 面板。添加(开始); //start.addActionListener(this); 面板。添加(重置); 添加(面板、边框布局、页面开始); JPanel panel1=新的JPanel(); 面板1.设置布局(新网格布局(宽度、长度)); 网格=新的JButton[宽度][长度]; 对于(int y=0;y
我无法执行此操作,因为我的actionListener不允许使用网格[x][y]设置背景 当然可以这样做。Java 我想通过重置按钮重置网格 公共类ButtonGrid扩展JFrame{ 专用最终JButton[][]网格; 私有整数长度、宽度; 公共ButtonGrid(整数宽度、整数长度){ JPanel面板=新的JPanel(); JButton start=新JButton(“start”); JButton reset=新JButton(“reset”); 面板。添加(开始); //start.addActionListener(this); 面板。添加(重置); 添加(面板、边框布局、页面开始); JPanel panel1=新的JPanel(); 面板1.设置布局(新网格布局(宽度、长度)); 网格=新的JButton[宽度][长度]; 对于(int y=0;y,java,swing,grid-layout,Java,Swing,Grid Layout,我无法执行此操作,因为我的actionListener不允许使用网格[x][y]设置背景 当然可以这样做。grid变量是一个实例变量,因此您可以在ActionListener中直接访问它。因此ActionListener中的代码将是一个循环,循环遍历数组的两个维度,然后设置每个按钮的背景 public class ButtonGrid extends JFrame{ private final JButton[][] grid; private int length,width; publ
grid
变量是一个实例变量,因此您可以在ActionListener中直接访问它。因此ActionListener中的代码将是一个循环,循环遍历数组的两个维度,然后设置每个按钮的背景
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);
}
});
}
}
for(int y=0;y注意,ActionListener
只会被注册它的按钮调用。因此,您不必比较事件的源
以下代码将通过注册多个ActionListener
s来重置所有按钮,单击重置按钮时将调用这些按钮:
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);
}
}
for(int y=0;y重置按钮应该做什么?重置所有按钮?注意:if(b1==源)
检查是不必要的。您的ActionListener
只会被您注册它的按钮调用,其他什么都不会调用。更重要的是,您正在添加ActionListener以在for循环内重置。这在我看来似乎毫无意义。您的网格[][]
是在类上下文中声明的。请确保您可以从任何成员方法或嵌套类访问它。@Hafiz,最好您自己检查代码。并按照注释中指出的问题不断提问。当我单击“重置”按钮时,我想将网格中所有按钮的背景更改为白色。因此,我想访问所有按钮通过重置按钮的动作监听器,我可以将背景更改为白色。
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);
}
}
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);
}
});