单击按钮后,如何移动Jcomponent的位置?

发布于 2025-01-25 16:33:42 字数 660 浏览 3 评论 0原文

因此,我想在单击“相应”按钮后更改我作为JCOMPONENT实现的图像的位置,但是,单击时,它不会做任何事情。

我认为问题可能在我的PaintComponent方法中,因为我希望一开始将图像绘制在面板的中心。

面板本身具有一个网格杂志,尽管我认为将其锚定在中心也无济于事,因为我想在单击按钮时移动图像,并且只希望它在我打开时出现在面板的中心中框架...

特别是对于此按钮,我希望我的jcomponent/image向右移动50像素。

无论如何,这就是我所做的:

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

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.

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.

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...

For this button in particular I want my JComponent/image to move 50 pixels to the right.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2025-02-01 16:33:42

当前,您的PaintComponent方法始终使用Xy的相同值,因此图像不会移动不足为奇。

xy状态,需要记住。这是通过定义实例字段来完成的:

private int x;
private int y;

您无法在施工时间初始化它们,因为组件创建时的宽度和高度为零(通常)。幸运的是,有一种方法可以告诉您何时给出一个宽度和高度。该方法是 addnotify ,您可以并且应该覆盖:

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

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

请注意,您应该将this(您的组件实例)传递给任何期望image observer参数而不是null的方法。

现在,PaintComponent不需要进行任何计算。它只是在任何xy的情况下绘制图像恰好是:

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

现在您的坐标是实例字段,您可以直接更改它们:

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

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.

x and y are state that needs to be remembered. 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:

@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.

Now paintComponent doesn’t need to do any calculation. It just paints the image at whatever x and y happen to be:

@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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文