6

这个JScrollPane基于窗口是JSplitPane.

getBounds(), getWidth(),getHeight()都返回窗口的完整大小,包括不可见(可滚动)部分。

我只想知道可见部分的尺寸。

4

3 回答 3

9

这是一个仅打印可见部分的高度和宽度的示例,

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

public class TestWidth {

    public static void main(String[] args) {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTextPane newsTextPane = new JTextPane();
        newsTextPane.setEditable(false);

        JScrollPane scrollPane = new JScrollPane(newsTextPane);

        frame.add(scrollPane);
        frame.setSize(300, 250);
        frame.setVisible(true);

        System.out.println("Height : " + scrollPane.getViewport().getSize().height + "\nWidth :" + scrollPane.getViewport().getSize().width);
    }
}
于 2012-05-06T17:37:46.467 回答
5

你看看JViewport,你可以 从JScrollPane派生JViewport

于 2012-05-06T17:26:28.907 回答
4

我认为您正在寻找JComponent#getVisibleRect()

返回 的Component“可见矩形” - 此组件的可见矩形new Rectangle(0, 0, getWidth(), getHeight())与其所有祖先的可见矩形的交集。

于 2012-05-06T17:37:32.103 回答