【问题标题】:joystick event in javajava中的操纵杆事件
【发布时间】:2015-07-07 21:35:05
【问题描述】:

我有一个包含一个文本字段的 Java 应用程序,并且我正在使用操纵杆。​​p>

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

/*
 * 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                   
}

【问题讨论】:

  • 您在这里并没有真正提出明确的问题。当用户按下操纵杆上的特定按钮时,您想在文本字段中添加文本吗?您真正所做的只是发布了一堆代码,而不是真正提出问题。它只会让帮助变得非常困难,而且对于将来可能遇到类似问题的其他用户来说也不太有用。
  • 是的,当用户按下操纵杆上的特定按钮时,我想在文本字段中添加文本

标签: java joystick jinput


【解决方案1】:

基于此处的教程项目:https://theuzo007.wordpress.com/2013/10/26/joystick-in-java-with-jinput-v2/ 看起来您必须循环并检查内容,所以

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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多