Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 第一次单击后禁用JButton_Java_Arraylist_Jbutton_Actionlistener_Point - Fatal编程技术网

Java 第一次单击后禁用JButton

Java 第一次单击后禁用JButton,java,arraylist,jbutton,actionlistener,point,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 = ne

我能够用这个代码在一个圆圈中创建大约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.setEnabled(false);

它应该可以工作。

在按钮的动作侦听器中,尝试执行以下操作:

button.setEnabled(false);

它应该可以工作。

您应该为所有按钮添加一个侦听器:

方法1:

方法2:

方法3我个人的选择:


您应该为所有按钮添加一个侦听器:

方法1:

方法2:

方法3我个人的选择:


非常感谢你的可能的重复!我很感激!!第一个很好用,但我仍然会考虑第二个,还没有试过呢!。。。。但再次感谢!这三个是做同样的事情,他们只是写得不同。非常感谢!我很感激!!第一个很好用,但我仍然会考虑第二个,还没有试过呢!。。。。但再次感谢!这三个都在做同样的事情,只是写得不同而已。不!。。。这将概括它并禁用最后一个按钮……而不是所选按钮!!不!。。。这将概括它并禁用最后一个按钮……而不是所选按钮!!
    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);
                }
    }
Beginner implements ActionListener {

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

            ...

            quest.addActionListener(this);

}