gpt4 book ai didi

Java JButton addActionListener 并获取自己的名字

转载 作者:行者123 更新时间:2023-11-29 05:47:41 28 4
gpt4 key购买 nike

我正在尝试动态生成按钮并添加一个将按钮名称放入 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

我该怎么办?谢谢

最佳答案

您可以使用 ActionEvents 的 getSource 方法。在这里你会做:

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

在您的 actionPerformed 方法中。

关于Java JButton addActionListener 并获取自己的名字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15235086/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com