使用动作侦听器获取JButton的文本

使用动作侦听器获取JButton的文本,第1张

使用动作侦听器获取JButton的文本

您可以使用一个不错的简单技巧…

@Overridepublic void actionPerformed(ActionEvent e){    String butSrcTxt = e.getActionCommand();}

如果未

actionCommand
按钮指定,则
text
使用按钮的。

现在,如果您确实指定了

actionCommand
按钮的属性,并且您仍然想知道文本(对我来说似乎很奇怪),则可以使用更多类似…

@Overridepublic void actionPerformed(ActionEvent e){    Object source = e.getSource();    if (source instanceof JButton) {        JButton btn = (JButton)source;        String butSrcTxt = btn.getText();    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5462253.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存