ActionMap / KeyBindings - <identifier>预期错误

时间:2017-09-09 04:22:41

标签: java

我正在尝试使用Java在我的GUI上移动ImageIcon PNG图像。但是我得到了这个错误:

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

这是我的代码:

public class MainClass extends JPanel {

    public static void main(String[] args) {

        JLabel label;
        JPanel panel = new JPanel();

        ImageIcon image = new ImageIcon("Character Face Left - Bronze.png");

        int x = 300;
        int y = 300;

        label = new JLabel(image);
        label.setLayout(null);

        JFrame frame = new JFrame("Rover: Bound to Earth");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(label);
        frame.setSize(500,500);
        frame.setVisible(true);

        InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
        ActionMap actionMap = getActionMap();

        inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), LEFT);
        actionMap.put(LEFT, new AbstractAction() {
            x--;
        } );

        label.setLocation(x,y);
    }
}

我想要发生的是,当按下“A”键时,ImageIcon会向左移动。我试图用x和y设置ImageIcon的位置,并在按下“A”键时发生x--但是,我再次收到错误。

1 个答案:

答案 0 :(得分:0)

稍微修改了你的代码:

// 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();
        } );
    }
}

此代码将运行,但仍无效,因为Key Press->Acion转换在某处被破坏。我不太了解Action在这里真正的帮助。
尝试在一个新问题中询问它......