java中的操纵杆事件

java中的操纵杆事件,java,joystick,jinput,Java,Joystick,Jinput,我有一个包含一个文本字段的java应用程序,我正在使用一个操纵杆 按下操纵杆上的按钮时,如何在文本字段中执行操作 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ packa

我有一个包含一个文本字段的java应用程序,我正在使用一个操纵杆

按下操纵杆上的按钮时,如何在文本字段中执行操作

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



package op;

import net.java.games.input.Controller;

/**
 *
 * @author Ahmed
 */
public class Test extends javax.swing.JFrame {

    /**
     * Creates new form Test
     */
    public Test() {
        initComponents();
        con();
    }

    private void con(){

        JInputJoystick joystick = new JInputJoystick(Controller.Type.STICK);
        if( !joystick.isControllerConnected() ){   
            txt.setText("Not Connected");
        }
        else    
           txt.setText(joystick.getControllerType()+" "+joystick.getControllerName()+" Controller Cound!");
        if( !joystick.pollController() ) {

            txt.setText("Controller disconnected!");
        }

       // Number of buttons.

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        txt = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(70, 70, 70)
                .addComponent(txt, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                .addGap(88, 88, 88))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(txt, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(145, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */


    // Variables declaration - do not modify                     
    private javax.swing.JTextField txt;
    // End of variables declaration                   
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
包op;
导入net.java.games.input.Controller;
/**
*
*@作者艾哈迈德
*/
公共类测试扩展了javax.swing.JFrame{
/**
*创建新的表单测试
*/
公开考试(){
初始化组件();
con();
}
私人监狱{
Jinput操纵手柄=新的Jinput操纵手柄(控制器类型斗杆);
如果(!mogage.isControllerConnected()){
txt.setText(“未连接”);
}
其他的
txt.setText(操纵杆.getControllerType()+“”+操纵杆.getControllerName();
如果(!mogage.pollController()){
setText(“控制器断开连接!”);
}
//按钮的数量。
}
/**
*从构造函数中调用此方法来初始化表单。
*警告:不要修改此代码。此方法的内容始终为
*由表单编辑器重新生成。
*/
@抑制警告(“未选中”)
//                           
私有组件(){
txt=newjavax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout=newjavax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(布局);
layout.setHorizontalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(70,70,70)
.addComponent(txt,javax.swing.GroupLayout.DEFAULT\u SIZE,242,Short.MAX\u值)
.addGap(88,88,88))
);
layout.setVerticalGroup(
createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(122、122、122)
.addComponent(txt,javax.swing.GroupLayout.PREFERRED\u SIZE,33,javax.swing.GroupLayout.PREFERRED\u SIZE)
.addContainerGap(145,简称最大值))
);
包装();
}//                         
/**
*@param指定命令行参数
*/
//变量声明-不修改
私有javax.swing.JTextField txt;
//变量结束声明
}

基于这里的教程项目:看起来你必须循环检查,所以

private void con(){
    JInputJoystick joystick = new JInputJoystick(Controller.Type.STICK);
    while (joystick.isControllerConnected()) {
        // Go trough all components of the controller.
        Component[] components = joystick.getComponents();
        for(int i=0; i < components.length; i++) {
            Component component = components[i];
            Identifier componentIdentifier = component.getIdentifier();

            // Buttons
            if(componentIdentifier.getName().matches("^[0-9]*$")){ // If the component identifier name contains only numbers, then this is a button.
                // Is button pressed?
                if(component.getPollData() != 0.0f) {
                    txt.setText("Button got pressed!")
                }
            }
        }

        // We have to give processor some rest.
        try {
            Thread.sleep(25);
        } catch (InterruptedException ex) {
            Logger.getLogger(JoystickTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    txt.setText("Controller not connected");
}
private void con(){
Jinput操纵手柄=新的Jinput操纵手柄(控制器类型斗杆);
while(操纵杆.isControllerConnected()){
//检查控制器的所有部件。
Component[]components=操纵杆.getComponents();
对于(int i=0;i
您在这里提出的问题并不明确。当用户按下操纵杆上的特定按钮时,是否要在文本字段中添加文本?你真正做的只是发布一堆代码,而不是问一个问题。这只会使帮助变得非常困难,也会使它对将来可能遇到类似问题的其他用户不太有用。是的,当用户按下操纵杆上的特定按钮时,我想在文本字段中添加文本