gpt4 book ai didi

java - 我无法纠正的鼠标事件未知错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:54:24 25 4
gpt4 key购买 nike

我正在学习java,在解决MouseListener问题时,我在类声明中遇到了错误,尝试了我所知道的一切,请帮助我。据我说,我已经正确完成了所有编码,也将代码粘贴到 IDE 中,但出现了相同的错误。

谢谢

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

public class Sub extends JFrame
{
private JPanel mousepanel;
private JLabel statusbar;

public Sub()
{
super("Mouse Events");
// we didnt have a FlowLayout as we had in previous programs

mousepanel = new JPanel();
mousepanel.setBackground(Color.WHITE);
add(mousepanel, BorderLayout.CENTER); //BorderLayout used instead of FlowLayout and it will place it in the center of the window.

statusbar = new JLabel("Default");
add(statusbar, BorderLayout.SOUTH); // same as above

thehandler handler = new thehandler();
mousepanel.addMouseListener(handler);
mousepanel.addMouseMotionListener(handler);

private class thehandler implements MouseListener, MouseMotionListener
{
public void mouseClicked(MouseEvent event)
{
statusbar.setText(String.format("Clicked at %d, %d", event.getX(), event.getY()));
}
public void mousePressed(MouseEvent event)
{
statusbar.setText("You press down the mouse.");
}
public void mouseReleased(MouseEvent event)
{
statusbar.setText("You released the mouse.");
}
public void mouseEntered(MouseEvent event)
{
statusbar.setText("You enetered the mouse panel area.");
mousepanel.setBackground(Color.PINK);
}
public void mouseExited(MouseEvent event)
{
statusbar.setText("The mouse has left the window.");
mousepanel.setBackground(Color.WHITE);
}

//these aremouse motion events

public void mouseDragged(MouseEvent event)
{
statusbar.setText("Your are dragging the mouse.");
}
public void mouseMoved(MouseEvent event)
{
statusbar.setText("You moded the mouse.");
}
}
}
}

最佳答案

您需要将 thehandler 类定义移出 Sub 构造函数的范围。

旁注:类名称以大写字母开头。

关于java - 我无法纠正的鼠标事件未知错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848730/

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