繁体   English   中英

JCheckBox不会触发isselected

[英]JCheckBox doesn't trigger isselected

我有一个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"); 当我按下按钮时总是有效,但我用checkBoxes System.out.println("naaaaaaaaa");做什么System.out.println("naaaaaaaaa"); 永远不会奏效

你使用一段whilewhile (e.next()) { )来构建程序的一部分。 在其中你的每一次迭代sabt创建一个对lpJButtonsabt的新引用。

您的ActionListener只能引用创建的lp LAST实例。 这很可能就是为什么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());
        /*...*/
    }
}

正如我在评论中所说的那样,我认为从LimitPanel公开JCheckBox是一个坏主意,因为它允许应用程序的其他部分无限制地访问那些对象,而这些对象并不需要工作......

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

这非常危险。 您可以争辩说您的应用程序不会执行这些操作,但您无法阻止它发生...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM