简体   繁体   English

JSpinner在动作监听器中不起作用

[英]JSpinner doesn't work in action listener

I have an actionlistener inside another actionlistener when i create my jspinner inside the outer one it works but in the inner one it doesn't. 当我在外面的jspinner中创建我的jspinner时,我在另一个actionlistener的里面有一个actionlistener,但在里面的jspinner中却没有。 this is my code.what would the problem be? 这是我的代码。问题是什么?

this is the code which doesn't work and if I put sp and js out of the inner action listener it works. 这是无效的代码,如果我将sp和js放在内部动作侦听器之外,它将起作用。

    menuItem = new JMenuItem("Insert Exams", KeyEvent.VK_E);
    menuItem.addActionListener(new ActionListener() {


        @Override
        public void actionPerformed(ActionEvent arg0) {
            count = 0;
            jt = new ArrayList[3];
            for (int i = 0; i < 3; i++)
                jt[i] = new ArrayList<JTextField>();
            panel = new JPanel();
            panel.setLocation(0, 0);
            panel.setSize(d.width, d.height);
            panel.setLayout(null);

            JButton add = new JButton("add Exam");
            add.setSize(120, 80);
            add.setLocation(250, 100);


            add.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    sp = new SpinnerNumberModel(1, 1, 5, 1);
                    js = new JSpinner(sp);
                    js.setSize(100, 30);
                    js.setLocation(450, 80 + count * 50);
                    panel.add(js);

                    for (int i = 1; i < 3; i++) {
                        JTextField jt1 = new JTextField(20);
                        jt1.setSize(150, 30);
                        jt1.setLocation(450 + i * 200, 80 + count * 50);

                        jt[i].add(jt1);
                        panel.add(jt1);
                    }
                    count++;
                    repaint();
                }
            });
            panel.add(add);

            setContentPane(panel);

        }
    });

You should be using revalidate instead of repaint . 您应该使用revalidate而不是repaint

You should also be relying on a LayoutManager rather then using setSize and setLocation 您还应该依赖LayoutManager而不是使用setSizesetLocation

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

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