繁体   English   中英

IndexOutOfBounds追踪滑鼠gui的滑鼠

[英]IndexOutOfBounds tracking mouse with swing gui

我无法弄清楚为什么我写的这个简单程序在鼠标离开跟踪区域(白色的JPanel)时尝试更新鼠标的坐标时会得到IndexOutOfBounds异常。 我认为第38行的支票会解决这个问题。 有什么建议么? 谢谢!

import java.awt.*;

import javax.swing.JFrame;

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L;

    Label coorLabel;
    Panel coorPanel, content;

    public MainFrame(String s){

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container cont = getContentPane();

        coorLabel = new Label("Mouse Coordinates: ");

        coorPanel = new Panel();
        coorPanel.setPreferredSize(new Dimension(400,400));
        coorPanel.setBackground(Color.WHITE);
        /**
        content = new Panel();
        content.add(coorPanel, BorderLayout.PAGE_START);
        content.add(coorLabel, BorderLayout.PAGE_END);
        **/

        cont.add(coorPanel, BorderLayout.PAGE_START);
        cont.add(coorLabel, BorderLayout.PAGE_END);

        pack();
        setVisible(true);

    }

    public void updateCoor(){

        if(coorPanel.getMousePosition()!=null){
            coorLabel.setText("Mouse Coordinates: "+getMousePosition().x+", "+getMousePosition().y);
            coorLabel.repaint();
        }

    }

    public static void main(String[]args){

        MainFrame frame = new MainFrame("Coor App");
        while(true){
            frame.updateCoor();
        }

    }

}

在Swing教程中查看并发 您正在使用while(true)循环完全占用初始线程,并在Event Dispatch Thread之外更新用户界面。

请参阅“事件侦听器简介”以熟悉Swing事件模型和“ 如何编写鼠标动作侦听器” ,尤其是对于鼠标动作侦听器示例。

您检查coorPanel.getMousePosition()是否不为null,然后引用(this。) getMouseLocation() ; 尝试将其更改为仅在支票中说getMousePosition,然后添加打印件:

if( this.getMousePosition() != null ){
    System.out.println(getMousePosition());
    coorLabel.setText("Mouse Coordinates: "+getMousePosition().x+", "+getMousePosition().y);
    coorLabel.repaint();
}

这是您应该做的:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;

public class Stack extends JFrame implements MouseMotionListener{
    int x;
    int y;
    JPanel p = new JPanel();
    JPanel detectPanel = new JPanel();
    JTextField t = new JTextField(10);
    JLabel l = new JLabel("Position's inside of bordered panel: ");
    public Stack(){
        setLayout(new BorderLayout());
        t.setEditable(false);
        p.add(l);
        p.add(t);
        detectPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        add(p,BorderLayout.NORTH);
        add(detectPanel,BorderLayout.CENTER);
        detectPanel.addMouseMotionListener(this);

    }

    public static void main(String[] a){
        SwingUtilities.invokeLater(new Runnable(){
            public void run(){
                Stack s = new Stack();
                s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                s.setLocationByPlatform(true);
                s.setPreferredSize(new Dimension(640,480));
                s.pack();
                s.setVisible(true);
            }
        });
    }

    public void mouseDragged(MouseEvent e) {

    }

    public void mouseMoved(MouseEvent e) {
        x= e.getX();
        y= e.getY();
        t.setText(x+", "+y);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM