Java ActionMap/KeyBindings-<;标识符>;预期误差

Java ActionMap/KeyBindings-<;标识符>;预期误差,java,Java,我正试图在我的GUI上用Java获取一个ImageIcon PNG图像。但我得到了这个错误: MainClass.java:31: error: <identifier> expected x = x--; ^ 1 error 我想要发生的是,当按下“A”键时,图像图标向左移动。我尝试用x和y设置图像图标的位置,并在按下“A”键时使x发生,但再次出现错误

我正试图在我的GUI上用Java获取一个ImageIcon PNG图像。但我得到了这个错误:

MainClass.java:31: error: <identifier> expected
                                x = x--;
                                 ^
1 error

我想要发生的是,当按下“A”键时,图像图标向左移动。我尝试用x和y设置图像图标的位置,并在按下“A”键时使x发生,但再次出现错误。

稍微修改了您的代码:

// originally this extended JPanel, but was never used...
public class MainClass extends AbstractAction
{
    // he label to be moved
    JLabel label;
    // the co-ordinates   
    int x = 300;
    int y = 300;
    // constructor... moved your code from main() to here...
    public MainClass()
    {
        // instantiating panel and label
        final JPanel panel = new JPanel();
        final ImageIcon image = new ImageIcon( "Character Face Left - Bronze.png" );
        // but as i do no have your image, i added some text...
        label = new JLabel( "IMG", image, JLabel.CENTER );
        // and specified he size of the label
        label.setSize( 100, 50 );
        // initial positioning
        label.setLocation( x, y );
        final JFrame frame = new JFrame( "Rover: Bound to Earth" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        // the frame is the one hat lays out its child components
        // so it is the one to set he layout to null, not the label...
        frame.setLayout( null );
        frame.add( label );
        frame.setSize( 500, 500 );
        frame.setVisible( true );
        // setting up he key event, however his still not working
        InputMap inputMap = panel.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW );
        ActionMap actionMap = panel.getActionMap();
        // your LEFT identifier should be an object, that is unique,
        // i just used the string "LEFT"....
        inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_A, 0 ), "LEFT" );
        actionMap.put( "LEFT", this );
    }
    // the action's listener
    @Override
    public void actionPerformed( ActionEvent e )
    {
        // decrement x, and also move the label
        x -= 10;
        label.setLocation( x, y );
    }
    // a new main()
    public static void main( String[] args )
    {
        // AWT uses a special thread to queue, dispatch and execute events
        // the SWING GUI must run on the AWT Event Dispatch Thread
        SwingUtilities.invokeLater( () ->
        {
            new MainClass();
        } );
    }
}
此代码将运行,但仍不工作,因为
按键->Acion
转换在某个地方中断。我对
行动了解不多
s如何在这里提供真正的帮助。

尝试在一个新问题中询问它…

x
main
方法的一个局部变量,因此当
main
方法完成时,我将被销毁…您将布局设置为
null
,但框架没有,这将布局其子对象,但是对于标签本身…不完全确定你要我做什么,我如何使它不是主方法的局部变量?在方法开始之前把它放进去也没用。你所说的“但是帧没有”是什么意思?在为关键事件设置侦听器之后,(顺便说一句,它不起作用),主方法将结束,变量
x
将被销毁…为什么它会被销毁?还有,为什么按键事件不起作用-我认为我正确地遵循了教程…我键盘上的
T
键有时不起作用,所以如果你发现有些事情没有意义,尝试在单词中添加
T
。。。。
// originally this extended JPanel, but was never used...
public class MainClass extends AbstractAction
{
    // he label to be moved
    JLabel label;
    // the co-ordinates   
    int x = 300;
    int y = 300;
    // constructor... moved your code from main() to here...
    public MainClass()
    {
        // instantiating panel and label
        final JPanel panel = new JPanel();
        final ImageIcon image = new ImageIcon( "Character Face Left - Bronze.png" );
        // but as i do no have your image, i added some text...
        label = new JLabel( "IMG", image, JLabel.CENTER );
        // and specified he size of the label
        label.setSize( 100, 50 );
        // initial positioning
        label.setLocation( x, y );
        final JFrame frame = new JFrame( "Rover: Bound to Earth" );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        // the frame is the one hat lays out its child components
        // so it is the one to set he layout to null, not the label...
        frame.setLayout( null );
        frame.add( label );
        frame.setSize( 500, 500 );
        frame.setVisible( true );
        // setting up he key event, however his still not working
        InputMap inputMap = panel.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW );
        ActionMap actionMap = panel.getActionMap();
        // your LEFT identifier should be an object, that is unique,
        // i just used the string "LEFT"....
        inputMap.put( KeyStroke.getKeyStroke( KeyEvent.VK_A, 0 ), "LEFT" );
        actionMap.put( "LEFT", this );
    }
    // the action's listener
    @Override
    public void actionPerformed( ActionEvent e )
    {
        // decrement x, and also move the label
        x -= 10;
        label.setLocation( x, y );
    }
    // a new main()
    public static void main( String[] args )
    {
        // AWT uses a special thread to queue, dispatch and execute events
        // the SWING GUI must run on the AWT Event Dispatch Thread
        SwingUtilities.invokeLater( () ->
        {
            new MainClass();
        } );
    }
}