2016-09-13 1 views
0

내 코드에 문제가 있습니다! ... 아래 코드로 JPanel의 순환 양식에 12 개의 JButton 어레이를 만들 수있었습니다! ... 각 JButton에 actionListener를 설정하고 Jbutton 중 하나가 클릭되면 다른 것들을 비활성화하고 싶습니다 .... 나중에 사용할 수있게 ..... 더 많은 이해를 위해 여기 내 코드버튼을 클릭 할 때 패널의 구성 요소를 비활성화하는 방법

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

관련 문제