Java 如何修复此ActionListener以将字符串传回小部件

Java 如何修复此ActionListener以将字符串传回小部件,java,actionlistener,jtextfield,jtextarea,Java,Actionlistener,Jtextfield,Jtextarea,我是Java新手,只是在练习ActionListeners。作为我处理的应用程序的一部分,我将有一个JTextField,允许用户搜索名称,然后是一个JTextArea,显示搜索结果。我有一个用于搜索和细化名称的api,唯一的问题是将小部件连接到方法和操作侦听器文件 以下是代码的一些部分: 小部件文件: //Text Field JTextField searchbox = new JTextField(); leftSide.add(searchbox, cnt); String userT

我是Java新手,只是在练习ActionListeners。作为我处理的应用程序的一部分,我将有一个JTextField,允许用户搜索名称,然后是一个JTextArea,显示搜索结果。我有一个用于搜索和细化名称的api,唯一的问题是将小部件连接到方法和操作侦听器文件

以下是代码的一些部分:

小部件文件:

//Text Field
JTextField searchbox = new JTextField();
leftSide.add(searchbox, cnt);

String userText = searchbox.getText();
ActionListener sendsText = new SearchListener(search box);
searchbox.addActionListener(sendsText);


//TextArea              
JTextArea stationList = new JTextArea(12, 0);
leftSide.add(stationList, cnt);

String entered = userText;
stationList.append(entered);
SearchListener:

    public class SearchListener implements ActionListener {
      private JTextField searchbox;
      private JTextArea stationList;


    public SearchListener(JTextField search box) {
        this.searchbox = searchbox;
    }
    public void ListF(JTextArea stationList){
        this.stationList = stationList;

    public void actionPerformed(ActionEvent event) {
       XXXX<NAMES> stations = HHHH.SimilarNames(searchbox.getText());

        for (NAMES station : stations) {
            //System.out.println(station);

*Problem*>    String result = (searchbox.getText());
*Problem*>    stationList.append(result);

    }
公共类SearchListener实现ActionListener{
专用JTextField搜索框;
私人区域统计人员;
公共SearchListener(JTextField搜索框){
this.searchbox=searchbox;
}
公共作废列表(JTextArea stationList){
this.stationList=stationList;
已执行的公共无效操作(操作事件){
XXXX stations=hhh.SimilarNames(searchbox.getText());
用于(站点名称:站点){
//系统输出打印LN(站);
*问题*>字符串结果=(searchbox.getText());
*问题*>stationList.append(结果);
}
所以在这个程序中,TextFiels是连接的,ActionListener工作,但它在CMD中以类似的名称打印出列表(我在这里对其进行了注释)。但我希望它将列表发送回API中的文本区域。(小部件文件)。因此我不确定SearchListener顶部的ActionListener方法是否正确。代码中的问题>,是我试图将搜索结果传递到文本字段,但这不起作用

有人知道怎么修理吗


感谢是前进。

我想你可能在扭转一些事情,但也许这就是你想要实现的目标:

搜索字段的值决定文本区域填充的内容

如果是这种情况,那么必须做一些更改。首先,在UI事件发生时,只执行ActionListener中的代码,因此没有理由在初始化期间对任何UI元素调用
getText()

其次,在此UI中添加一个按钮大大简化了逻辑。如果将侦听器连接到搜索框,则会出现诸如“我何时知道用户已完成输入文本?”和“我如何处理部分文本?”之类的问题,但是使用“搜索”按钮将此逻辑置于用户手中

单击“搜索”按钮后,将触发附加到该按钮的侦听器的操作事件,并且
stationListArea
将填充
similarNames()
的结果

请注意,尽管下面显示的不是执行此任务的最干净的方式(这将涉及字段和匿名内部类),但它是简单易懂的

小部件(不确定cnt是什么,所以我在下面的示例代码中省略了它)

搜索侦听器

注意:每次单击按钮时,此处的逻辑将继续将文本添加到
stationListArea
,并且
hhh.SimilarNames()
返回一个值。要使
stationListArea
每次都用新文本更新,替换旧文本,请添加
stationListArea.setText(“”)
actionPerformed()的开头

公共类SearchListener实现ActionListener{
专用JTextField搜索框;
私人JTextArea stationListArea;
公共搜索侦听器(JTextField searchBox、JTextArea stationListArea){
this.searchBox=searchBox;
this.stationListArea=stationListArea;
}
已执行的公共无效操作(操作事件){
XXXX stations=hhh.SimilarNames(searchBox.getText());
用于(站点名称:站点){
stationListArea.append(车站);
}
}
}

根据主要评论中的反馈进行更新

您的
名称
对象需要某种方式来提供自身的
字符串
表示。这样,您就可以将该值附加到文本区域中

public class SearchListener implements ActionListener {
    private JTextField searchbox;
    private JTextArea stationList;

    public SearchListener(JTextField search box) {
        this.searchbox = searchbox;
    }

    public void actionPerformed(ActionEvent event) {
        XXXX<NAMES> stations = HHHH.SimilarNames(searchbox.getText());

        for (NAMES station : stations) {
            stationList.append(station.getLocationName()());
        }

}
公共类SearchListener实现ActionListener{
专用JTextField搜索框;
私人区域统计人员;
公共SearchListener(JTextField搜索框){
this.searchbox=searchbox;
}
已执行的公共无效操作(操作事件){
XXXX stations=hhh.SimilarNames(searchbox.getText());
用于(站点名称:站点){
append(station.getLocationName()());
}
}

你不想做一些类似于
stationList.append(station)
的事情,而不是使用
searchBox
中的文本吗?@MadProgrammer我想到了这一点,但在Eclipse上它给出了一个错误:JTextArea类型中的append(String)方法不适用于参数(名称)您需要为append方法提供名称的字符串形式,例如stationList.append(NAMES.toString());或者您用于返回名称的字符串表示形式的任何方法都需要提供一个可以返回自身表示形式的
String
方法(例如
getName
),然后您可以使用
station.getName()
将其附加到文本区域检查我留下的答案。听起来您需要使用
station.getLocationName()
public class SearchListener implements ActionListener {
    private JTextField searchBox;
    private JTextArea stationListArea;

    public SearchListener(JTextField searchBox, JTextArea stationListArea) {
        this.searchBox = searchBox;
        this.stationListArea = stationListArea;
    }

    public void actionPerformed(ActionEvent event) {
        XXXX<NAMES> stations = HHHH.SimilarNames(searchBox.getText());

        for (NAMES station : stations) {
            stationListArea.append(station);
        }
    }
}
public class SearchListener implements ActionListener {
    private JTextField searchbox;
    private JTextArea stationList;

    public SearchListener(JTextField search box) {
        this.searchbox = searchbox;
    }

    public void actionPerformed(ActionEvent event) {
        XXXX<NAMES> stations = HHHH.SimilarNames(searchbox.getText());

        for (NAMES station : stations) {
            stationList.append(station.getLocationName()());
        }

}