簡體   English   中英

actionlistener回調到另一個類

[英]actionlistener callback to another class

我有一個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");
          }
       }

}

// //這會創建標簽和文本字段作為窗口小部件。 class 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.
   }

}

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() )); 
  }

由於無法更改ActionEvent的源,因此需要創建一個新的ActionEvent:

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

    mal.actionPerformed(e);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM