将多个按钮设置为禁用

时间:2015-08-25 22:10:58

标签: java swing

我正在尝试这样做,当用户点击按钮时它将被禁用。我使用以下代码来实现这一目标:

            final JButton die1 = new JButton(theModel.letters.get(0));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn1){
                   die1.setEnabled(false);
               }
            });
            boggleGrid.add(die1);

theModel.letters.get(0)是一个字符串值,有效地工作。我的问题是当我尝试将同一代码用于同一个boggleGrid JPanel中的其他按钮时。我认为它可能与“final”语句有关,但actionListener必须能够运行。这是我的其余代码:

JPanel boggleGrid = new JPanel(new GridLayout(4,4));

            theModel.randomLetters();
            final JButton die1 = new JButton(theModel.letters.get(0));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn1){
                   die1.setEnabled(false);
               }
            });
            boggleGrid.add(die1);

            final JButton die2 = new JButton(theModel.letters.get(1));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn2){
                   die2.setEnabled(false);
               }
            });
            boggleGrid.add(die2);

            final JButton die3 = new JButton(theModel.letters.get(2));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn3){
                   die3.setEnabled(false);
               }
            });
            boggleGrid.add(die3);

            final JButton die4 = new JButton(theModel.letters.get(3));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn4){
                   die4.setEnabled(false);
               }
            });
            boggleGrid.add(die4);

            final JButton die5 = new JButton(theModel.letters.get(4));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn5){
                   die5.setEnabled(false);
               }
            });
            boggleGrid.add(die5);

            final JButton die6 = new JButton(theModel.letters.get(5));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn6){
                   die6.setEnabled(false);
               }
            });
            boggleGrid.add(die6);

            final JButton die7 = new JButton(theModel.letters.get(6));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn7){
                   die7.setEnabled(false);
               }
            });
            boggleGrid.add(die7);

            final JButton die8 = new JButton(theModel.letters.get(7));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn8){
                   die8.setEnabled(false);
               }
            });
            boggleGrid.add(die8);

            final JButton die9 = new JButton(theModel.letters.get(8));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn9){
                   die9.setEnabled(false);
               }
            });
            boggleGrid.add(die9);

            final JButton die10 = new JButton(theModel.letters.get(9));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn10){
                   die10.setEnabled(false);
               }
            });
            boggleGrid.add(die10);

            final JButton die11 = new JButton(theModel.letters.get(10));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn11){
                   die11.setEnabled(false);
               }
            });
            boggleGrid.add(die11);

            final JButton die12 = new JButton(theModel.letters.get(11));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn12){
                   die12.setEnabled(false);
               }
            });
            boggleGrid.add(die12);

            final JButton die13 = new JButton(theModel.letters.get(12));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn13){
                   die13.setEnabled(false);
               }
            });
            boggleGrid.add(die13);

            final JButton die14 = new JButton(theModel.letters.get(13));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn14){
                   die14.setEnabled(false);
               }
            });
            boggleGrid.add(die14);

            JButton die15 = new JButton(theModel.letters.get(14));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn15){
                   die15.setEnabled(false);
               }
            });
            boggleGrid.add(die15);

            JButton die16 = new JButton(theModel.letters.get(15));
            die1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent btn16){
                   die16.setEnabled(false);
               }
            });
            boggleGrid.add(die16);

        frame.add(boggleGrid, BorderLayout.CENTER);

背景:我正在制作一个Boggle游戏(如果有帮助的话)。感谢

1 个答案:

答案 0 :(得分:4)

您多次在同一addActionListener次呼叫JButton,例如:

final JButton die4 = new JButton(theModel.letters.get(3));
die1.addActionListener(new ActionListener() {

您正在创建一个名为die4的按钮,然后将ActionListener添加到die1。在这种情况下,您希望在addActionListener上调用die4

final JButton die4 = new JButton(theModel.letters.get(3));
die4.addActionListener(new ActionListener() {
^^^^

以下是您的代码实现,它更简洁,更容易维护:

JPanel boggleGrid = new JPanel(new GridLayout(4, 4));

ActionListener disableButtonListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event)
    {
        // event.getSource() will return a reference to the control that
        // fired the event. In this case, a JButton. We protect
        // ourselves from a ClassCastException just in case though.
        if (!(event.getSource() instanceof JButton)) {
            return;
        }

        ((JButton) event.getSource()).setEnabled(false);
    }
};

theModel.randomLetters();

for (int i = 0; i < 16; i++) {
    JButton button = new JButton(theModel.letters.get(i));
    button.addActionListener(disableButtonListener);
    boggleGrid.add(button);
}

frame.add(boggleGrid, BorderLayout.CENTER);