【问题标题】:How to always scroll to bottom of text area?如何始终滚动到文本区域的底部?
【发布时间】:2013-03-15 10:26:12
【问题描述】:

如何使滚动窗格始终位于正在写入新数据的滚动窗格的底部?我看不到新文本的写入。

JTextArea itTextArea = new JTextArea(10,80);
new JScrollPane(itTextArea);
xmlTextArea.setEditable(true);

【问题讨论】:

    标签: java swing scroll jtextarea caret


    【解决方案1】:
    itTextArea.setCaretPosition(itTextArea.getText().length());
    

    查看JTextComponent#setCaretPosition了解更多详情

    【讨论】:

      【解决方案2】:

      这是我用的:

      DefaultCaret caret = (DefaultCaret) textArea.getCaret();
      caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
      

      这是你如何实现它的:

      public class Test
      {
          public static void createFrame()
          {
              EventQueue.invokeLater(new Runnable()
              {
                  @Override
                  public void run()
                  {
                      JFrame frame = new JFrame("Test");
                      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                      try 
                      {
                          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                      JPanel panel = new JPanel();
                      panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
                      panel.setOpaque(true);
                      JTextArea textArea = new JTextArea(15, 50);
                      textArea.setWrapStyleWord(true);
                      textArea.setEditable(false);
                      textArea.setFont(Font.getFont(Font.SANS_SERIF));
                      JScrollPane scroller = new JScrollPane(textArea);
                      scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                      scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                      JPanel inputpanel = new JPanel();
                      inputpanel.setLayout(new FlowLayout());
                      JTextField input = new JTextField(20);
                      JButton button = new JButton("Enter");
                      DefaultCaret caret = (DefaultCaret) textArea.getCaret();
                      caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
                      panel.add(scroller);
                      inputpanel.add(input);
                      inputpanel.add(button);
                      panel.add(inputpanel);
                      frame.getContentPane().add(BorderLayout.CENTER, panel);
                      frame.pack();
                      frame.setLocationByPlatform(true);
                      frame.setVisible(true);
                      frame.setResizable(false);
                      input.requestFocus();
                  }
              });
          }
      
          public static void main(String... args)
          {
              createFrame();
          }
      }
      

      应该是什么样子的:

      【讨论】:

      • 当然,假设 Caret 是 DefaultCaret,但还是个好主意
      • @MadProgrammer 是的,我做了很多假设我可能不应该这样做:P
      • 我不喜欢使用插入符号(13 年来,从来没有需要过),所以我可能也会这样做;)
      【解决方案3】:

      对于更高级的解决方案,您可以使用Smart Scrolling

      【讨论】:

        猜你喜欢
        • 2014-12-20
        • 1970-01-01
        • 2012-02-18
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        • 2011-11-14
        • 2012-05-21
        • 1970-01-01
        相关资源
        最近更新 更多