2016-09-13 4 views
0

私のコードにいくつかの問題があります!... 12個のJButtonの配列を以下のコードでJPanelの円形で作成できました! ...各JButtonにactionListenerを設定していて、Jbuttonの1つがクリックされた後で他のものを無効にしたいのですが....後で有効にします.....もっと理解するために、私のコードはボタンをクリックしたときにパネルのコンポーネントを無効にする方法

int n = 10; 


public Beginner() { 

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

      double angle = Math.toRadians(360/n); 

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

      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); 
     quest.addActionListener(new ActionListener(){ 
       public void actionPerformed (ActionEvent q) { 
       if (point.equals(points.get(0))) { 

       //Some action.... 
       //It is at this point that every other Jbutton in the panel is to be disabled until the action ends..... It is here that I need help!!! 
です

答えて

2

このソリューションを試してみてください。

  quest.addActionListener(new java.awt.event.ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        JButton b = (JButton)e.getSource(); 
        for(Component c : yourPanel.getComponents()){ 
         if(c instanceof JButton && !c.equals(b)){ 
          c.setEnabled(false); 
         } 
        } 
       } 
      }); 
+0

あなたのコードのロジックが良いです。 @ Presh_K7はテストを行います。 – cdaiga

+0

まあ、クールなコードなんだけど、適用するのが難しいと思って......アクションの前後に来なければならない@Abihabi87 –

+0

@ Presh_K7:アクションの前後にはどういう意味ですか? '?あなたの 'quest.addActionListener ...'をコードで置き換えてください。Abihabi87 – hamena314

関連する問題