【问题标题】:Making a method trigger another method in a Java GUI在 Java GUI 中使一个方法触发另一个方法
【发布时间】:2014-01-16 00:25:38
【问题描述】:

考虑以下代码。

import edu.cmu.ri.createlab.terk.robot.finch.Finch;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RobotControl extends JFrame { 
    public static void main (String args[])  {

    RobotControl GUI = new RobotControl(); //GUI is the name of my object.
    GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GUI.setSize(300,300);
    GUI.setVisible(true);
    GUI.setTitle("RobotControl");
}


    private JButton foward;



    public RobotControl() { //constructor
    setLayout (new FlowLayout());

    foward = new JButton("foward");
    add(foward);

    ActionListener e = new event();
    foward.addActionListener(e);
    }
             public class event implements ActionListener {
                       public void actionPerformed1(ActionEvent e){


    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

    }
}

}

现在考虑以下代码,它可以让我的雀形机器人向前移动 10 秒。

            Finch myf = new Finch();
            myf.setWheelVelocities(255, 255, 10000);

现在,我的问题是,是否可以通过单击在 GUI 上创建的转发按钮从第一段代码执行第二段代码?如果是这样,我会怎么做。我尝试将 finch 代码放入 actionListener 类,但没有任何反应。我哪里错了。我需要建议。

【问题讨论】:

  • 1- 不适用于ActionListener 中的myf.sleep(5000);System.exit(0);。 2- 什么是Finch
  • Finch 是我用来学习 java 的一个小型机器人。只是谷歌芬奇机器人。
  • 您似乎对ActionListeners 感到困惑。你看过Oracle's tutorial吗?

标签: java swing events event-handling


【解决方案1】:

简短的回答是,是的。

首先,你需要维护一个Finch的实例作为实例变量...

public class RobotControl extends JFrame { 
    private Finch finch;
    //...
}

你需要创建一个Finch的实例...

public RobotControl {
    finch = new Finch();
}

然后在你的ActionListener,你需要和Finch“交流”

public void actionPerformed1(ActionEvent e){
    myf.setWheelVelocities(255, 255, 10000);
}

长答案,很可能你将不得不按顺序发出多个命令,问题是这个过程可能会阻塞事件调度线程,阻止响应新的传入事件并使其看起来就像您的应用程序“停止”了一样

虽然有多种方法可以缓解此问题,但如果您不需要 Finch 与 UI 通信(例如报告电机状态或其他内容),您可以简单地使用单线程 Executor某种类型,并通过它简单地发出一系列命令。

如果您需要向客户端 UI 提供反馈,事情会变得相当复杂...

【讨论】:

    【解决方案2】:

    这是一个示例代码:

    -------------------------
    RobotControl.java
    myrobot = new Finch();
    
    foward = new JButton("foward");
        add(foward);
        forward.addActionListener(new ForwardButtonListener(myrobot));
    
    
    -------------------------
    ForwardButtonListener.java
    public class ForwardButtonListener implements ActionListener {
    
        Finch robotToControl;
    
        public FowardButtonListener(Finch aRobot) {
            robotToControl = aRobot;
        }
    
        public void actionPerformed (ActionEvent e) {
            robotToControl.setWheelVelocities(255, 255, 10000);
        }
    
    }
    -------------------------
    

    【讨论】:

    • 我花了大约 10 分钟才弄清楚您的示例代码,因为您所做的更改很少,但它运行良好。你帮了大忙。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 2012-03-08
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多