samedi 1 octobre 2016

How to disable all but one button in a JPanel and enable them one after the other

I created a JPanel with 11 buttons (all in an array). Now I set actionListeners on those buttons, but I want only one button to be enabled initially, then after the actionListener on it is completed, the next button is enabled. Here is my code!

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 ();

        }           
                } 
                       });
                   }

                        }
}

Aucun commentaire:

Enregistrer un commentaire