移动JScrollPane中的可见区域到特定位置 [英] Moving the visible area of a JScrollPane to a specific position

查看:226
本文介绍了移动JScrollPane中的可见区域到特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模仿使用Java调试光标的移动。我有问题得到JScrollPane中的可视区域,以正确的位置。

下面是一个图片我想才达到:

我要滚动只有当我想跳就行了是不可见的。如果其能帮助计算使用做了 codeDrowingPanel.NUMBER_OF_LINES codeDrowingPanel.FONT_SIZE 行被漆成使用这些常量一个面板上。

如果我跳,我也跳线应该是在底部。

我心目中裸露的可见区域取决于屏幕的分辨率。该应用程序最大化,没有调整的机会。

编辑:

 公共无效setCursorToLine(INT线,JScrollPane的codeAREA)
{
    如果(行* codeDrowingPanel.FONT_SIZE> this.getHeight()+ 43)
        this.cursorPosition = this.getHeight()+ 43;
    其他
        this.cursorPosition =行* codeDrowingPanel.FONT_SIZE;
    JViewport的视口=(JViewport的)SwingUtilities.getAncestorOfClass(JViewport.class,codeAREA);
    如果(视口!= NULL)
    {
        矩形视图= viewPort.getViewRect();
        view.y + =行 - previousLine;        codeArea.scrollRectToVisible(视图);
    }
    this.repaint();
}

这是我现在想修改就行了。但是,这是行不通的。我试图按照从第一评论你的第二个例子。我不知道如何使用的方法从第二个评论。


解决方案

我不认为行号应该是文本的一部分。例如你有一个水平滚动条。如果您滚动到右侧,你将失去的行号。

相反,你应该使用一个行标题显示的行号。

请参阅文本组件行号。它含有的类,它为你的行号的风俗画。你可以使用这个组件添加到行标题。

在该类绘画code将突出当前的行号。如果你想添加一个箭头,那么你将需要修改绘画code。在的paintComponent(...)方法,你可以添加以下内容:

  g.drawString(行号,X,Y);// code画一个箭头如果(isCurrentLine(rowStartOffset))
{
    INT高度= fontMetrics.getAscent() - fontMetrics.getDescent();    多边形三角=新的Polygon();
    triangle.addPoint(borderGap,Y);
    triangle.addPoint(borderGap,Y - 高度);
    triangle.addPoint(borderGap + 10,Y - 高度/ 2);
    Graphics2D的G2D =(Graphics2D的)g.create();
    g2d.fill(三角形);
    g2d.dispose();
}

还有一个变化做出。因为我们现在画箭头,我们将需要增加部件的宽度。所以在集preferredWidth(...)的方法,你需要做以下变化:

  // INT preferredWidth = insets.left + insets.right +宽度;
INT preferredWidth = insets.left + insets.right +宽+ 15;


  

我只想滚动,如果我想跳就行了是不可见的。


下面是一些code要做到这一点:

 公共静态无效gotoStartOfLine(JTextComponent的组件,INT线)
{
    元根= component.getDocument()getDefaultRootElement()。
    行= Math.max(线,1);
    行= Math.min(线,root.getElementCount());
    INT startOfLineOffset = root.getElement(线 - 1).getStartOffset();
    component.setCaretPosition(startOfLineOffset);
}

我从文本工具上面code 可能有其他感兴趣的方法(如果不是现在,未来)。

您也可以使用线画家如果你想突出显示文本窗格中的整条生产线。

I am trying to simulate the movement of a debugging cursor using java. I am having problem to get the viewable area of the JScrollPane to the right position.

Here is a picture I want to achive:

I want to scroll only if the line I want to jump it is not visible. The calculation if it helps can by done using CodeDrowingPanel.NUMBER_OF_LINES and CodeDrowingPanel.FONT_SIZE the lines are painted on a panel using these constants.

If I have to jump, the line I have to jump should be at the bottom.

I have to bare in mind that the visible area depends of the screen resolution. The application is maximized with no chance of resizing.

EDIT:

public void setCursorToLine(int line, JScrollPane codeArea)
{
    if(line*CodeDrowingPanel.FONT_SIZE > this.getHeight()+43)
        this.cursorPosition = this.getHeight()+43;
    else
        this.cursorPosition = line * CodeDrowingPanel.FONT_SIZE;
    JViewport viewPort = (JViewport) SwingUtilities.getAncestorOfClass(JViewport.class, codeArea);
    if (viewPort != null) 
    {
        Rectangle view = viewPort.getViewRect();
        view.y += line - previousLine;

        codeArea.scrollRectToVisible(view);
    }
    this.repaint();
}

This is how I am trying now to modify the line. But it does not work. I tried to follow your second example from the first comment. I don't know how to use the method from the second comment.

解决方案

I don't think the line numbers should be part of the text. For example you have a horizontal scrollbar. If you scroll to the right you will lose the line numbers.

Instead you should use a row header to display the line numbers.

See Text Component Line Number. It contains a class that does custom painting of the line number for you. You can use add this component to the row header.

The painting code in that class will highlight the current line number. If you want to add an arrow then you will need to modify the painting code. In the paintComponent(...) method you can add the following:

g.drawString(lineNumber, x, y);  

//  Code to paint an arrow

if (isCurrentLine(rowStartOffset))
{
    int height = fontMetrics.getAscent() - fontMetrics.getDescent();

    Polygon triangle = new Polygon();
    triangle.addPoint(borderGap, y);
    triangle.addPoint(borderGap, y - height);
    triangle.addPoint(borderGap + 10, y - height / 2);
    Graphics2D g2d = (Graphics2D)g.create();
    g2d.fill( triangle );
    g2d.dispose();
}

One more change to make. Since we are now painting an arrow we will need to increase the width of the components. So in the setPreferredWidth(...) method you will need to make the following change:

//int preferredWidth = insets.left + insets.right + width;
int preferredWidth = insets.left + insets.right + width + 15;

I want to scroll only if the line I want to jump it is not visible.

Here is some code to do this:

public static void gotoStartOfLine(JTextComponent component, int line)
{
    Element root = component.getDocument().getDefaultRootElement();
    line = Math.max(line, 1);
    line = Math.min(line, root.getElementCount());
    int startOfLineOffset = root.getElement( line - 1 ).getStartOffset();
    component.setCaretPosition( startOfLineOffset );
}

I took the above code from Text Utilities which may have other methods of interest (if not now, in the future).

You can also use the Line Painter if you want to highlight the entire line in the text pane.

这篇关于移动JScrollPane中的可见区域到特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆