1

ボタンを動的に生成し、ボタンの名前を TextArea に入れる ActionListener を追加しようとしています。私のコードは次のようなものです:

    int numnotas = model.getColumnCount();
    JButton[] notasbot = new JButton[numnotas];

    BotonesNotas.setLayout(new GridLayout());
    for( int k = 1; k < numnotas; k++){
        variables.add(model.getColumnName(k));
        notasbot[k-1]=new JButton(variables.get(k-1));
        notasbot[k-1].setSize(new Dimension(80,23));
        notasbot[k-1].setLocation(80, 350);
        notasbot[k-1].setMargin(new Insets(2,14,2,14));
        notasbot[k-1].setAlignmentX(0);
        notasbot[k-1].setAlignmentY(1);
        notasbot[k-1].setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        notasbot[k-1].setVerticalAlignment(javax.swing.SwingConstants.CENTER);
        notasbot[k-1].addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                entrada.insert(????,entrada.getSelectionEnd());
            }
        });
        notasbot[k-1].setLayout(null);
        BotonesNotas.add(notasbot[k-1]);
        notasbot[k-1].setVisible(true);
    }

何を設定できますか???? ボタンの名前を取得します。「notasbot[k-1].getName()」のようなものを入れると、エラーが発生します。ローカル変数 k は内部クラス内からアクセスされます。final を宣言する必要があります

私は何をすべきか ?ありがとう

4

1 に答える 1

3

ActionEvents getSource メソッドを使用できます。ここでは、次のようにします。

JButton eventSourc = (JButton)e.getSource();

actionPerformed メソッドの内部。

于 2013-03-05T22:09:10.977 に答える