0

我有一个类 LabelTextField ,它按照它的指示创建一个标签,后跟一个文本字段。我希望这个类像 JTextField 一样,即在添加动作侦听器时响应 actionPerformed。我正在使用 LabelTextField 添加一个动作侦听器,因此此类中的 JTextField 将需要接收回调并转发 LabelTextField 对象名称而不是 JTextField 对象名称。

我展示了一个简单的 JTextField 及其工作原理,LabelTextField 也在那里以及我希望如何使用它。

我提供了我想要做的代码片段。

我在使用这种类型的回调时遇到问题,详细信息会很有用。

//

class C_one extends JPanel
{

       private JTextField      mjtfOne;
       private LabelTextField  m_nameFind;

       public C_one ()
       {
          setLayout(new FlowLayout());

          mjtfOne = new JTextField(20);
          mjtfOne.addActionListener(this);

          m_nameFind = new LabelTextField("Name", 20);
          m_nameFind.addActionListener(this);

          add(mjtfOne);
          add(m_nameFind);

       }

       // This is called when return is pressed in either object.
       public void actionPerformed(ActionEvent ae) 
       {
          // This works.
          if (ae.getSource() == mjbnOne) {
             System.out.println("Text field one!");

          // ****** This doesn't work. *****
          } else if (ae.getSource() == m_nameFind) {
             System.out.println("Label Text Field name");
          }
       }

}

// // 这将创建一个标签和文本字段作为小部件。类 LabelTextField 扩展 JPanel {

   private  JTextField     mjtf;
   private ActionListener  mal;


   public LabelTextField(String label, int textfieldWidth)
   {
      setLayout(new FlowLayout());

      JLabel lbl = new JLabel(label);
      add(lbl);

      mjtf = new JTextField(textfieldWidth);    
      add(mjtf);    
   }

   // The caller of this class will call this as he would for
   // JTextField.
   public void addActionListener(ActionListener al) 
   {
      mjtf.addActionListener(this);
      mal = al;     
   }

   // This will receive the return from JTextField and needs
   // to forward this on to calling class with caller object
   // not mjtf. 
   //
   // Problem: I can not figure out how to save caller object 
   // name so when this is called I can forward it on just like 
   // what JTextField would have done, ie caller can use 
   // ae.getSource() == m_nameFind.
   public void actionPerformed(ActionEvent ae) 
   {
      mal.actionPerformed(ae); // This does call the caller.
   }

}

4

2 回答 2

1

由于无法更改 ActionEvent 的来源,因此需要创建一个新的 ActionEvent:

public void actionPerformed(ActionEvent e)
{
    e = new ActionEvent(
        this,
        ActionEvent.ACTION_PERFORMED,
        e.getActionCommand(),
        e.getWhen(),
        e.getModifiers());

    mal.actionPerformed(e);
}
于 2013-06-29T01:38:11.480 回答
1

的值ae.getSource()将是mjtf,因为那是生成原始的组件ActionEvent。如果你想让事件的来源是m_nameFind,即你的自定义LabelTextField对象,你需要手动设置它:

  public void actionPerformed(ActionEvent ae) 
  {
    ae.source=this; // this makes it look like the LabelTextField generated the event
    mal.actionPerformed(ae); 
  }

这是你想做的吗?

--

编辑

谢谢@Madprogrammer ..也许你应该重新创建ActionEvent这样的

  public void actionPerformed(ActionEvent ae) 
  {
      mal.actionPerformed(new ActionEvent(this, ae.getID(),
         ae.getCommand(), ae.getWhen(), ae.getModifiers() )); 
  }
于 2013-06-29T01:29:47.147 回答