0

我有一个 JPanel,其中包含一个 JPanel 和一个 JButton。在内部 JPanel 中,我有一些最初选择的 JCheckBoxes。我已经在我的 JButtons 中添加了一个 actionListener,用于检查是否每个 JCheckBoxes 都更改为未选中的动作执行。但它不起作用。这是我的代码,问题出在哪里?

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;
}

在我的 Main 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");

4

1 回答 1

2

您使用while( while (e.next()) {) 来构建程序的一部分。在其中,您可以在每次迭代中 创建对lpand的新引用。JButtonsabt

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 中公开 es是一个坏主意LimitPanel,因为它允许应用程序的其他部分不受限制地访问这些对象,而它们不需要这些对象来工作......

JCheckBox[] jcb = limitPane.getChecks();
for (JCheckBox cb : jcb) {
    cb.setSelected(false); //...
}
for (JCheckBox cb : jcb) {
    cb.getParent().remove(cb); //...
}

这是非常危险的。你可以争辩说你的应用程序不会做这些事情,但你不能阻止它发生......

于 2013-08-19T21:26:29.793 回答