java中的鼠标输入问题
编辑: 我有一个应用程序,它使用swing计时器来控制动作侦听器接口何时启动。鼠标逻辑工作,但偶尔不会检测到单击。下面是我的注释代码java中的鼠标输入问题,java,swing,timer,mouse,Java,Swing,Timer,Mouse,编辑: 我有一个应用程序,它使用swing计时器来控制动作侦听器接口何时启动。鼠标逻辑工作,但偶尔不会检测到单击。下面是我的注释代码 public class Board extends JPanel implements MouseListener, MouseMotionListener, ActionListener { private MainMenu mainMenu = new MainMenu(); private static String State; /*Ma
public class Board extends JPanel implements MouseListener, MouseMotionListener, ActionListener
{
private MainMenu mainMenu = new MainMenu();
private static String State; /*Makes the control flow simpler, just checking
strings that describe the state. All the states are contained in GameState class.*/
public Board()
{
this.addMouseListener(this);
this.addMouseMotionListener(this);
setVisible(true);
mainMenu.initLogIn(); /*This just loads the button images*/
Timer timer = new Timer(12, this); /*(millisecond delay, tells this class
to update any actionlistener (mouselistener etc)*/
timer.start();
}
public void paint(Graphics G)
{
super.paint(G);
Graphics G2d = (Graphics2D)G;
/*Main menu paint logic*/
// This paints buttons from mainMenu class on screen
G.drawImage(mainMenu.getTopic1().getspriteImage(),
mainMenu.getTopic1().getxCoord(),
mainMenu.getTopic1().getyCoord(),this);
G.drawImage(mainMenu.getTopic2().getspriteImage(),
mainMenu.getTopic2().getxCoord(),
mainMenu.getTopic2().getyCoord(), this);
G.drawImage(mainMenu.getTopic3().getspriteImage(),
mainMenu.getTopic3().getxCoord(),
mainMenu.getTopic3().getyCoord(),this);
/*Shows mouse input worked by changing the background color*/
if (State == GameState.MAINMENU_TOPIC1)
{
setBackground(Color.BLACK);
}
if (State == GameState.MAINMENU_TOPIC2)
{
setBackground(Color.BLUE);
}
if (State == GameState.MAINMENU_TOPIC3)
{
setBackground(Color.GRAY);
}
repaint(); //tells paint to repaint, which allows gui to update
}
@Override
public void mouseClicked(MouseEvent e)
{
Point point = e.getPoint();
/*This contains the logic to change State based on mouse clicks*/
if(mainMenu.getTopic1().getRectangle().contains(point))
{
State = GameState.MAINMENU_TOPIC1;
}
if(mainMenu.getTopic2().getRectangle().contains(point))
{
State = GameState.MAINMENU_TOPIC2;
}
if(mainMenu.getTopic3().getRectangle().contains(point))
{
State = GameState.MAINMENU_TOPIC3;
}
}
所以,我不确定为什么鼠标点击不总是被检测到。我知道分配给更新操作侦听器的时间可能太短。但是,机器没有太多代码可以循环,所以我认为这不是问题所在。有没有想过是什么原因导致了鼠标的这种行为
另外,我以后肯定会使用JButtons实现这一点。我确信这将有助于在更大的项目上清理我的代码
谢谢你的评论,我希望这能澄清大部分问题。鼠标“点击”实际上可能是双击或三次点击。您可以使用evt.单击计数
来获得它。它将成为一个事件
如果你想每次按下鼠标,请使用
mousePressed()
。“这会在我的鼠标逻辑、执行的操作和绘制方法之间循环(如果我理解正确)。”
——这对我来说既奇怪又困惑。如果你没有很快得到答案,或者即使你得到了答案,请更详细地解释你的程序在做什么,以及你的问题是什么。你能定义“跳过点击”吗?你的意思是不是每次点击都没有反应?计时器非常快(每毫秒?),而且发布的代码甚至不能尝试重现,更不用说猜测问题了。建议发布一个你巨大的总机鼠标器看起来像噩梦(很抱歉直言不讳,但我不得不这么说)。为什么不简单地在JButtons上使用单个actionlistener呢?@HovercraftFullOfEels我肯定会详细介绍这个程序正在做什么,可能是通过copeg建议的MVCE。也不需要道歉,我总是欣赏建设性的批评。我对编程相当陌生,所以我会接受所有我能得到的帮助!顺便说一句,我想知道你是否最好用CardLayout交换JPanel,每个JPanel绘制不同的背景并具有不同的行为,而不是像你这样交换图像。如果你显示的是行为迥然不同的视图,比如菜单JPanel和游戏玩法JPanel,我当然建议你走这条路线。你说得对,我收集了鼠标点击,当跳转发生时,它会快速闪烁数字2并恢复为1。当我按下鼠标时,这个问题就解决了。我注意到大多数应用程序都需要实际的“点击”按钮来完成其功能。因此,单击将导致按钮按下,如果您在仍然单击的情况下将鼠标拖离,则按钮将不会响应。这是否意味着,要像我所描述的那样点击工作,我的最佳选择是在pressed和released方法中都有逻辑?