gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:03:26 25 4
gpt4 key购买 nike

我正在制作一个纸牌程序作为业余项目,但我制作的绘画窗口遇到了问题。

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

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调用的,我也尝试从不同的MouseListener调用它MouseMotionListener 方法无济于事。

注意:如果调用成功,paintComponent 方法会通知我,当我单击时,paintComponent 方法不会执行。

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

注意: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 组件实现监听器接口(interface)并不是一个好主意。如果您希望面板中的不同组件有两个鼠标监听器怎么办?面板上不能同时有两个监听器。

更好的方法是让监听器接口(interface)由匿名类实现,遵循关注点分离指南。

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

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

关于java - 从鼠标监听器方法调用时 repaint() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384170/

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