JButton jb=new JButton("click");
jb.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    JOptionPane.showMessageDialog(null,"ok!");
  }
});
然后如果需要调用这个事件,则如下调用即可
jb.doClick();

解决方案 »

  1.   

    sorry,
     忘了说了,具体说吧。
     目前的项目涉及声音编程。所以我用一实现了runnable的applet来进行录音(也是单独的线程)。
     applet上全部时awt组件。
     我需要在录音线程运行1段时间后停止。在applet的run()中,调用applet上的“stop”按键。
      

  2.   

    基本上就是这样
    int stopTime=0;
    public void run() {
    try {
    while(true) {
    ……………………//你的操作
    Thread.sleep(100);
    if((stopTime++)==1000) {
      break;
      return;
    }
    }
    }catch(Exception e){System.out.println(e.toString());return;}
    }
      

  3.   

    applet代码段:  public void paint(Graphics g) {
        //如果点击了录音按键,才运行线程
        if (brecord) {
          //只显示60秒录音
          if (ishowtime < 60) {
            if (ishowtime >= 54) {
              tfsoundlength.setForeground(Color.RED);
            }
            tfsoundlength.setText(String.valueOf(++ishowtime));
          }      //如果到达了60秒,自动停止录音
          if ( ishowtime == 60 )
          {
              //希望在此处调用stop的actionPerformed();      }
        }
        else {
          tfsoundlength.setText(String.valueOf(ishowtime));
        }
      }  /**
       * 运行线程
       */
      public void run() {
        Thread me = Thread.currentThread();
        while (timer == me) {
          try {        Thread.currentThread().sleep(1000);
          }
          catch (InterruptedException e) {
          }
          repaint();
        }
      }停止播放按键部分代码
      void btnstop_actionPerformed(ActionEvent e) {
        btnrecord.setEnabled(true);
        btnplay.setEnabled(true);
        btnsend.setEnabled(true);
        btnreply.setEnabled(true);
        brecord = false;
        dorecord.actionPerformed(ButtonType.BTN_STOP, e, sessionid);
      }
      

  4.   

    自己new一个ActionEvent就完了呗
      

  5.   

    我需要的是该事件点火后的ActionEvent
      

  6.   

    void methodA()
    {
      stopButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent event)
        {
          processActionEvent(event);
        }
      });
    }
    void processActionEvent(ActionEvent event)
    {
       //here you do something to process the action event
       //you get from the above listener
       //****************************
    }
    ?? is that processActionEvent() method what you want? You can customize the 
    method in any way you like. And the parameter of ActionEvent event is what you are looking for, right?
    Come to me if you don't understand.
      

  7.   

    or if you want to fire that button clicked action in the run() method body...
    then modify the code given by beyond_xiruo:
    基本上就是这样
    boolean isActive = true;
    int stopTimes = 0;
    public void run() 
    {
    try {
    while(isActive) {
    ……………………//你的操作
    Thread.sleep(100);
    if((stopTimes++)==10) 
    {
      stopButton.doClick();
    }
    }
    }catch(Exception e){System.out.println(e.toString());return;}
    }
      

  8.   

    用swing 可以用doclick();
     其实如果不传参数,可以调用actionperformed( null );
    只是我想构造ActionEvent!