【发布时间】:2012-11-08 16:44:42
【问题描述】:
我正在编写一个程序,它将有多个窗口,它们将在它们之间传递一个值。 目前,我正在测试由 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 存在问题。
【问题讨论】:
-
代码的哪一部分有问题?您当然不希望我们浏览您的整个代码,对吧?
-
它确实在那里,这是我放的第一个代码段。 Fors 似乎不起作用,我已经尝试使用 Checkbox 数组并且它确实有效,我启动了复选框矩阵,但是当我应该单击 OK 按钮并检查它们的状态时,用 fors 将它们全部通过它只是没有' t.
标签: java loops checkbox nested