简体   繁体   English

需要在java中帮助定位

[英]Need help positioning in java

I created this java program and I wanted an output of which if int x and int y are above 100, it would draw a rectangle. 我创建了这个java程序,我想要一个输出,如果int x和int y大于100,它会绘制一个矩形。 But it doesn't. 但事实并非如此。 How can I make it work?Is there another line of code I need to add? 我怎样才能使它工作?我需要添加另一行代码吗? Here's my code: 这是我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


@SuppressWarnings("serial")
public class GameSetup extends JPanel implements MouseMotionListener{

public static JFrame njf = new JFrame("Test");
public static int x, y;

public static void main(String[] args){

    GameSetup gs = new GameSetup();
    njf.add(gs);

}

public void paintComponent(Graphics g){
    super.paintComponent(g);
    this.setBackground(Color.BLACK);
    g.setColor(Color.GREEN);
    g.fillRect(150, 75, 200, 100);
    g.setColor(Color.ORANGE);
    g.drawString("Play", 239, 123);
    njf.addMouseListener(new MouseAdapter() {
        public void mouseMoved(MouseEvent e) {
            x = e.getX();
            y = e.getY();

        }
    });
    if(x > 100 && y > 100){
        g.drawRect(10, 10, 100, 100);
    }
}

public GameSetup(){
    njf.setSize(500,500);
    njf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    njf.setResizable(false);
    njf.setLocationRelativeTo(null);
    njf.setVisible(true);
}

@Override
public void mouseDragged(MouseEvent arg0) {

}

@Override
public void mouseMoved(MouseEvent e) {

}
  }

Okay, there are several things wrong with the code that you included above. 好的,上面包含的代码有几个问题。

The first that stood out to me was the way that you are adding the mouse action listener to frame. 第一个让我感到高兴的是你将鼠标动作监听器添加到框架的方式。 There are multiple things that are wrong with this. 这有很多问题。

First of all, you are doing this in the paintComponent method, which, if it works, is still considered to be bad practice because the paintComponent method may be called multiple times. 首先,你在paintComponent方法中这样做,如果它起作用,它仍被认为是不好的做法,因为paintComponent方法可能被多次调用。 Do that, as pointed out by the comments, in the constructor of the panel. 正如评论所指出的那样,在专家组的构造函数中这样做。

The second is that you are adding the mouse listener to the frame, not the panel, which doesn't work because the panel is "above" the frame, so the mouse event will only be recognized within the panel. 第二个是你将鼠标监听器添加到框架而不是面板,因为面板位于框架的“上方”,所以它不起作用,因此只能在面板中识别鼠标事件。 Your best bet here is to add a MouseMotionListener directly to the panel itself. 这里最好的选择是将MouseMotionListener直接添加到面板本身。

The third is that you are implementing the MouseMotionListener interface in the GameSetup class, but never actually doing anything with this implementation. 第三个是你在GameSetup类中实现MouseMotionListener接口,但实际上从未对这个实现做任何事情。 So what I did was that I got rid of the inner class, and just had the panel be its own MouseMotionListnener 所以我做的是我摆脱了内部类,并且让面板成为它自己的MouseMotionListnener

The second thing that is wrong with the code is that the paintComponent method is only called at certain points in time (see this ). 代码的第二个问题是paintComponent方法仅在某些时间点被调用(参见此内容 )。 That means that even though the mouse might have moved within the zone, your paintComponent method isn't being called to update the screen accordingly. 这意味着即使鼠标可能已在区域内移动,也不会调用paintComponent方法来相应地更新屏幕。 For this, you need to call the repaint method for your panel. 为此,您需要为面板调用repaint方法。

The third is that you didn't set a size for your panel, and the default one is 0x0, so you need to set a size of your panel (which should be the same as the frame itself) (if you want to keep the default layout). 第三个是您没有为面板设置大小,默认值是0x0,因此您需要设置面板​​的大小(应该与框架本身相同)(如果您想保留默认布局)。

All of that being said, here is your code that I fixed up. 所有这些都说,这是我修复的代码。 I added a variable called enteredZone to keep track of if the mouse had previously entered the zone, so that the rectangle would stay up even if the mouse left the zone after entering it (its your choice if you want to keep it). 我添加了一个名为enteredZone的变量来跟踪鼠标是否先前进入了区域,这样即使鼠标在输入后离开了区域,矩形也会保持不变(如果你想保留它,你可以选择它)。 Note that there are other things with this code that might be considered bad practice, but this is enough to get you started: 请注意,此代码还有其他一些可能被认为是不好的做法,但这足以让您入门:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


@SuppressWarnings("serial")
public class GameSetup extends JPanel implements MouseMotionListener {

    public static JFrame njf = new JFrame("Test");
    public static int x = 0, y = 0;
    public static boolean enteredZone = false;

    public static void main(String[] args) {
        GameSetup gs = new GameSetup();
        gs.addMouseMotionListener(gs);
        njf.add(gs);
        njf.setVisible(true);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        this.setBackground(Color.BLACK);

        g.setColor(Color.GREEN);
        g.fillRect(150, 75, 200, 100);
        g.setColor(Color.ORANGE);
        g.drawString("Play", 239, 123);


        if (x > 100 && y > 100 || enteredZone){
            g.drawRect(10, 10, 100, 100);
            enteredZone = true;
        }
    }

    public GameSetup() {
        super();
        setSize(500, 500);
        njf.setSize(500,500);
        njf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        njf.setResizable(false);
        njf.setLocationRelativeTo(null);
    }

    @Override
    public void mouseDragged(MouseEvent arg0) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        x = e.getX();
        y = e.getY();

        if (x > 100 && y > 100) repaint();
    }

}

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

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