简体   繁体   English

Swing awt 组件方法 getName from setName?

[英]Swing awt component method getName from setName?

Trying to add a getName() method to a setName() based on this method void java.awt.Component.setName(String arg0) but without luck.尝试将getName()方法添加到基于此方法的setName() void java.awt.Component.setName(String arg0)但没有运气。 The button below is loaded into the contentPane.下面的按钮被加载到 contentPane 中。

I need to add an actionlistener to a button instantiated in this method (either based on setName or something else similar):我需要向在此方法中实例化的按钮添加一个动作监听器(基于 setName 或其他类似的东西):

public JButton JButtonComponent(String btnRef) {
    
    button = new JButton("Convert to binary");
    button.setMaximumSize(new Dimension(width/4,unitHeight));
    
    button.addActionListener(this);
    button.setName("Binary"); // my set name 
    
    return button;
}

This is my actionPerformed method:这是我的 actionPerformed 方法:

@Override
public void actionPerformed(ActionEvent e) {

    System.out.println(e.getSource());

}

And the above system print outputs:以上系统打印输出:

javax.swing.JButton[Binary,270,14,90x33,alignmentX=0.0,alignmentY=0.5,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Convert,defaultCapable=true]

My question is how do i get the name value i've defined in JButtonComponent from the ActionEvent?我的问题是如何从 ActionEvent 中获取我在 JButtonComponent 中定义的名称值? - The setName value is clearly in the ActionEvent e but no method seems to be right for the object to extract the value so i can do a compare. - setName 值显然在 ActionEvent e 中,但似乎没有适合 object 提取值的方法,因此我可以进行比较。 And is this the best method to define an "id" like you would in HTML if you were to compare?如果您要进行比较,这是定义“id”的最佳方法吗?

Your JButton definition code should be:您的JButton定义代码应为:

button.setText("Binary");

Your ActionListener code should be:您的ActionListener代码应该是:

JButton button = (JButton) event.getSource();
String text = button.getText();

You can also use the JButton ActionCommand String to pass along additional;您还可以使用JButton ActionCommand String来传递附加信息; information.信息。

either based on setName or something else similar基于 setName 或其他类似的东西

Don't use getName/setName for something like this.不要将 getName/setName 用于这样的事情。

There is already an API that allows you to use an "action command".已经有一个 API 允许您使用“动作命令”。

For a JButton you can use:对于JButton ,您可以使用:

button.setActionCommand("Binary");

Then in the ActionListener you use:然后在您使用的ActionListener中:

String command = e.getActionCommand();

The setName value is clearly in the ActionEvent setName 值明确在 ActionEvent 中

No it isn't.不,不是。 A reference to the source object is contained in the ActionEvent. ActionEvent 中包含对源 object 的引用。 You need to first access the source before you can access the methods of the source object:需要先访问源码,才能访问源码object的方法:

JButton button = (JButton)e.getSource();
System.out.println( button.getName() );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM