如何禁用JPanel中的所有按钮,并依次启用它们

时间:2016-10-01 15:27:51

标签: java arrays swing if-statement point

我创建了一个带有11个按钮的JPanel(全部在一个数组中)。现在我在这些按钮上设置了actionListeners,但是我想最初只启用一个按钮,然后在完成actionListener之后,启用下一个按钮。这是我的代码!

public class Beginner extends JPanel {

    static JButton quest;
    Random rand = new Random();

    int n = 10;

    static List <Point> points = new ArrayList<Point> ();

    Quest1 q1;
    Quest2 q2;
    Quest3 q3;
    Quest4 q4;
    Quest5 q5;
    Quest6 q6;
    Quest7 q7;
    Quest8 q8;
    Quest9 q9;
    Quest10 q10;
    Quest11 q11;

    public Beginner() {

        int radius = 200;
        Point center = new Point (250, 250);

        double angle = Math.toRadians(360 / n);      
        points.add(center);
        for (int i = 0; i < n; i++) {
            double theta = i * angle;

            int dx = (int) (radius * Math.sin(theta));    
            int dy = (int) (radius * Math.cos(theta));
            Point p = new Point (center.x + dx , center.y + dy);
            points.add(p);
        }
        draw (points);
    }

    public void draw (List<Point> points) {
        JPanel panels = new JPanel();
        SpringLayout spring = new SpringLayout();
        int count = 1;
        for (Point point: points) {
            quest = new JButton("Question " + count);
            quest.setForeground(Color.BLACK);
            Font fonte = new Font("Script MT Bold", Font.PLAIN, 20);
            quest.setFont(fonte);
            add (quest);
            count++;
            spring.putConstraint(SpringLayout.WEST, quest, point.x, SpringLayout.WEST, panels );

            spring.putConstraint(SpringLayout.NORTH, quest, point.y, SpringLayout.NORTH, panels );
            setLayout(spring);
            panels.setOpaque(false);
            panels.setVisible(true);
            panels.setLocation(5,5);
            add(panels);
            //I want to disable all but one button at this point

            quest.addActionListener (new ActionListener() {
                public void actionPerformed (ActionEvent p) {
                    if (point.equals(points.get(0))) {
                        q1 = new Quest1();
                        //then, enable the next after this is completed
                    }   
                    else if (point.equals(points.get(1))) {
                        q2 = new Quest2();
                        //same comment in Quest1 follows....and so on!
                    }
                    else if (point.equals(points.get(2))) {
                        q3 = new Quest3();
                    }
                    else if (point.equals(points.get(3))) {
                        q4 =  new Quest4();
                    }
                    else if (point.equals(points.get(4))) {
                        q5 = new Quest5();
                    }
                    else if (point.equals(points.get(5))) {
                        q6 = new Quest6();
                    }  
                    else if (point.equals(points.get(6))) {
                        q7 = new Quest7();
                    }  
                    else if (point.equals(points.get(7))) {
                        q8 = new Quest8();
                    }  
                    else if (point.equals(points.get(8))) {
                        q9 = new Quest9 () ;
                    }
                    else if (point.equals(points.get(9))) {
                        q10 = new Quest10() ;
                    }
                    else if (point.equals(points.get(10))) {
                        q11 =  new Quest11 ();
                    }
                } 
            });
        }
    }
}

1 个答案:

答案 0 :(得分:2)

如果你想顺序启用按钮,我会使用至少两个字段,一个ArrayList<JButton>填充你的按钮,一个int buttonListIndex int索引字段告诉程序哪个按钮是活动的。您可以通过setEnabled(true)启用和停用按钮。在ActionListener中,递增索引int并使用for循环遍历所有按钮,禁用除索引之外的所有按钮。我将把代码实现留给您。