我想从我的JButton扩展中检索值

时间:2014-10-25 00:43:45

标签: java swing reference jbutton actionlistener

我已经读过ActionEvents返回事件对象的引用。我想知道如何从该引用中提取一条信息。我确定答案已经存在,我只是不确定如何提出问题来解决问题。

我创建了一个扩展JButton的新类。我希望我创建的每个按钮都存储一个数学运算的整数值。

import javax.swing.JButton;
public class NewButton extends JButton {
    int value;
    public NewButton(String writing, int value) {
        this.setText(writing);
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

我想使用ActionListener来检查点击,然后我想在控制台中显示int value。这是我的GUI:

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyGui {
    public static void main(String[] args) {

        JFrame frame = new JFrame("GUI");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        frame.add(panel);

        NewButton button = new NewButton("ten", 10);
        panel.add(button);

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == button) {
                    System.out.println(button.getValue());
                }
            }
        };

        button.addActionListener(listener);

        frame.setVisible(true);
        frame.setSize(100,100);
    }
}

所以这适用于一个按钮。

现在我们解决了我的问题(最后)。我想制作另一个NewButtonbutton2。我以为我会改变我的ActionListener部分。我想我可以为两个按钮都包含一个语句,因为它们属于同一类。

        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(e.getSource() == button1 || e.getSource() == button2) {
                    System.out.println(e.getSource().getValue());
                }
            }
        };

但这不起作用。

我的理由是,因为我可以检查:

e.getSource() == button

然后引用e.getSource()应该可以访问我的getValue()

有谁知道从value获取e.getSource()的方法?我的目标是看看我是否可以避免为每个按钮制作单独的if语句或动作侦听器。

2 个答案:

答案 0 :(得分:4)

您可以将JButton强制转换为NewButton,然后调用您的方法。

public void actionPerformed(ActionEvent e) {
    NewButton newBtn = (NewButton) e.getSource();
    int value = newBtn.getValue();   
}

说完这个,我自己,我不想扩展JButton而是扩展AbstractAction。例如:

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.event.ActionEvent;

public class MyGui {
   public static void main(String[] args) {
      String[] texts = { "One", "Two", "Three", "Four", "Five" };
      JFrame frame = new JFrame("GUI");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel panel = new JPanel();

      for (int i = 0; i < texts.length; i++) {
         int value = i + 1;
         panel.add(new JButton(new NewAction(value, texts[i])));
      }

      frame.add(panel);

      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }
}

class NewAction extends AbstractAction {
   private int value;

   public NewAction(int value, String name) {
      super(name);
      this.value = value;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      System.out.println("Value: " + value);
   }
}

答案 1 :(得分:3)

除了@HovercraftFullOfEels之外,如果您的用例与设置/检索任意值一样简单,那么您可以使用JButton来同时使用JComponent来“保持”此值。{{3}从JButton button = new JButton("Button 1"); button.putClientProperty("ValueKey", "Actual value"); ActionListener listener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); System.out.println(button.getClientProperty("ValueKey")); }; } 类继承的{}和putClientProperty(key, value)方法。像这样:

AbstractAction

通过这种方式,您根本不必延长JButton和{{1}}。请注意,此方法不仅适用于按钮,也适用于其他组件。有一个完整的示例使用与getClientProperty(key)

中的复选框完全相同的方法