2016-09-08 2 views
1

나는 지금 ......비활성화하는 JButton 첫번째 후 클릭

public class Beginner extends JPanel { 
     private JButton quest; 
     public Beginner() { 

        int n = 10; //no of JButtons 
        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); //JButton is drawn about 10 times in a circle arragement 
          quest.setForeground(Color.BLUE); 
          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(10, 10); 

          add(panels); 
} 
} 
} 

를이 코드를 사용하여 원에서 약 11 JButton의를 만들 수 있었다, 나는 각각의 JButton에 대한 ActionListener를 작성해야하고, 그것은이다 그런 각 버튼은 클릭 한 번만 활성화해야합니다. 그 후에 색상은 초록색으로 바뀝니다. 그 방법에 대한 아이디어가 없습니다! 당신의 도움을 주셔서 감사합니다! 버튼의 액션 청취자에

+1

[클릭 befor actionPerformed에 단추를 해제 할 수 있습니다] 가능한 중복 복제] (http://stackoverflow.com/questions/17807846/disable-button-on-click-befor-actionperformed-is-competed-java) –

답변

1

:

방법 1 :

quest.addActionListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e) { 
        JButton source = (JButton) e.getSource(); 
        source.setEnabled(false); 
        source.setBackground(Color.GREEN); 
      } 
    }); 

방법 2 :

quest.addActionListener(new DisableButtonActionListener()); 

      ... 

    private class DisableButtonActionListener implements ActionListener { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
         JButton source = (JButton) e.getSource(); 
         source.setEnabled(false); 
         source.setBackground(Color.GREEN); 
       } 
    } 

방법 3 (내 개인의 choise) :

Beginner implements ActionListener { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
        JButton source = (JButton) e.getSource(); 
        source.setEnabled(false); 
        source.setBackground(Color.GREEN); 
      } 

      ... 

      quest.addActionListener(this); 

} 
+0

대단히 감사합니다! 나는 고맙다 !! –

+0

첫 번째 작품은 효과가 좋았지 만, 두 번째 작품은 계속 볼 것입니다. 아직 시도하지 않았습니다! ....하지만 다시 한 번 감사드립니다! –

+0

이 3 명은 똑같은 일을합니다. 그들은 다르게 쓰여졌습니다. – Damiano

1

수행하려고 : 그것은 작동해야

button.setEnabled(false); 

.

당신은 모든 버튼에 대한 리스너를 추가해야합니다
+0

아니요! ... 일반화하고 마지막 버튼 비활성화 ....... 선택한 버튼이 아님 !! –

관련 문제