java - CheckBox 矩阵出现问题,循环不起作用

标签 java loops checkbox nested

我正在编写一个程序,该程序将有多个窗口,这些窗口将在它们之间传递值。 目前我正在测试我的程序的一部分,该部分由 1-99 个复选框组成。但是当我想通过单击按钮来检查它们的状态时,它就不起作用了。问题出在这里:

public void actionPerformed(ActionEvent event) {



    if(event.getSource() == okay) {
        for(int i=0;i<box.length; i++){
            for(int j=0;j<box.length; j++){
                if((i==0)&&(j==0)) continue;                    
                if(box[i][j].getState())
                asdf.matra[i][j]=true;
                System.out.println(box[i][j].getLabel() + " is " + asdf.matra[i][j]);
                }       
        }

    }


}    

这是主类:

    public class asdf {
    public static boolean matra[][] = new boolean[10][10];

    public static void main(String arg[]) {

    for(int ii=0;ii<matra.length; ii++){
        for(int jj=0;jj<matra.length; jj++){
            matra[ii][jj]=false;
            }           
    }
    new JFrameDemo();
}
}

和另一个类:

import java.awt.*;

import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class JFrameDemo extends Frame implements ActionListener, ItemListener {
    Checkbox box[][] = new Checkbox[10][10];
    Button okay;



JFrameDemo() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    add(makePanel());
    pack();
    show();

}
private Panel makePanel() {


    GridBagConstraints con = new GridBagConstraints();
    Panel panel = new Panel();
    GridBagLayout gridbag = new GridBagLayout();
    panel.setLayout(gridbag);

    for(int i=0;i<box.length; i++){
        for(int j=0;j<box.length; j++){
            if((i==0)&&(j==0)) continue;                    

            box[i][j] = new Checkbox(i+j*10+"");
            con.gridx = i;
            con.gridy = j;
            panel.add(box[i][j],con);
            }           
    }

    okay = new Button("Unesi");
    con.gridx = 10;
    con.gridy = 10;
    panel.add(okay,con);

    return(panel);
}
public void actionPerformed(ActionEvent event) {

    if(event.getSource() == okay) {
        for(int i=0;i<box.length; i++){
            for(int j=0;j<box.length; j++){
                if((i==0)&&(j==0)) continue;                    
                if(box[i][j].getState())
                asdf.matra[i][j]=true;
                System.out.println(box[i][j].getLabel() + " is " + asdf.matra[i][j]);
                }       
        }

    }


}
public void itemStateChanged(ItemEvent event) {

}
public void processWindowEvent(WindowEvent event) {
    if(event.getID() == WindowEvent.WINDOW_CLOSING)
        System.exit(0);
}
}

程序正在运行,没有任何错误,但控制台没有给出任何结果。它也应该将值传递给全局变量。我认为嵌套 for 存在问题。

最佳答案

您忘记将 ActionListener 添加到按钮。

okay = new Button("Unesi");
okay.addActionListener(this);

关于java - CheckBox 矩阵出现问题,循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13293590/

相关文章:

java - 一次性为所有 JSP 页面启用 JSTL 支持(不是基于 "per-every-single-page")

幕后的Java 8方法引用

arrays - 这是想告诉我什么?

c - 使用浮点变量作为循环计数器及其在非 "=="条件下的小数递增/递减有任何风险吗?

python - 对于变量 URL 的循环列表 Python

android - 保存复选框值

javascript - 在输入类型复选框中绑定(bind) ng-models

php - 我正在尝试将复选框连接到 PHP 中的 SQL 语句中

java - 枚举的实现

java - Spring RestController 不明确的映射错误