JAVA:如何通过此命令来监听按钮按下?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA:如何通过此命令来监听按钮按下?相关的知识,希望对你有一定的参考价值。

当程序调用命令时,我无法弄清楚为什么以下命令不起作用。我在Java中没有很好的背景,但据我所知,当我按下调用该命令的操纵杆上的按钮时,该命令应该执行print行语句。我不确定问题是否可能是命令需要某个地方的Action侦听器或按钮侦听器,或者我是否需要以某种方式将该命令与同一个控制台相关联。应该只有一个可识别的控制台可以打印到,我知道原始程序中的其他打印行语句的作品......对吗?

这是library可能会有所帮助

该程序:

/*--------------------------------------------------------------------------
--*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        
*/
/* Open Source Software - may be modified and shared by FRC teams. The code   
*/
/* must be accompanied by the FIRST BSD license file in the root directory 
of */
/* the project.                                                               
*/
/*--------------------------------------------------------------------------
--*/

package org.usfirst.frc.team5621.robot;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.buttons.Button;
import edu.wpi.first.wpilibj.buttons.JoystickButton;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;

/**
 * The VM is configured to automatically run this class, and to call the
 * functions corresponding to each mode, as described in the IterativeRobot
 * documentation. If you change the name of this class or the package after
 * creating this project, you must also update the manifest file in the resource
 * directory.
 */
public class Robot extends IterativeRobot {

    private DifferentialDrive m_robotDrive
            = new DifferentialDrive(new Spark(0), new Spark(1));
 static Joystick m_stick = new Joystick(1);
    private Timer m_timer = new Timer();
    static Subsystem ExampleSubsystem;
    Command ExampleCommand;
    Command CompressCommand;
    Command DecompressCommand;
    Command OpenClawCommand;
    Command CloseClawCommand;
    Command CompressorToggleCommand;

public static class OI {
    //Create Joystick and Buttons
    static Joystick m_stick = new Joystick(1);
    static Button   button1 = new JoystickButton(m_stick, 1);
    static Button   button2 = new JoystickButton(m_stick, 2);
    static Button   button3 = new JoystickButton(m_stick, 3);
    static Button   button4 = new JoystickButton(m_stick, 4);
    static Button   button5 = new JoystickButton(m_stick, 5);
    static Button   button6 = new JoystickButton(m_stick, 6);
    static Button   button7 = new JoystickButton(m_stick, 7);
    static Button   button8 = new JoystickButton(m_stick, 8);

public OI() {
    // Define Commands for Joystick Buttons
    OI.button1.whileHeld(new CompressorToggleCommand());
    OI.button2.whileHeld(new CompressCommand());
    OI.button3.whileHeld(new DecompressCommand());
    OI.button4.whileHeld(new OpenClawCommand());
    OI.button5.whileHeld(new CloseClawCommand());
    OI.button6.whileHeld(new ExampleCommand());
    OI.button7.whileHeld(new ExampleCommand());
    OI.button8.whileHeld(new ExampleCommand());
    }
}

public class Compressor {
    Compressor c = new Compressor();
}
public class Solenoid {
    Solenoid exampleSolenoid = new Solenoid();
}

/**
 * This function is run when the robot is first started up and should be
 * used for any initialization code.
 */
@Override
public void robotInit() {
}

/**
 * This function is run once each time the robot enters autonomous mode.
 */
@Override
public void autonomousInit() {
    m_timer.reset();
    m_timer.start();
}

/**
 * This function is called periodically during autonomous.
 */
@Override
public void autonomousPeriodic() {
    // Drive for 2 seconds
    if (m_timer.get() < 2.0) {
        m_robotDrive.arcadeDrive(0.5, 0.0); // drive forwards half speed
    } else {
        m_robotDrive.stopMotor(); // stop robot
    }
}

/**
 * This function is called once each time the robot enters teleoperated mode.
 */
@Override
public void teleopInit() {
    System.out.println("TeleOperated Mode Enabled");
}

/**
 * This function is called periodically during teleoperated mode.
 */
@Override
public void teleopPeriodic() {
    m_robotDrive.arcadeDrive(m_stick.getY(), m_stick.getX());
}

/**
 * This function is called periodically during test mode.
 */
@Override
public void testPeriodic() {
}
}

命令

    /*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */
/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package org.usfirst.frc.team5621.robot;

import edu.wpi.first.wpilibj.command.Command;
/**
 * An example command.  You can replace me with your own command.
 */
public class CompressCommand extends Command {
    public CompressCommand() {
    }

// Called just before this Command runs the first time
@Override
protected void initialize() {
}

// Called repeatedly when this Command is scheduled to run
@Override
protected void execute() {
    System.out.println("Compressing...");
    exampleSolenoid.set(true);

}

// Make this return true when this Command no longer needs to run execute()
@Override
protected boolean isFinished() {
    return false;
}
}
答案

首先,我想说在Stack Overflow上看到一位FRC程序员很高兴。与您的问题有关,您的问题可能在于您在同一文件中声明多个公共类。这在Java编程中是非常糟糕的做法。您要解决的问题是:

public OI() {
// Define Commands for Joystick Buttons
OI.button1.whileHeld(new CompressorToggleCommand());
OI.button2.whileHeld(new CompressCommand());
OI.button3.whileHeld(new DecompressCommand());
OI.button4.whileHeld(new OpenClawCommand());
OI.button5.whileHeld(new CloseClawCommand());
OI.button6.whileHeld(new ExampleCommand());
OI.button7.whileHeld(new ExampleCommand());
OI.button8.whileHeld(new ExampleCommand());
}

再一次,嵌套公共类是不好的做法,但真正导致问题的是你为OI类的构造函数中的每个按钮设置按钮按钮操作,但你实际上从未实际创建一个OI对象因此,永远不会调用构造函数,并且永远不会运行代码。在您的robotInit函数中,创建一个新的OI对象:

void robotInit() {
    OI myOI = new OI();
}

还要从OI类中的语句中删除OI标记。这不是必需的。这样:

OI.button1.whileHeld(new CompressorToggleCommand());

变成这样:

button1.whileHeld(new CompressorToggleCommand());

我还建议将所有对象初始化移动到robotInit。我建议将所有嵌套类分别放在自己的文件中,并确保在主要的Robot类中创建所有这些类的对象。我还建议阅读一些基本的面向对象编程概念,如how constructors work,以及how to use classes and objects in Java。也可以阅读您在FRC API Reference中使用的FRC功能

以上是关于JAVA:如何通过此命令来监听按钮按下?的主要内容,如果未能解决你的问题,请参考以下文章

如何通过按下 Java Swing 按钮来停止线程?

如何禁用在android片段类中按下的后退按钮

如果在 WebView 片段中按下后退按钮,如何返回上一页?

使用导航控制器按下后退按钮后如何防止前一个片段显示?

如何在按钮单击时添加/删除片段?

使用导航控制器按下后退按钮后,如何防止先前的片段出现?