我不知道如何做一個好標題的..但在這裏,我們去:Java的鼠標位置couting的面積已滾動
我試圖得到準確的鼠標的位置,當我點擊的標籤上,用我添加到JScrollPane的JViewPort的MouseAdapter 我的面板本身添加到JScrollPane中。
當我試圖讓鼠標位置的座標相對於面板的面積是可見總是..其不包括不可見的,因爲滾動的區域..
我不知道如果我讓自己清楚,我希望如此.. 已經嘗試從JViewPort和JScrollPane使用getMousePosition,也沒有工作.. 非常感謝!
下面是一些代碼: ,其具有在其內部的面板框架的施工..有關添加JViewport.getViewPosition()到()從getMousePosition獲得的座標
public GraphViewer(ArrayList<TimeSlot> graph) throws HeadlessException {
final MyCustomPanel panel = new MyCustomPanel(graph);
panel.setPreferredSize(panel.getLargestSize());
scroll = new JScrollPane(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
JViewport v = scroll.getViewport();
ViewportDragScrollListener l = new ViewportDragScrollListener(panel);
v.addMouseMotionListener(l);
v.addMouseListener(l);
}
請你只想從鼠標中獲取點,或將此點重定向到JScrollPane中的JPanel – mKorbel
嘿!感謝您的回覆 。我想獲得鼠標在JPanel中的位置..但是我使用getMousePosition的位置始終是視口..而不是整個jlabel ..如果有一種方法可以獲得X滾動量和數量Y滾動我可以做數學.. – TiagoM
有看看SwingUtilities.convertXxxXxx,我敢肯定,我發佈的答案非常similair問題(S),請在這裏搜索我的帖子 – mKorbel