java - 第一次点击后禁用 JButton

标签 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 应该只在一次单击时处于 Activity 状态,然后它会将其颜色更改为绿色!我不知道该怎么做!感谢您的帮助!

最佳答案

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

方法一:

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

方法二:

    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);

}

关于java - 第一次点击后禁用 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39387875/

相关文章:

java - main 的 JPanel 未从另一个类更新

java - FileWriter 上出现意外的 FileNotFoundException

java - 通过 JSON 发送 java.lang.Boolean (Spring Boot)

java - Android 存储和检索数据列表的最佳方法

java - 从 Java ArrayList 中删除项目时接收 IndexOutOfBoundsException

java - 自动调整 JButton 图标大小

java - 从 JTree 事件获取节点值到另一个事件按钮按下

Java正则表达式匹配模式并提取它们

java - Eclipse [桌面 headless ]

java - 在 Java 中,如何迭代 ArrayList 的子列表?