我有一个JPanel,它包含一个JPanel和一个JButtons。 在内部JPanel中,我有一些最初选择的JCheckBox。我已经向我的JButtons添加了一个actionListener,用于检查每个JCheckBox是否更改为未选择的动作执行。 但它不起作用。这是我的代码,问题出在哪里?
public LimitPanel(String[] dates, int column) {
GridLayout layout = new GridLayout(1 + dates.length, 0);
setLayout(layout);
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
setVisible(true);
this.dates = dates;
checks = new JCheckBox[dates.length][column-1];
for (int i = 0; i < dates.length; i++) {
for (int j = 0; j < column - 1; j++) {
checks[i][j] = new JCheckBox();
checks[i][j].setSelected(true);
checks[i][j]
.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
add(checks[i][j]);
}
}
}
public JCheckBox[][] getChecks() {
return checks;
}
在我的主要Clas中,我有另一种方法,包含:
ResultSet e = connect.select("Invi", "*");
try {
while (e.next()) {
final int inviID = e.getInt("inviID");
JPanel pn = new JPanel();
pn.setSize(d.width, d.height);
pn.setLocation(0, 0);
pn.setLayout(new BoxLayout(pn, BoxLayout.PAGE_AXIS));
lp = new LimitPanel(st, 6);
pn.add(lp);
JButton sabt = new JButton(" ثبت ");
sabt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.out.println("saaaaaaaaaaaaaaaabt");
JCheckBox[][] jcb = new JCheckBox[lp.getDates().length][5];
jcb = lp.getChecks();
for (int i = 0; i < lp.getDates().length; i++)
for (int j = 0; j < 5; j++) {
if (!jcb[i][j].isSelected()) {
System.out.println("naaaaaaaaa");
connect.insertLimit(inviID, (lp
.getDates())[i], j+"");
}
}
}
});
pn.add(sabt);
panels.add(pn);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
setContentPane(panels.get(p));
revalidate();
我编辑它以包含必要的内容,我的问题是当我按下按钮时System.out.println("saaaaaaaaaaaaaaaabt");
始终有效但我对复选框System.out.println("naaaaaaaaa");
的操作无效。
答案 0 :(得分:2)
您使用while
(while (e.next()) {
)来构建部分程序。在其中,您可以在每次迭代时创建对lp
和JButton
,sabt
的新引用。
您的ActionListener
只能引用已创建的lp
的最后一个实例。这很可能是因为actionPerformed
正在做你认为应该做的事情......
想想这样......如果我这样做......
lp = new new LimitPanel(st, 6);
lp = new new LimitPanel(st, 6);
lp = new new LimitPanel(st, 6);
lp = new new LimitPanel(st, 6);
JCheckBox[] = lp.getChecks();
lp
的哪个实例我从
更新了更多详情
// Create a new instance of "LimitPanel"
lp = new LimitPanel(st, 6);
// You can check this by using the hashCode of the object...
System.out.println(lp.hashCode());
pn.add(lp);
JButton sabt = new JButton(" ثبت ");
sabt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
/*...*/
// Use what ever was last assigned to "lp"
jcb = lp.getChecks();
// You can check this by using the hashCode of the object...
System.out.println(lp.hashCode());
/*...*/
}
});
如果您确实希望ActionListener
使用LimitPanel
的特定实例,则应将该引用传递给ActionListener
的特殊实例...
例如......
lp = new LimitPanel(st, 6);
// You can check this by using the hashCode of the object...
System.out.println(lp.hashCode());
pn.add(lp);
JButton sabt = new JButton(" ثبت ");
sabt.addActionListener(new LimitActionHandler(lp));
LimitActionHandler
...
public class LimitActionHandler implements ActionListener {
private LimitPanel limitPane;
public LimitActionHandler(LimitPanel limitPane) {
this.limitPane = limitPane;
}
@Override
public void actionPerformed(ActionEvent arg0) {
/*...*/
// Use what ever was last assigned to "lp"
jcb = limitPane.getChecks();
// You can check this by using the hashCode of the object...
System.out.println(limitPane.hashCode());
/*...*/
}
}
正如我在评论中所述,我认为从JCheckBox
公开LimitPanel
es是一个坏主意,因为它允许您的应用程序的其他部分不受限制地访问这些对象,不需要工作......
JCheckBox[] jcb = limitPane.getChecks();
for (JCheckBox cb : jcb) {
cb.setSelected(false); //...
}
for (JCheckBox cb : jcb) {
cb.getParent().remove(cb); //...
}
这非常危险。您可以争辩说您的应用程序不会执行这些操作,但您无法阻止它发生...