2012-09-04 77 views
0

我不明白爲什麼我寫這個簡單的程序,當鼠標離開跟蹤區域(白色JPanel)時試圖更新鼠標的座標時,得到一個IndexOutOfBounds異常。我認爲38號線的支票會照顧它。有什麼建議麼?謝謝!IndexOutOfBounds跟蹤鼠標與鞦韆gui

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(); 
     } 

    } 

} 
+1

爲什麼不使用MouseMotionListener及其mouseMoved();方法來捕捉位置? –

回答

1

這裏是你應該做的:

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); 
    } 
} 
+0

謝謝,使JFrame實現MouseMotionListener是一個很酷的方法。 – user1631937

+0

不客氣。 –

1

您檢查,看看是否coorPanel.getMousePosition()不爲空,但隨後引用getMouseLocation()(本);嘗試改變這種只說getMousePosition的檢查,並添加打印:

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