Java 理解jViewport时遇到一些问题
我一直试图理解JViewports,并尝试使用它。我一直在使用这位先生的,它只是扩展了JViewport类Java 理解jViewport时遇到一些问题,java,swing,viewport,jviewport,Java,Swing,Viewport,Jviewport,我一直试图理解JViewports,并尝试使用它。我一直在使用这位先生的,它只是扩展了JViewport类 如果我想在实现JPAND类的6480×4860对象中间设置视口,为什么这个代码会给我一个空白视图,即使在调试时,我已经确认实现了我使用的JPAND类已经被添加到视图中了吗?我是否完全卷入了对不同方法的调用中,或者我对所用坐标及其含义的理解中? 还有一点: 打电话吗 v.setOpaque(true); 仅将视口设置为不透明,否则将同时设置其所有子对象? 如果我知道如何正确使用视口
如果我想在实现JPAND类的6480×4860对象中间设置视口,为什么这个代码会给我一个空白视图,即使在调试时,我已经确认实现了我使用的JPAND类已经被添加到视图中了吗?我是否完全卷入了对不同方法的调用中,或者我对所用坐标及其含义的理解中? 还有一点: 打电话吗
v.setOpaque(true);
仅将视口设置为不透明,否则将同时设置其所有子对象?
如果我知道如何正确使用视口,我想我很快就会知道这个问题的答案
public myProgram(){
...
myCustomJPanel = new MyCustonJPanel();
myCustomJPanel.setBackground(Color.WHITE);
GrabbableViewport v = new GrabbableViewport();
v.setViewSize(new Dimension(720,540));
v.setViewPosition(new Point(720,540));
v.setView(myCustomJPanel);
v.setViewPosition(new Point(720,540));
v.setLocation(43, 5);
appropriateParentPanel.add(v);
}
这是我正在尝试使用的类。这个简短的问题需要更多的空间,但是格式化的文本应该比纯文本更让你高兴!如果我没有弄错的话,问题不应该来自这里
// Copyright (c) 2006 - 2008, Markus Strauch.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
package net.sf.sdedit.ui.components;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JViewport;
import javax.swing.event.MouseInputListener;
/**
* A <tt>GrabbableViewport</tt> is a <tt>JViewport</tt> that scrolls its
* view when the mouse is dragged. While the mouse is being dragged, its cursor
* is set to a "grabbing hand", like in applications such as Acrobat
* Reader.
*
* @author Markus Strauch
*
*/
public class GrabbableViewport extends JViewport implements MouseInputListener {
private static Cursor HAND = new Cursor(Cursor.MOVE_CURSOR);
private static Cursor DFLT = new Cursor(Cursor.DEFAULT_CURSOR);
public static void setHandCursorIcon(ImageIcon icon) {
Image grabbingHand = icon.getImage();
HAND = Toolkit.getDefaultToolkit().createCustomCursor(grabbingHand,
new Point(0, 0), "hand cursor");
}
/**
* Constructor.
*/
public GrabbableViewport () {
super ();
}
private Rectangle rect;
private Point point;
private JComponent view;
public void setView(Component view) {
super.setView(view);
if (this.view != view) {
if (this.view != null) {
this.view.removeMouseListener(this);
this.view.removeMouseMotionListener(this);
}
if (view != null) {
view.addMouseListener(this);
view.addMouseMotionListener(this);
}
this.view = (JComponent) view;
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
view.setCursor(HAND);
//((Component) e.getSource()).setCursor(HAND);
rect = getViewRect();
point = screenLocation(e);
}
private Point screenLocation(MouseEvent e) {
Point root = view.getLocationOnScreen();
Point mouse = e.getPoint();
if (rect != null && !rect.contains(mouse)) {
return null;
}
Point screenPoint = new Point(root.x + mouse.x, root.y + mouse.y);
return screenPoint;
}
public void mouseReleased(MouseEvent e) {
view.setCursor(DFLT);
scrollTo(screenLocation(e));
clear();
}
public void mouseDragged(MouseEvent e) {
scrollTo(screenLocation(e));
}
public void mouseMoved(MouseEvent e) {
}
private void scrollTo(Point newPoint) {
if (point != null && newPoint != null && rect != null) {
int deltaX = point.x - newPoint.x;
int deltaY = point.y - newPoint.y;
rect.x = rect.x + deltaX;
rect.y = rect.y + deltaY;
((JComponent) getView()).scrollRectToVisible(rect);
point = newPoint;
}
}
private void clear() {
rect = null;
point = null;
}
}
//版权所有(c)2006-2008,Markus Strauch。
//版权所有。
//
//以源代码和二进制形式重新分发和使用,带或不带
//如果满足以下条件,则允许进行修改:
//
//*源代码的重新分发必须保留上述版权声明,
//此条件列表和以下免责声明。
//*二进制形式的重新分发必须复制上述版权声明,
//此条件列表和文档中的以下免责声明
//和/或分发时提供的其他材料。
//
//本软件由版权所有者和贡献者“按原样”提供
//以及任何明示或暗示的保证,包括但不限于
//对适销性和特定用途适用性的默示保证
//拒绝承认。在任何情况下,版权所有人或贡献者均不得
//对任何直接、间接、附带、特殊、示范或
//间接损害(包括但不限于采购
//替代商品或服务;使用、数据或利润的损失;或业务
//中断)无论是何种原因造成的,且基于任何责任理论,无论是在
//合同、严格责任或侵权行为(包括疏忽或其他)
//因使用本软件而产生的任何后果,即使
//这种损害的可能性。
包net.sf.sdedit.ui.components;
导入java.awt.Component;
导入java.awt.Cursor;
导入java.awt.Image;
导入java.awt.Point;
导入java.awt.Rectangle;
导入java.awt.Toolkit;
导入java.awt.event.MouseEvent;
导入javax.swing.ImageIcon;
导入javax.swing.JComponent;
导入javax.swing.JViewport;
导入javax.swing.event.MouseInputListener;
/**
*GrabbableViewport是滚动其
*拖动鼠标时查看。拖动鼠标时,其光标
*设置为“抓取手”,如在Acrobat等应用程序中
*读者。
*
*@作者Markus Strauch
*
*/
公共类GrabbableViewport扩展了JViewport,实现了MouseInputListener{
私有静态光标手=新光标(光标.移动光标);
私有静态游标DFLT=新游标(游标.DEFAULT\u游标);
公共静态无效setHandCursorIcon(图像图标){
Image grabbingHand=icon.getImage();
HAND=Toolkit.getDefaultToolkit().createCustomCursor(grabbingHand,
新点(0,0),“手动光标”);
}
/**
*构造器。
*/
公共抓取bablevewport(){
超级();
}
私有矩形矩形;
专用点;
私有组件视图;
公共无效集合视图(组件视图){
super.setView(视图);
if(this.view!=视图){
如果(this.view!=null){
this.view.removeMouseListener(this);
this.view.removeMouseMotionListener(this);
}
如果(视图!=null){
view.addMouseListener(此);
view.addMouseMotionListener(此);
}
this.view=(JComponent)视图;
}
}
公共无效mouseClicked(MouseEvent e){
}
公共无效鼠标事件(鼠标事件e){
}
公共无效mouseExited(MouseEvent e){
}
公共无效鼠标按下(MouseEvent e){
view.setCursor(手动);
//((组件)e.getSource()).setCursor(手动);
rect=getViewRect();
点=屏幕位置(e);
}
专用点屏幕位置(MouseEvent e){
Point root=view.getLocationOnScreen();
点鼠标=e.getPoint();
if(rect!=null&!rect.contains(鼠标)){
返回null;
}
点屏幕点=新点(root.x+mouse.x,root.y+mouse.y);
返回点;
}
公共无效MouseEvent(MouseEvent e){
view.setCursor(DFLT);
滚动至(屏幕位置(e));
清除();
}
公共无效鼠标标记(鼠标事件e){
滚动至(屏幕位置(e));
}
public void mouseMoved(MouseEvent e){
}
私有无效滚动到(点newPoint){
if(point!=null&&newPoint!=null&&rect!=null){
int deltaX=point.x-newPoint.x;
int deltaY=point.y-newPoint.y;
rect.x=rect.x+deltaX;
矩形y=矩形y+deltaY;
((JComponent)getView()).scrollRectToVisible(rect);
点=新点;
}
}
私人空位清除(){
rect=null;
点=空;
}
}
我可以给您一个sscce,但我认为这应该足够简单,尽管如果您要求,我可以快速拼凑一些示例。因为
GrabbableViewport扩展了JViewport
,我猜您应该在JScrollPane
中使用它,如图所示。如果没有,该示例可能会帮助您使用ScrollPanePaint.MyPanel
构造一个
myCustomJPanel = new MyCustonJPanel();
GrabbableViewport v = new GrabbableViewport();
v.setView(myCustomJPanel);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewport(v);
enclosingContainer.add(scrollPane);
另请参见本节中有关利用e