如何获取并打印包含JButton的变量的名称? [英] How to get and print the name of a variable which holds a JButton?

查看:86
本文介绍了如何获取并打印包含JButton的变量的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以获取并打印JButton的名称?

I was wondering if it is possible to get and print a JButton's name?

给出以下代码:

JButton button1 = new JButton();

我如何找回button1?

我尝试过:

button.getName()

但返回

null

推荐答案

我的猜测-您确实希望JButton显示文本,而不是它的name属性.如果是这样,请签出,

My guess - you really want the text displayed by the JButton, not its name property. If so, check out,

button.getText();
// or
button.getActionCommand();

如果您要查找变量名,那么不能通过方法调用获得它,也不应该这样,因为对象可以被许多变量引用,或者可以是集合或数组的一部分,等等.对象的变量名几乎没有意义.

If you're looking for the variable name, well that is not obtainable by method call, nor should it be since an object can be referred to by many variables, or may be part of a collection or array, and so a variable name for an object is close to a meaningless concept.

道德:通过向我们介绍您的背景故事来澄清您的问题,只是您想做的是什么,而不是您想怎么做.

Moral: clarify your question by giving us your background story, just what it is you're trying to do, not so much how you're trying to do it.

修改
您发表评论,

Edit
You state in comment,

获取按钮的名称,例如,如果我有一个名为JButton的按钮button1 = new JButton();我要打印此按钮的名称,即button1

get the name of the button, for example, if I have a button called JButton button1 = new JButton(); I want to print the name of this button which is button1

您正在将变量名与对象名混淆.上面的button1是JButton 变量 的名称,但不是它所引用的对象,因为对象没有名称.

You're confusing variable names with object names. button1 above is the name of the JButton variable but not the object it refers to since objects don't have names.

通常不可能通过在对象上调用方法来获取对象的变量名称",因为许多对象甚至都没有名称,例如,如果它们是集合的一部分,或者可以引用单个对象取决于许多变量,因此您希望使用哪个名称".

It's not generally possible to get an object's variable "name" by calling a method on object, because many objects don't even have names, for instance if they're part of a collection, or a single object may be referred to by many variables, and so which "name" would be the one you desire.

例如,假设您有以下代码:

For instance say you have this code:

JButton button1 = new JButton("Foo");
JButton button2 = button1;

在这里创建的JButton对象的名称"是什么? button1和button2都指的是精确的相同 JButton对象,,因此这里没有对象的名称.

What is the "name" of the JButton object created here? Both button1 and button2 refer to the exact same JButton object, and so there's no such thing as a name for an object here.

同样,您不是在告诉我们您要做什么,而是要告诉我们您如何做.您的问题是经典的 XY问题,只有当您告诉我们时才能解决整体问题.

Again, you're not telling us what you're trying to do, but rather how you're trying to do it. Your question is a classic XY Problem and only will be solved when you tell us the overall problem.

如果您试图找出按下了哪个JButton,则ActionListener的actionPerformed ActionEvent参数具有getSource()方法,该方法将返回对按钮 object 的引用>已按下,您可以使用它.如何使用它取决于您需要告诉我们的问题的详细信息.

If you're trying to find out which JButton has been pressed, the ActionListener's actionPerformed ActionEvent parameter has a getSource() method which will return a reference to the button object that was pressed, and you can use that. How you use that will depend on the details of your problem which you need to tell us.

例如:

// inside of an ActionListener
public void actionPerformed(ActionEvent evt) {
  // this will get you a reference to the button object that was pressed:
  AbstractButton sourceBtn = (AbstractButton) evt.getSource();

  // now you can use the sourceBtn object, or compare its reference to your 
  // JButton variables or collections in your GUI
  // for example if your GUI has buttons in a List<JButton> you could
  for (JButton button : buttonList) {
    if (sourceBtn == button) {
      // you've found your button of interest!
    }
  }
}

这篇关于如何获取并打印包含JButton的变量的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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