1

我正在打印从文本文件扫描到 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);
4

1 回答 1

2

它没有向下滚动,因为 的尺寸JPanel不大于 的视口JScrollPaneJPanel您要么必须通过调用来增加尺寸,要么*Object name*.setPreferredSize(new Dimension(int w, int h))通过a 中显示文本来以正确的方式进行操作JTextArea,这是为此目的而制作的组件。

前任:

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以了解其他使用方法

于 2012-10-27T19:10:51.133 回答