-1

我有一个包含一个文本字段的 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                   
}
4

1 回答 1

0

基于此处的教程项目: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");
}
于 2015-07-08T00:05:47.517 回答