JTextField setActionCommand()的用途是什么[如何以编程方式访问它以查找事件发生在哪个单元格中?] [英] What's the purpose of JTextField setActionCommand() [how do you access it programmatically to find which cell an event occurred in?]

查看:97
本文介绍了JTextField setActionCommand()的用途是什么[如何以编程方式访问它以查找事件发生在哪个单元格中?]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

===编辑原始帖子后的月份===

===EDIT MONTHS AFTER ORIGINAL POST===

该问题是由于需要了解在单击按钮时11x11网格(JTextField数组)的哪个单元处于活动状态.我当时问了一个错误的问题,选择了错误的工具(setActionCommand),尽管当时它通过字符串操作起作用. (我最终选择完全重写那是一个令人费解的折磨代码示例.)

比使用setActionCommand()更好的解决方案是下面的答案,它使用.setName().

================================================ =

================================================

我为JTextField cell执行此操作:

cell.setActionCommand("55");

我将cell推入名为staq的堆栈,然后使用以下方法将pop推入堆栈:

I push cell onto a stack named staq and later pop it off in a method with the following:

JTextField f = staq.pop();      \\ this works fine
System.out.println(f.command);  \\ this gives error mentioned below

错误:"命令在JTextField中具有私有访问权限"

Error: "command has private access in JTextField"

f的Netbeans监视窗口中,我可以监视 f.command,其中有"55".但是对于JTextField,没有getCommand,没有getActionCommand,没有什么,它返回的String可能包含"55".

In Netbeans watch window for f, I am able to watch f.command and there's the "55". But there's no getCommand, no getActionCommand, nothing for JTextField that returns a String that could contain "55".

所以我问:

(a)setActionCommand对于JTextField

和/或

(b)您如何获取其内容?

(b) how do you get its contents?

(上周我可以通过evt.getComponent().toString()的文本操作获得命令",但是JTextField没有getComponent(),也没有其他东西可以保证.)

(Last week I was able to get at "command" via text manipulation of evt.getComponent().toString() but there's no getComponent() for JTextField nor does anything else appear to have promise.)

(我又回到了愚蠢和沮丧的状态.也许我的设计就是愚蠢的.)

(I'm back to feeling stupid and frustrated. Maybe my design's just what's stupid.)

(也许我不能为setActionCommand添加标签,因为我的声誉不是1500(仅1475就被它遗漏了)是我的线索,就是我要付出更多的努力,而不是从错误中咀嚼驶过桥下水坝的那辆马车前的那匹马的一部分.)

(And maybe the fact that I can't add a tag for setActionCommand since my reputation isn't 1500 [missed it by a mere 1475] is my clue that I'm biting off more than I can chew from the wrong part of the horse before the cart that's headed over the dam under the bridge.)

推荐答案

考虑在侦听器中添加到JTextField时的情况.我可以将相同的侦听器添加到另一个JTextField(将动作命令设置为77).这样,在ActionListener中,我可以识别出哪个触发了事件.

Consider situation when you add to your JTextField following listener. I can add same listener to another JTextField (which has action command set to 77). This way in ActionListener I can recognize which one triggered the event.

myTextField.addActionListener(new TestActionListener());
mySecondTextField.addActionListener(new TestActionListener());

public class TestActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent event) {
        if(event.getActionCommand().equals("55")){
            //Source of you action is your JTextField
            System.out.println(((JTextField)event.getSource()).getText());
        }
        if(event.getActionCommand().equals("77")){
            //There might be different component having this set to 77 using same ActionListener
        }           

    }

}

这篇关于JTextField setActionCommand()的用途是什么[如何以编程方式访问它以查找事件发生在哪个单元格中?]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆