简体   繁体   English

单击按钮后如何移动 Jcomponent 的 position?

[英]How can I move the position of my Jcomponent after I click a button?

So I want to change the position of an image which I implemented as a JComponent after I click the corresponding button, however, it does not do anything when getting clicked.所以我想在单击相应按钮后更改我作为 JComponent 实现的图像的 position,但是,单击相应按钮时它不会执行任何操作。

I think the problem may lay inside my paintComponent method, since I want the image to be drawn in the center of my panel in the beginning.我认为问题可能出在我的 paintComponent 方法中,因为我希望图像一开始就绘制在面板的中央。

The panel itself has a GridBagLayout, although I think that anchoring it in the center would not help either, since I want to move the image whenever the button is clicked and only want it to show up in the center of the panel when I open the frame...面板本身有一个 GridBagLayout,虽然我认为将它锚定在中心也无济于事,因为我想在单击按钮时移动图像并且只希望它在我打开时显示在面板的中心框架...

For this button in particular I want my JComponent/image to move 50 pixels to the right.特别是对于此按钮,我希望我的 JComponent/图像向右移动 50 像素。

Anyway here's what I did:无论如何,这就是我所做的:

@Override 
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    int x = (this.getWidth() - img.getWidth(null))/2;
    int y = (this.getHeight() - img.getHeight(null))/2;
    
    g.drawImage(img, x, y, null);
}

@Override
public void actionPerformed(ActionEvent e) {
    img.repaint(img.getX()+50, img.getY(), img.getHeight(), img.getWidth());
}

Currently, your paintComponent method always uses the same values for x and y , so it should come as no surprise that the image doesn't move.目前,您的 paintComponent 方法始终对xy使用相同的值,因此图像不动也就不足为奇了。

x and y are state that needs to be remembered. xy是需要记住的state This is done by defining instance fields:这是通过定义实例字段来完成的:

private int x;
private int y;

You can't initialize them at construction time, because a component has a width and height of zero when it's created (usually).您不能在构造时初始化它们,因为组件在创建时(通常)的宽度和高度为零。 Fortunately, there is a method which tells you when a component has been given a width and height;幸运的是,有一种方法可以告诉您何时为组件指定了宽度和高度; that method is addNotify , which you can and should override:该方法是addNotify ,您可以而且应该覆盖它:

@Override
public void addNotify() {
    super.addNotify();

    x = (this.getWidth() - img.getWidth(this)) / 2;
    y = (this.getHeight() - img.getHeight(this)) / 2; 
}

Notice that you should pass this (your component instance) to any method which expects an ImageObserver argument, rather than null.请注意,您应该this (您的组件实例)传递给需要 ImageObserver 参数的任何方法,而不是 null。

Now paintComponent doesn't need to do any calculation.现在 paintComponent 不需要做任何计算。 It just paints the image at whatever x and y happen to be:它只是在xy恰好是的任何位置绘制图像:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.drawImage(img, x, y, this);
}

And now that your coordinates are instance fields, you can change them directly:现在您的坐标是实例字段,您可以直接更改它们:

@Override
public void actionPerformed(ActionEvent event) {
    x += 50;
    repaint();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM