java - java中鼠标输入问题

标签 java swing timer mouse

编辑: 我有一个应用程序,它使用 swing 计时器来控制 Action 监听器界面何时触发。鼠标逻辑可以工作,但有时无法检测到单击。下面是我的注释代码。

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.clickCount 获得该值。它将合并为一个事件。

如果您想获得每次“按下”,请使用 mousePressed()

关于java - java中鼠标输入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30308113/

相关文章:

c# - 在 C# 中使用超时更改按钮的文本

java - 将 xml 文件转换为 xls 文件

java - 如何修复这个 lang.AssertionError?

java - 如何使用 JPA 查询使 PostgreSQL 不区分大小写

java - 有没有办法重写 MenuItemActionPerformed 中的 PaintComponent 方法?

java - JToolBar 不显示

java - 如何临时禁用 JOptionPane 输入对话框上的“确定”按钮?

wpf - 由于 “direct delegate roots”,System.Timers.Timer泄漏

javascript - 如何为html中的多个元素设置相同的id

java - 使用 Java Web Start 输入/输出