java - 如何在不同类中的按钮上使用 Action 监听器

标签 java swing user-interface actionlistener

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

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() 是设置底部按钮文本时调用的方法,除了 TopPanel 中使用的足球运动员对象之外,我们无法创建另一个足球运动员对象。任何帮助将不胜感激!

最佳答案

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

您会在 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());
        }
    }
}

关于java - 如何在不同类中的按钮上使用 Action 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021935/

相关文章:

java - 从线程捕获 ActionEvents

java - Swing 不在 Applet 中绘图

java - 调用paint()方法时图像闪烁

java JScrollPane with JPanel with BoxLayout resizes elements

java - 使用 Gson 反序列化 Map<String, Object> 字段

java - 成员变量的局部变量的目的是什么?

java - Tomcat7 使用自定义文件部署应用程序

java - Android模态对话框堆叠

c - 如何退出xlib的XNextEvent的阻塞

html - 用于黑莓的嵌入式 HTML 控件?