我有点不确定如何提出这个问题,因为我的代码几乎是正确的,但无论如何我正在实现通常的WASD键,以便在我拥有的机器人上移动。现在我只是简单地将按键绑定到按钮上。因此我的代码应该在我点击它时显示键并在释放时显示不等效,但它显示它的方式似乎更像是程序轮询键盘而不是在键被击中时中断它。因此,当我按下一个按键并按住它时,它会一次又一次快速地按下按钮,但是当我按下一个W A S D键并按住它时,我希望它只是按一下按钮,然后在我释放按键时释放按钮。该版本工作正常,但是当我按住它时,例如,如果我按住W键,它会显示“w w w w w w w w w w”然后“w!”当它被释放并且我希望它显示“w”时,那么“w!”何时释放。我是否需要使用不同的方法来绑定密钥,还是有更好的方法来实现按钮?我的代码相对较短。这是我的完整代码:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import javax.swing.JToggleButton;
public class RobotWASDKeys
{
private static JFrame mainFrame;
private static JToggleButton wButton;
private static JToggleButton aButton;
private static JToggleButton sButton;
private static JToggleButton dButton;
private static JPanel mainPanel;
private static Action wAction;
private static Action wNotAction;
private static Action aAction;
private static Action aNotAction;
private static Action sAction;
private static Action sNotAction;
private static Action dAction;
private static Action dNotAction;
private static ButtonListener buttonListener;
public static void main( String[] args )
{
mainFrame = new JFrame( "Buttons" );
mainFrame.add( makePanel() );
mainFrame.setLocationRelativeTo( null );
mainFrame.setSize( 300, 200 );
mainFrame.setResizable( false );
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
mainFrame.setVisible( true );
mainFrame.getContentPane().validate();
mainFrame.getContentPane().repaint();
}
//this is my panel
static JPanel makePanel()
{
mainPanel = new JPanel();
buttonListener = new ButtonListener();
wButton = new JToggleButton("w");
aButton= new JToggleButton("a");
sButton= new JToggleButton("s");
dButton= new JToggleButton("d");
wButton.addActionListener(buttonListener);
aButton.addActionListener(buttonListener);
sButton.addActionListener(buttonListener);
dButton.addActionListener(buttonListener);
wAction = new WAction();
wNotAction = new WNotAction();
aAction = new AAction();
aNotAction=new ANotAction();
sAction = new SAction();
sNotAction=new SNotAction();
dAction = new DAction();
dNotAction=new DNotAction();
//binds w a s d keys to an action
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0,true),"forwardNot");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W,0,false),"forward");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A,0,false),"left");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A,0,true),"leftNot");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S,0,false),"backward");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_S,0,true),"backwardNot");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0,false),"right");
mainPanel.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_D,0,true),"rightNot");
mainPanel.getActionMap().put("forward", wAction);
mainPanel.getActionMap().put("forwardNot", wNotAction);
mainPanel.getActionMap().put("left", aAction);
mainPanel.getActionMap().put("leftNot",aNotAction);
mainPanel.getActionMap().put("backward", sAction);
mainPanel.getActionMap().put("backwardNot", sNotAction);
mainPanel.getActionMap().put("right", dAction);
mainPanel.getActionMap().put("rightNot",dNotAction);
mainPanel.add(wButton);
mainPanel.add(aButton);
mainPanel.add(sButton);
mainPanel.add(dButton);
return mainPanel;
}
//prints a string to the command prompt when a key is hit.
static class WAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "w" );
wButton.doClick();
}
}
static class WNotAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "w!" );
wButton.doClick();
}
}
static class AAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "a" );
aButton.doClick();
}
}
static class ANotAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "a!" );
aButton.doClick();
}
}
static class SAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "s" );
sButton.doClick();
}
}
static class SNotAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "s!" );
sButton.doClick();
}
}
static class DAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "d" );
dButton.doClick();
}
}
static class DNotAction extends AbstractAction
{
public void actionPerformed( ActionEvent tf )
{
System.out.println( "d!" );
dButton.doClick();
}
}
static class ButtonListener implements ActionListener
{
public void actionPerformed( ActionEvent bp )
{
mainPanel.requestFocusInWindow();
}
}
}