JScrollPane不滚动

时间:2012-10-27 17:35:49

标签: 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);
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);

1 个答案:

答案 0 :(得分:2)

它不向下滚动,因为JPanel的尺寸不大于JScrollPane的视口。您必须通过调用JPanel来增加*Object name*.setPreferredSize(new Dimension(int w, int h))的尺寸,通过在 JTextArea <中显示文字来正确地做到这一点/ strong>,这是为此目的而制作的组件。

例如:

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* );
}

Oracle有一个关于如何使用JTextAreas的页面,您还可以查看API以了解其他使用方法