java中的鼠标输入问题

时间:2015-05-18 16:12:04

标签: java swing timer mouse

修改 我有一个应用程序,它使用swing Timer来控制动作侦听器接口何时触发。鼠标逻辑可以工作,但偶尔也不会检测到点击。以下是我的评论代码。

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实现它。我相信这有助于清理我在大项目上的代码

感谢您的评论,我希望这可以解决大部分问题。

1 个答案:

答案 0 :(得分:1)

鼠标"点击"可能基本上是双击或三击。您可以使用evt.clickCount来获得该功能。它将成为一个事件。

如果您希望获得所有"请按",请改用mousePressed()