【发布时间】: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