首次单击后禁用JButton

时间:2016-09-08 09:55:27

标签: java arraylist jbutton actionlistener point

我能够使用此代码在一个圆圈中创建大约11个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,这样每个Button只能在一次点击时激活,之后它会将颜色变为绿色!我不知道如何做到这一点!谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

在按钮的动作侦听器中尝试执行:

button.setEnabled(false);

它应该有用。

答案 1 :(得分: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(我的个人选择):

Beginner implements ActionListener {

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

            ...

            quest.addActionListener(this);

}