禁用匿名内部类中的按钮

发布于 2024-11-15 04:51:54 字数 951 浏览 3 评论 0原文

我有这行代码,我想在添加乘客后禁用该按钮。我想禁用该按钮。 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.
        }
    });
}

I have these line of code and I want to disable the button after a passenger has been added. I want to disable the button. seats[i].setEnabled(false) won't work since it's inside an anonymous inner class.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扛刀软妹 2024-11-22 04:51:54

因为当您这样做时:

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

Because when you do:

setEnabled(false);

inside the anonymous inner class, you're calling that method on the ActionListener instance. not the JButton.

Try this:

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);
        }
    });
}
望喜 2024-11-22 04:51:54

一种方法是:

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

EG

这是基于您早期代码的 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);
    }
}

屏幕截图

在此处输入图像描述

One way is:

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

E.G.

Here is an SSCCE based on your earlier code.

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

Screenshot

enter image description here

失与倦" 2024-11-22 04:51:54

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

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

Try to export it as a class field, it should work that way:

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.
                }
             });
}
拒绝两难 2024-11-22 04:51:54

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

Try seats[i].setEnabled(false);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文