2012-01-25 4 views
0

私は、Javaでボタンを使用してキーボードを作成しています。ユーザがAからZのラベルの付いたボタンをクリックすると、JTextFieldのテキストをAまたは押されたボタンに設定します。同様のアクションまたは汎用アクションごとに別々のActionListenerを使用する必要がありますか?

私はボタンごとに別々のクラスを用意していますので、Aのpublic class listenser1 implements ActionListener、Bのpublic class listenser2 implements ActionListenerはこれを行う良い方法ですか?

はまた、私は一つのクラスの下でそれを行う実行しようとしました、使用else文が

 if(a.getText().equals("A")) 
     { 
      input1.setText(input.getText() + "A");  
     } 
     . 
     . 
     . 

を使用して購入し、これが動作しない場合とあれば、それはABCDEFGHIJKLMNOPQRSTUVWXYZだけではなく1文字を出力します。

答えて

4

いいえ、それが最も効率的ではありません方法。それはあまりにも長く書くことができません。あなたはこのように言って、単一のクラスを使用する理由はない

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionListener; 

public class Example extends JFrame implements ActionListener { 
    private final String[] letters = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; 
    private JButton[] buttons = new JButton[26]; 
    private JTextArea text = new JTextArea(); 

    public Example() { 
     super("Example"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     for (int i = 0; i < letters.length; i++) { 
      buttons[i] = new JButton(letters[i]); 
      buttons[i].addActionListener(this); 
      add(buttons[i]); 
     } 
     add(text); 
     pack(); 
     setVisible(true); 
    } 

    public void actionPerformed(ActionEvent event) { 
     text.append(event.getActionCommand()); 
    } 

    public static void main(String[] args) { 
     Example ex = new Example(); 
    } 
} 
+0

+1ドキュメントを追加するか、xxx.setText(xxx.getText + event.getActionCommand()) – mKorbel

+0

ActionListenerを再利用するために+1 – camickr

+0

@mKorbelが編集されました。 – fireshadow52

2

アクションごとに1つのリスナーインスタンスを作成することをお勧めします。匿名の内部クラスは、finalローカルを読むことで簡単にパラメータ化できます。

void addButton(final char letter) { 
    JButton button = new JButton(Character.toString(letter)); 
    button.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent event) { 
      input.setText(input.getText() + letter); 
     } 
    }); 
    panel.add(button); 
} 

(私はDocumentはなくJtextComponent.getText/setTextのためにまっすぐに行くことをお勧めしたい。)

+0

一つだけが必要なときに、なぜ各ボタンの新しい 'ActionListener'を作成しますか? – MisterEd

+0

@MisterEdなぜ地球上ではないでしょうか?あなたは、イベントがどこから来たかを間違った方向に見るためにどんなハッキリもなく、自己完結型の行動を持っています。 –

+0

次に、 'hackery? 'というイベントを参照する場合、' actionPerformed() 'が最初にイベントを渡すのはなぜですか? – MisterEd

1

あなたは1人のパラメータ化リスナーを書くことができます。

private class LetterActionListener implements ActionListener { 

     private final char letter; 

     public LetterActionListener(char letter) { 
      this.letter = letter; 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      input1.setText(input1.getText() + letter); 
     } 

    } 
2

:代わりに、これを試して

ActionListener commonListener = new ActionListener() { 

    public void actionPerformed(ActionEvent e) { 
     Object o = e.getSource(); 
     if (o instanceof JButton) { 
      JButton button = (JButton)o; 
      String command = (String) button.getActionCommand()(); 
      input1.setText(command); 
     } 
    } 
}; 

をそして、あなたはとあなたのボタンにこのリスナーを追加することができます。

buttonA = new JButton(); 
buttonA.addActionListener(commonListener); 
buttonA.setActionCommand("A"); // same for others till Z. 

・ホープ、この何らかの形であなたを助けるかもしれません。

1

ActionListener1つののインスタンスを作成し、ループ内の各ボタンに追加します。

final JTextField input1; 

    ... 

    final ActionListener l = new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      input1.setText(input1.getText() + ((JButton) e.getSource()).getText()); 
     } 
    }; 
    for (char c = 'A'; c <= 'Z'; c++) { 
     final JButton jButton = new JButton(String.valueOf(c)); 
     panel.add(jButton); 
     jButton.addActionListener(l); 
    } 
関連する問題