【问题标题】:JScrollPane scroll at last added lineJScrollPane 在最后添加的行滚动
【发布时间】:2011-05-09 09:31:19
【问题描述】:

我有 JTextArea text 和 JScrollPane pane=new JScrollPane(text),我放了 pane.setAutoScrolls(true)。当我将一些文本附加到窗格在末尾滚动的组件文本(最后一行)时,如何获得它?

【问题讨论】:

  • IIRC,setAutoScrolls() 只与拖放行为有关,也就是说,如果你开始拖动 JScrollPane 内容,然后继续往外拖动,那么内容会自动滚动到正确的方向.

标签: java swing jscrollpane


【解决方案1】:

关注此线程的链接ScrollPane scroll to bottom problem

【讨论】:

    【解决方案2】:

    Rob Camick 对插入符号如何移动的最佳(据我所知是最新的)解释:

    http://tips4java.wordpress.com/2008/10/22/text-area-scrolling/

    【讨论】:

      【解决方案3】:

      有没有可能,你不在 EDT 上? 如果 EDT 上没有追加,则 JTextArea 的位置不会更新。

      显示此行为的简短、可运行的示例:

      import java.awt.TextArea;
      
      import javax.swing.JFrame;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.SwingUtilities;
      
      
      public class Sample {
      
          public static void main(String[] args) {
      
              /*
               * Not on EDT
               */
              showAndFillTextArea("Not on EDT", 0, 0);
      
              /*
               * On EDT
               */
              SwingUtilities.invokeLater(new Runnable() {
      
                  @Override
                  public void run() {
                      showAndFillTextArea("On EDT", 400, 0);
                  }
              });
          }
      
          private static void showAndFillTextArea(String title, int x, int y) {
      
              JFrame frame = new JFrame(title);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JTextArea textArea = new JTextArea(20, 20);
              JScrollPane scrollPane = new JScrollPane(textArea);
              frame.getContentPane().add(scrollPane);
              frame.pack();
              frame.setLocation(x, y);
              frame.setVisible(true);
              for(int i = 0; i < 50; i++) {
                  textArea.append("Line" + i + "\n");
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-22
        • 2020-10-10
        • 1970-01-01
        • 1970-01-01
        • 2014-08-29
        • 1970-01-01
        相关资源
        最近更新 更多