我有一个 JPanel,其中包含一个 JPanel 和一个 JButtons。 在内部 JPanel 中,我有一些最初选择的 JCheckBoxes。我已向 JButton 添加了一个 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;
}
在我的主类中,我有另一个方法,其中包含:
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("saaaaaaaaaaaaaaaaabt");
当我按下按钮时总是有效,但我用它做什么checkBoxes System.out.println("naaaaaaaaa");
永远不起作用。
最佳答案
您使用 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
是一个坏主意。 es 来自LimitPanel
,因为它允许应用程序的其他部分不受限制地访问这些对象,而它们不需要这些对象......
JCheckBox[] jcb = limitPane.getChecks();
for (JCheckBox cb : jcb) {
cb.setSelected(false); //...
}
for (JCheckBox cb : jcb) {
cb.getParent().remove(cb); //...
}
这是非常危险的。您可以争辩说您的应用程序不会执行这些操作,但您无法阻止它发生......
关于java - JCheckBox不触发isselect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18322894/