如何在不同的按钮上使用动作侦听器 class

how to use action listener on a button in a different class

我对 GUI 还很陌生,在完成这项作业时遇到了问题。基本上我们总共有八个 classes,必须以三种不同的方式使用三个面板 classes,一种方式仅使用 TopPanel 和 InitialPanel classes,一种方式仅使用 BottomPanel 和 InitialPanel class,还有一个仅使用 InitialPanel class。目标是在按下顶部面板中的按钮时,使底部面板中的按钮显示有关足球运动员的信息。 `

public class BottomPanel extends JPanel implements ActionListener
{
    public JButton b1;
    public BottomPanel(final JPanel topPanel)
    {
        super();
        setBackground(Color.pink);
        //setLayout(new GridLayout(3,1));
        b1 = new JButton("When the user clicks on the button in the UPPER panel, displays the football player's position here" );

        add(b1);
    }

    public void actionPerformed(ActionEvent event)
    {
        Object obj = event.getSource();
        if(obj == b1)
        {
            b1.setText(fp1.getPosition());
        }
    }




}



public class InitialPanel extends JPanel 
{
    public InitialPanel()
{
    super();
    setBackground(Color.gray);
    setLayout(new BorderLayout());

    TopPanel p1 = new TopPanel();
    add(p1,"North");

    BottomPanel p2 = new BottomPanel(p1);
    add(p2,"Center");




}


}`

public class TopPanel extends JPanel
{   
    public TopPanel()
    {
        super();
        setBackground(Color.yellow);        
        footballPlayer fp1 = new footballPlayer("Mark","Allen",22, "IST", 5.6f, 180, "Junior","Running Back");
        // the whatsUp of this student has to shown in the other panel      
        JButton jl1 = new JButton(fp1.getInfo());
        add(jl1);
    }
}`

我想我得到了唯一的 TopPanel 和 InitialPanel 运行,但我不知道如何处理其他两个。此外,getInfo() 是在设置底部按钮的文本时调用的方法,我们不能创建另一个 footballplayer 对象,而不是在 TopPanel 中使用的对象。任何帮助将不胜感激!

您已经将 jl1 按钮添加到 TopPanel,现在您应该向该按钮添加一个侦听器:您可以将其添加到 BottomPanel class。

您在 BottomPanel 构造函数中收到对 topPanel 的引用,因此您可以将他保留为成员并创建侦听器,如下所示:

public class BottomPanel extends JPanel implements ActionListener
{
    public JButton b1;

    /** added member to topPanel **/
    private JPanel mTopPanel;

    public BottomPanel(final JPanel topPanel)
    {
        super();
        setBackground(Color.pink);

        this.mTopPanel = topPanel;

        b1 = new JButton("When the user clicks on the button in the UPPER             
        panel, displays the football player's position here" );

        add(b1);

        /** the topPanel jli button listener **/
        this.mTopPanel.jl1.addActionListener(new ActionListener()
        {
             public void actionPerformed(ActionEvent e)
             {
                 /** edit your button with the info of the player **/
                 b1.setText("player info added here");
             }
        });
    }

    public void actionPerformed(ActionEvent event)
    {
        Object obj = event.getSource();
        if(obj == b1)
        {
            b1.setText(fp1.getPosition());
        }
    }
}

您应该将底部面板注册为顶部面板中按钮的 Listener。为此,您应该制作一个列表,将所有监听器存储到 TopPanel class 中的按钮。并且一旦按下按钮,当前玩家的信息就会广播给所有的听众。而你需要做的是让 ButtonPanel class 实现 Listener 在这里我称之为 DisplayPlayerInfoListener.

class TopPanel extends JPanel {

    JButton displayButton = new JButton("show player info");

    FootballPlayer currentPlayer = new FootballPlayer();
    List<DisplayPlayerInfoListener> listeners = new ArrayList<>();

    public TopPanel() {
        add(displayButton);
        displayButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                displayButtonPressed();
            }
        });
    }

    void displayButtonPressed() {
        for (DisplayPlayerInfoListener listener : listeners) {
            listener.displayPlayer(currentPlayer);
        }
    }

    public void addDisplayPlayerInfoListener(DisplayPlayerInfoListener listener) {
        listeners.add(listener);
    }
}

interface DisplayPlayerInfoListener {
   void displayPlayer(FootballPlayer player);
}

class BottomPanel extends JPanel implements DisplayPlayerInfoListener {

    @Override
    public void displayPlayer(FootballPlayer player) {
        // show the player's information on your bottom panel's UI
    }
}

在代码中的某处,您需要将底部面板添加为顶部面板按钮的侦听器。

topPanel.addDisplayPlayerInfoListener(bottomPanel);

通过这样做,您可以注册任意数量的侦听器,因为此信息可能还需要广播到其他 UI 组件。此外,它将两个面板分开,您不需要通过访问方法上的其他面板字段之一来获取信息。信息将在准备好发送时进行处理。 TopPanel 仅将 BottomPanel 视为 DisplayPlayerInfoListener 实例,因此耦合较少。