【问题标题】:Disabling a button inside an anonymous inner class禁用匿名内部类中的按钮
【发布时间】:2011-06-12 09:52:59
【问题描述】:

我有这行代码,我想在添加乘客后禁用该按钮。我想禁用该按钮。 seats[i].setEnabled(false) 不起作用,因为它位于匿名内部类中。

JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
for (int i = 0; i < 40; i++)
{
    seats[i] = new JButton();//creating the buttons
    seats[i].setPreferredSize(new Dimension(50,25));//button width
    panel4seating.add(seats[i]);//adding the buttons to the panels
    final int seatingID = i;  // Create a local final variable so it can be passed to the anonymous innerClass...

    seats[i].addActionListener(new ActionListener()
    {  //anonymous inner class
        public void actionPerformed(ActionEvent evt)
        {  
            String firstName = showInputDialog();
            String lastName = showInputDialog();

            sw101.AddPassenger(firstName, lastName, seatingID);//adding a pasenger

            //I want to add a line here that disables the button.
        }
    });
}

【问题讨论】:

    标签: java class anonymous-inner-class


    【解决方案1】:

    因为当你这样做时:

    setEnabled(false);
    

    在匿名内部类中,您在 ActionListener 实例上调用该方法。 不是 JButton。

    试试这个:

    JButton [] seats = new JButton[40];
    
    for (int i = 0; i < 40; i++)
    {
        final JButton seat = new JButton();
        final int seatingID = i;
    
        seats[i] = seat;
        seat.setPreferredSize(new Dimension(50,25));
        panel4seating.add(seat);
    
        seat.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent evt)
            {  
                String firstName = showInputDialog();
                String lastName = showInputDialog();
    
                sw101.AddPassenger(firstName, lastName, seatingID);
    
                seat.setEnabled(false);
            }
        });
    }
    

    【讨论】:

      【解决方案2】:

      一种方法是:

      ((JButton)ae.getSource()).setEnabled(false);

      例如

      这是基于您之前的代码的 SSCCE。

      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
      
      public class GuiCreator extends JFrame
      {
          public GuiCreator()
          {
              super("Seats");
              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
              Container contentPane = getContentPane();
              contentPane.add(new SeatingPanel());
      
              pack();
      
              setVisible(true);
          }
      
          public static void main(String[] args) {
              new GuiCreator();
          }
      }
      
      class SeatListener implements ActionListener {
          @Override
          public void actionPerformed(ActionEvent ae) {
              //String firstName = showInputDialog();
              //String lastName = showInputDialog();
      
              //sw101.AddPassenger(firstName, lastName, seatingID);//adding a passenger
              ((JButton)ae.getSource()).setEnabled(false);
          }
      
          public String showInputDialog() {
              return JOptionPane.showInputDialog(null, "Enter Data");
          }
      }
      
      class SeatingPanel extends JPanel
      {
          public SeatingPanel()
          {
              super(new BorderLayout());
      
              JPanel panel4seating = new JPanel();//creating a grid panel
              panel4seating.setLayout(new GridLayout(4, 10));//setting the layout of the grid panel
      
              JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray
              ActionListener listener = new SeatListener();
              for (int i = 0; i < 40; i++)
              {
                  seats[i] = new JButton();//creating the buttons
                  //better to set the preferred size of the button
                  seats[i].setPreferredSize(new Dimension(50,25));
                  panel4seating.add(seats[i]);
                  seats[i].addActionListener(listener);
              }
      
              add(panel4seating, BorderLayout.CENTER);
          }
      }
      

      截图

      【讨论】:

      • 非常感谢它有效。 stackoverflow 社区非常有帮助。再次感谢大家的帮助。我应该开始在我的专业领域做出贡献,回答简单的问题:)
      【解决方案3】:

      尝试将其导出为类字段,它应该可以这样工作:

      public class Whatever {
      
      private JButton [] seats;
      private function whastsUpDude() {
      seats = new JButton [40]; //creating a pointer to the buttonsArray
              for (int i = 0; i < 40; i++)
              {
                  seats[i] = new JButton();//creating the buttons
                  seats[i].setPreferredSize(new Dimension(50,25));//button width
                  panel4seating.add(seats[i]);//adding the buttons to the panels
                  final int seatingID = i;  // Create a local final variable so it can be passed to the anonymous innerClass...
      
                  seats[i].addActionListener(new ActionListener()
                   {  //anonymous inner class
                      public void actionPerformed(ActionEvent evt)
                      {  
                          String firstName = showInputDialog();
                          String lastName = showInputDialog();
      
                          sw101.AddPassenger(firstName, lastName, seatingID);//adding a passenger
      
                          //I want to add a line here that disables the button.
                      }
                   });
      }
      

      【讨论】:

      • 我认为他的意思是像这样声明 JButton:private JButton [] seats
      【解决方案4】:

      试试seats[i].setEnabled(false);

      【讨论】:

      • 它不会工作,因为它在一个匿名内部类中它不会接受任何非最终变量
      • 我觉得这个答案不值得投反对票。您能否添加您对 Protostome 的答案所做的“评论”,作为对您答案的编辑? AFAIK 这是一个“正确”的答案,但只是做出了一些不同(可能更好)的设计假设。
      猜你喜欢
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多