这个JScrollPane
基于窗口是JSplitPane
.
getBounds()
, getWidth()
,getHeight()
都返回窗口的完整大小,包括不可见(可滚动)部分。
我只想知道可见部分的尺寸。
这个JScrollPane
基于窗口是JSplitPane
.
getBounds()
, getWidth()
,getHeight()
都返回窗口的完整大小,包括不可见(可滚动)部分。
我只想知道可见部分的尺寸。
这是一个仅打印可见部分的高度和宽度的示例,
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);
}
}
你看看JViewport,你可以 从JScrollPane派生JViewport
我认为您正在寻找JComponent#getVisibleRect()。
返回 的
Component
“可见矩形” - 此组件的可见矩形new Rectangle(0, 0, getWidth(), getHeight())
与其所有祖先的可见矩形的交集。