Java JScrollPane未滚动
我正在打印从文本文件扫描到JPanel的文本。文本正在显示,但没有向下滚动。有什么想法吗?这是我的密码:Java JScrollPane未滚动,java,user-interface,jframe,jpanel,Java,User Interface,Jframe,Jpanel,我正在打印从文本文件扫描到JPanel的文本。文本正在显示,但没有向下滚动。有什么想法吗?这是我的密码: rFrame = new JFrame(); rFrame.setBounds(10, 10, 502, 502); rFrame.getContentPane().setLayout(null); JPanel pan = new JPanel(); pan.setBackground(Color.WHITE); pan.setBounds(100, 100, 400, 400); r
rFrame = new JFrame();
rFrame.setBounds(10, 10, 502, 502);
rFrame.getContentPane().setLayout(null);
JPanel pan = new JPanel();
pan.setBackground(Color.WHITE);
pan.setBounds(100, 100, 400, 400);
rFrame.getContentPane().add(pan);
pan.setEnabled(false);
JScrollPane scrollBar=new JScrollPane(pan,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
rFrame.add(scrollBar);
JTextArea area = new JTextArea();
area.setText(printToFrame()); //Function prints from text file
pan.add(area);
rFrame.add(pan);
rFrame.setVisible(true);
rFrame.setLayout(new FlowLayout());
rFrame.getContentPane().add(pan);
rFrame.pack();
ClientWindow window = new ClientWindow();
window.rFrame.setVisible(true);
它不会向下滚动,因为
JPanel
的尺寸不大于JScrollPane
的视口。您可以通过调用*对象名*.setPreferredSize(新维度(intw,inth))
,增加JPanel
的维度,也可以通过在JTextArea
中显示文本来正确地增加维度
例:
Oracle有一个关于如何使用的页面,您还可以查看其他方法的使用指南pan在这里完全没有用处。将文本区域直接添加到滚动窗格中。另外,如果我没记错的话,setEnabled不适用于嵌套组件,但我可能在这里错了。你是对的,因此我删除了我的答案。WM,您需要学习Swing教程,因为所有这些都在这里进行了解释。另外,学习您在JDK安装目录的“demo”子目录中找到的SwingSet2的源代码。另外:不要设置布局(null),除非你有很好的理由(在本例中你没有),否则滚动窗格不是滚动条,也不要向面板添加文本区域或框架,因为你已经添加了滚动窗格。而不是使用设置大小,您应该从preferredSize返回适当的值或使用可滚动界面。。。依我拙见
public static void main(String[] args) {
JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta);
// disables editing
ta.setEditable(false);
// enable line wrap to wrap text around
ta.setLineWrap(true);
// words will not be cut off when wrapped around
ta.setWrapStyleWord(true);
// displays the text you read in
ta.append( *text you read in* );
}