繁体   English   中英

从鼠标侦听器方法调用时repaint()不起作用

[英]repaint() not working when called from mouse listener method

我正在制作纸牌程序作为辅助项目,而我制作的绘画窗口存在问题。

在程序中,我有一条直线从一点开始,到鼠标单击的位置结束。 当我单击窗口时,它会成功读取我的点击并将xcorycor变量更改为我的鼠标单击位置,但是无法使用新坐标重新绘制线条。

public class Game_Play extends JFrame {

public int xcor = 0;
public int ycor = 0;
public void setup() { //sets up JFrame
    JFrame frame = new JFrame();
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(0, 0);
    frame.setTitle("Circles");
    frame.add(new MouseHandler());
    frame.addMouseListener(new MouseHandler());
    frame.addMouseMotionListener(new MouseHandler());
    frame.setVisible(true);
}

          //listener and painting subclass
class MouseHandler extends JPanel implements MouseListener, MouseMotionListener {
              //when mouse pressed, the Xcor and Ycor
              //will be changed to the current mouse 
              //x and y cords, then it will call 
              //repaint() to repaint the line using the
              //new Xcor and Ycor locations
    public void mousePressed(MouseEvent me) {
        System.out.println("mouse pressed");
        xcor = me.getX();
        ycor = me.getY();
                  //prints out new cords
        System.out.println(xcor + " xcor");
        System.out.println(ycor + " ycor");
        repaint();
    }

    public void mouseReleased(MouseEvent me) { //tests to make sure listener is working
        System.out.println("mouse released");
    }

    public void mouseClicked(MouseEvent me) {}
    public void mouseEntered(MouseEvent me) {}
    public void mouseMoved(MouseEvent me) {}
    public void mouseExited(MouseEvent me) {}
    public void mouseDragged(MouseEvent me) {}

              //paints the line with the Xcor and Ycor values
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        System.out.println("repaint check"); //test to see if repaint has been called
        g.drawLine(100, 100, xcor, ycor);
    }
}
}

注意: repaint()是从MouseListener方法mousePressed调用的,我也尝试mousePressed不同的MouseListenerMouseMotionListener方法调用它,无济于事。

注意: paintComponent方法会通知我是否已成功调用它,并且当我单击时, paintComponent方法不会执行。

注意:我确实注意到,如果我在屏幕上单击以设置新的线,然后单击窗口上的最大化按钮,它将成功地使用新的线使用重绘的行调用repaint方法。

注意: setup()方法是从另一个文件中的另一个类调用的,代码如下:

public static void main(String[] args) throws IOException {
  deck_Create();
  deck_Shuffle();
  game_setup();
  BufferedImage_array_Setup();
  //being called here
  Game_Play a = new Game_Play();
  a.setup();
  //
}

最后说明:我一直在高低搜寻此问题的解决方法,只是提出了对我没有帮助的类似问题。 给出的任何反馈将不胜感激。

如有任何疑问,请告诉我,我将在少数几个时间内为您解决。

谢谢!

对您的代码的一些评论:

public void setup() {
    JFrame frame = new JFrame();
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(0, 0);
    frame.setTitle("Circles");
    frame.add(new MouseHandler());// your panel
    frame.addMouseListener(new MouseHandler()); // your listener, also a panel, but not the one you added to your frame
    frame.addMouseMotionListener(new MouseHandler()); // yet another listener, also not the panel you added to your frame
    frame.setVisible(true);
}

您可能打算写:

public void setup() {
    JFrame frame = new JFrame();
    frame.setSize(500, 500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocation(0, 0);
    frame.setTitle("Circles");
    JPanel p = new MouseHandler();
    frame.add(p);
    frame.addMouseListener(p);
    frame.addMouseMotionListener(p);
    frame.setVisible(true);
}

请注意,让UI组件实现侦听器接口不是一个好主意。 如果要在面板中有两个用于组件不同的鼠标侦听器怎么办? 面板上不能同时有两个侦听器。

更好的方法是遵循关注点分离准则,由匿名类实现侦听器接口。

另一件事是将侦听器添加到应处理它们的组件中。 您应该在面板上注册这些侦听器,而不是在包含面板的框架上注册。

最后,您应该使用setContentPane将面板设置为内容窗格。 通常,最好通过覆盖setPreferredSize来让面板决定其大小。 在这种情况下,您无需设置包含框架的大小,而是调用pack将框架的大小调整为其子组件的首选大小。

暂无
暂无

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

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