[英]JTextArea keeps scrolling the main panel to the top
我定義了一個JTextArea,如下所示:
JTextArea textArea = new JTextArea();
textArea.setText("Some text");
textArea.setEditable(true);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.getVerticalScrollBar().setAutoscrolls(false);
現在,此組件是已添加到主JPanel中的許多組件之一,其定義如下:
JPanel panel = new JPanel();
panel.setName("Some name");
JScrollPane scrollPane = new JScrollPane();
scrollPane.setName("Some name");
scrollPane.getViewport().add(panel);
scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.getVerticalScrollBar().setAutoscrolls(false);
現在,JTextArea組件顯示在主面板底部附近,並且部分隱藏,因此您必須向下滾動才能看到所有內容。 但是我遇到的問題是,當我單擊它時,主面板會自動再次滾動回到頂部,再次隱藏了我的大部分JTextArea組件。 因此,在單擊它之后,用戶必須再次向下滾動才能鍵入內容。
但是我不知道為什么要這么做。 使用JTextArea是否有問題? 如果我使用JTextField,則不會發生此問題。
任何幫助將不勝感激!!
我認為,用scrollPane.getViewport().add(panel)
scrollPane.getViewport().setView(panel)
替換scrollPane.getViewport().add(panel)
scrollPane.getViewport().setView(panel)
將解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.