gpt4 book ai didi

java - ActionMap/KeyBindings - 预期错误

转载 作者:行者123 更新时间:2023-11-30 06:31:02 24 4
gpt4 key购买 nike

我正在尝试让 ImageIcon PNG 图像在 Java 中的 GUI 上移动。但我收到此错误:

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-- ,但我再次收到错误。

最佳答案

稍微修改一下你的代码:

// 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 不太了解,无法在这里提供真正的帮助。
尝试在新问题中询问它...

关于java - ActionMap/KeyBindings - <identifier> 预期错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46127073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com