简体   繁体   English

当我尝试将ActionListener应用于JButton更改JLabel的内容时,没有任何反应

[英]Nothing happens when I try to apply an ActionListener to a JButton to change the content of the JLabel

I need a way to create an ActionListener that when a JButton is pressed, it updates the content of 7 different JLabels to display the information in the form of text. 我需要一种创建ActionListener的方法,当按下JButton时,它会更新7个不同JLabel的内容,以文本形式显示信息。

The data is retrieved from methods called from an external JAR file. 数据是从外部JAR文件调用的方法中检索的。 The methods return ArrayList. 该方法返回ArrayList。 I attempted to convert the ArrayList into a String, and tried to change the JLabel content with setText(). 我试图将ArrayList转换为String,并尝试使用setText()更改JLabel内容。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import api.anAPI.THEAPINAME;


public class Controller implements ActionListener {

    private MainGUI maingui;
    private SubPanel subpanel;
    private static THEAPINAME anAPI =new THEAPINAME("XyP0D75oRCGrLE78","x47ka5jmOGaJ2zvw");
    static ArrayList<String> nameList =new ArrayList<String>();
private String names;


     public Controller(MainGUI maingui,SubPanel subpanel){

        this.maingui = maingui;
        this.SubPanel = subpanel;

        MainGUI.getSearchBtn().addActionListener(this);


        nameList.addAll(anAPI.getNames());

        for (String s: nameList){

         names+= s+"\t";
        }

    }

    public void actionPerformed(ActionEvent e) {

         SubPanel.label1.setText(names);
    //6 more Labels. 
    }

}

An additional, because I have 7 JLabels, would I need to do 7 getLabel methods? 另外,因为我有7个JLabel,我需要做7个getLabel方法吗? Or is there a way to get them all with just 1 method. 还是有一种方法仅用一种方法就能将它们全部获得。

I am not entirely sure what I am doing incorrectly, it could be that the getMethods I used returned the wrong widget in question as the code for the GUI was not done by me but by a teammate and he had done a really poor job of making it clear for us. 我不能完全确定我做错了什么,可能是因为我使用的getMethods返回的小部件有问题,因为GUI的代码不是我而是由队友完成的,而他的工作做得很差对我们来说很清楚。

UPDATE: Fixed up the GUI to make it clearer, so I think that is no longer the problem. 更新:修复了GUI,使其更清晰,所以我认为这不再是问题。 Now I think the problem might be that I did not convert the contents of the ArrayList into a String in the way I thought. 现在我认为问题可能是我没有按照我的想法将ArrayList的内容转换为String。

The desired function of the code is when the JButton is clicked on, the JLabels in question are all updated to their relevant data. 该代码的期望功能是,当单击JButton时,有问题的JLabel都将更新为它们的相关数据。

addController method addController方法

public void addController(Controller controller){
        control = controller;
        jb1.addActionListener(control);

    }

You didn't really describe what the problem is of your current code. 您并未真正描述当前代码的问题所在。

You can add a method say getLabels() in SubPanel class to return all of its labels, or you can add a method setLabelText(String text) to set text for all of its labels by extending or directly modifying SubPanel class. 您可以在SubPanel类中添加方法getLabels()以返回其所有标签,也可以添加方法setLabelText(String text)通过扩展或直接修改SubPanel类来SubPanel所有标签设置文本。

UPDATE 更新

You have several very confusing parts in your code. 您的代码中有几个非常混乱的部分。

In your constructor, it should be this.subpanel = subpanel and then it should be maingui.getSearchBtn().addActionListener(this) , also in method actionPerformed it should be subpanel.label1.setText(names) . 在构造函数中,应为this.subpanel = subpanel ,然后应为maingui.getSearchBtn().addActionListener(this) ,在actionPerformed方法中也应为subpanel.label1.setText(names) These might not be your problems though since you didn't say it's the code you're actually running. 尽管由于您没有说这实际上是您正在运行的代码,但这些可能不是您的问题。

Looks like that you haven't created any instance of class Controller thus the code in it never gets executed. 看来您尚未创建Controller类的任何实例,因此其中的代码永远不会执行。

You need to have some code outside of you Controller class like this: 您需要在Controller类之外添加一些代码,如下所示:

   MainGUI maingui;
   SubPanel subpanel;
   // they're somehow initialized

   Controller controller = new Controller(maingui, subpanel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM