JScrollPane 的使用

发布于 2024-10-19 13:08:59 字数 191 浏览 1 评论 0原文

我里面有一个 JScrollPane 和 JPanel(我想在 JPanel 上绘图)。我还有一种用线长度参数绘制线的方法。如果行的长度大于 JScrollPane 上 JPanel 的大小(高度或宽度),我希望 JScrollPane 滚动。 我该怎么做?

PS我尝试过 jScrollPane.scrollRectToVisible 但它不起作用

I have a JScrollPane and JPanel inside it(I want to draw on JPanel). Also I have a method that draws lines with parametre of length of the line. I want my JScrollPane to scroll if the length of the line is more then the size(height or width) of my JPanel that is on JScrollPane.
How can I do this?

P.S. I've tried jScrollPane.scrollRectToVisible but it doesn't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一身骄傲 2024-10-26 13:08:59

这是一个实现您想要的示例,

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class DrawLine {

    JFrame j;
    JPanel p;
    JScrollPane sp;

    public DrawLine() {
        j = new JFrame();
        p = new JPanel() {
            public void paintComponent(Graphics g) {
                g.drawLine(20, 20, 250, 250);
            }
        };
        p.setPreferredSize(new Dimension(300, 300));
        sp = new JScrollPane(p);
        j.getContentPane().add(sp);
        j.setSize(300, 300);
        j.setVisible(true);
    }

    public static void main(String[] args) {
            new DrawLine();
        }
}

请注意 p.setPreferredSize(new Dimension(300, 300)); 行,该行设置面板的首选大小。

Here is an example implementing what you want

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class DrawLine {

    JFrame j;
    JPanel p;
    JScrollPane sp;

    public DrawLine() {
        j = new JFrame();
        p = new JPanel() {
            public void paintComponent(Graphics g) {
                g.drawLine(20, 20, 250, 250);
            }
        };
        p.setPreferredSize(new Dimension(300, 300));
        sp = new JScrollPane(p);
        j.getContentPane().add(sp);
        j.setSize(300, 300);
        j.setVisible(true);
    }

    public static void main(String[] args) {
            new DrawLine();
        }
}

Note the line p.setPreferredSize(new Dimension(300, 300)); which sets the preferred size of the panel.

り繁华旳梦境 2024-10-26 13:08:59

您需要在 LinePanel 类中定义以下方法:

  public Dimension getPreferredSize() {
    return new Dimension(myLine.getWidth(), myLine.getHeight());
  }

确保 myLine 对象可用于上述方法。并且,您将根据需要获得滚动条。

PS:我假设 LinePanel 扩展 JPanel 并且是绘制线条的面板。

You need to define the following method in the LinePanel class:

  public Dimension getPreferredSize() {
    return new Dimension(myLine.getWidth(), myLine.getHeight());
  }

Make sure that myLine object is available to the above method. And, you will get the scrollbars as needed.

P.S.: I assume LinePanel extends JPanel and is the panel on which the line is drawn.

夏末 2024-10-26 13:08:59

您需要将 JScrollPanel 与它正在滚动的 JPanel 关联起来。也就是说,您需要

JScrollPane lineScrollPane = new JScrollPane(linePanel);

这将创建一个 JScrollPane,它会自动调整自身大小以滚动到任何 linePanel 的大小上。

然后,重要的是要确保包含您的生产线的面板实际上足够大,可以容纳整条生产线。也就是说,您可能有类似

linePanel.setSize(myLine.getWidth(), myLine.getHeight());

或任何关联的线路尺寸。请注意,类似的内容

lineScrollPanel.setSize(new Dimension(600, 400));

会调整滚动面板的大小,但不会调整内部的大小 - 也就是说,它将是一个 600x400 滚动面板,但内部面板可能会更大更小,并且它将滚动。

You need to associate the JScrollPanel with the JPanel that it is scrolling. That is, you need

JScrollPane lineScrollPane = new JScrollPane(linePanel);

This will create a JScrollPane that automatically sizes itself to scroll over whatever linePanel's size is.

It's important to make sure then, that the panel containing your line actually IS big enough to fit entire line. That is, you might have something like

linePanel.setSize(myLine.getWidth(), myLine.getHeight());

or whatever your associated line size is. Note that something like

lineScrollPanel.setSize(new Dimension(600, 400));

will resize the scroll panel but not the thing inside--that is, it will be a 600x400 scroll pane but the inside panel may be bigger smaller, and it will be scrolled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文