1

我正在编写一个程序,它将有多个窗口,它们将在它们之间传递一个值。目前我正在测试由 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);
}
}

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

4

1 回答 1

1

您忘记将 ActionListener 添加到按钮。

okay = new Button("Unesi");
okay.addActionListener(this);
于 2012-11-08T20:38:24.900 回答