0

私は 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);

検索リスナー:

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

    }

したがって、このプログラムでは、TextFiels が接続され、ActionListener が機能していますが、CMD の類似の名前のリストが出力されます (ここでコメントしました)。しかし、リストをAPIのテキストエリアに送り返したい(ウィジェットファイル)。したがって、 SearchListener の上部にある ActionListener メソッドが正しいかどうかはわかりません。また、コード内の問題> は、検索結果をテキスト フィールドに渡すのに疲れた場所であり、機能しません。

それで、誰もそれを修正する方法を知っていますか?

ありがとうございます。

4

2 に答える 2

1

あなたはいくつかのことをひねっていると思いますが、おそらくこれがあなたが達成しようとしていることです:

検索フィールドの値によって、テキスト領域に入力される内容が決まります。

この場合、いくつか変更する必要があります。まず、ActionListener 内のコードのみが UI イベントで実行されるため、getText()初期化中に UI 要素を呼び出す必要はありません。

次に、この UI にボタンを追加すると、ロジックが大幅に簡素化されます。リスナーを検索ボックスに接続すると、「ユーザーがテキストを入力したことがいつわかるのか?」などの問題が発生し始めます。および「部分的なテキストを処理するにはどうすればよいですか?」ですが、「検索」ボタンを使用すると、このロジックはユーザーの手に委ねられます。

[検索] ボタンをクリックすると、ボタンに関連付けられているリスナーからのアクション イベントがトリガーされ、 にstationListAreaの結果が取り込まれますsimilarNames(<text from search box>)

以下に示すのは、このタスクを実行するための最もクリーンな方法ではありませんが (フィールドと匿名の内部クラスが含まれます)、単純明快で理解しやすいことに注意してください。

Widget (よくわからないのでcnt、以下のサンプル コードでは省略しています)

//Create the search text box
JTextField searchBox = new JTextField();
leftSide.add(searchBox);

// Create a button that will serve as the "Action" for the search box
JButton searchButton = new JButton("Search");
lefSide.add(searchButton);

// Create the station-list text area that will display text
//  based on results from the search box
JTextArea stationListArea = new JTextArea(12, 0);
leftSide.add(stationListArea);

// Create a listener that listens to the Button, then performs an
//   action from the search box to the text area
ActionListener searchButtonListener = new SearchListener(searchBox, stationListArea);
searchButton.addActionListener(searchButtonListener);

検索リスナー

注: ここでのロジックはstationListArea、ボタンがクリックされるたびにテキストを追加し続けHHHH.SimilarNames()、値を返します。stationListArea毎回新しいテキストで更新し、古いテキストを置き換えるにはstationListArea.setText("")、 の先頭に a を追加しactionPerformed()ます。

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);
        }
    }
}
于 2013-02-15T03:37:48.490 に答える
0

メインコメントのフィードバックに基づいて更新

オブジェクトには、それ自体の表現NAMESを提供する何らかの方法が必要です。Stringこのようにして、その値をテキスト領域に追加できます..

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

}
于 2013-02-20T23:06:39.167 に答える