2016-09-08 27 views
1

我能創建使用此代碼一圈約11 Jbutton將......禁用的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); 
} 
} 
} 

現在,我要爲每個JButton的一個ActionListener,它是這樣每個按鈕應該只有一次點擊纔有效,之後它會將其顏色更改爲綠色!我不知道該怎麼做!謝謝你的幫助!

+1

可能重複[禁用按鈕點擊befor actionPerformed是競爭的java](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

這三個是做同樣的事情,他們只是寫不同的。 – Damiano

1

按鈕的動作監聽者儘量做到:

button.setEnabled(false); 

它應該工作。

+0

不!!這將推廣它,並禁用最後一個按鈕.......不是選定的按鈕! –