如何在 Java Swing 中将 ScrollBar 添加到 JTextArea?

How can I add ScrollBar to JTextArea in Java Swing?

任何人都可以帮助我如何在 Java 中使用 Swing 向 JTextArea 添加滚动条?

当我在上面添加滚动条时,JTextArea 就消失了。

希望有人帮我在上面添加一个垂直滚动条。

补充说明将不胜感激

public class Practice extends JFrame {
    JFrame frame = new JFrame("AAA");

    JTextArea textarea = new JTextArea();
    JScrollPane scroll = new JScrollPane(textarea);
    JPanel panelForScroll = new JPanel(null);

    public Practice(){
        frame.setLayout(null);
        frame.setBounds(100,100,400,710);
        frame.setResizable(false);
        frame.setVisible(true);

        textarea.setEditable(false);
        textarea.setFont(new Font("arian", Font.BOLD, 16));
        textarea.setBounds(20, 280, 340, 70);

        panelForScroll.add(scroll);
        frame.add(panelForScroll); //can't find text area....
    }

    public static void main(String[] args) {
        new Practice();
    }
}
 JPanel panelForScroll = new JPanel(null);

这会将 NULL 布局 设置为此 JPanel。这将需要更多配置(就像您对框架对象所做的那样)。

只需删除 null(也来自 frame.setLayout(null)!)

您必须使用 Jtextpane 才能在 textarea 上获得滚动条。

JTextArea ta = new JTextArea();
JScrollPane sp = new JScrollPane(ta); 
JFrame f = new JFrame();
f.getContentPane().add(sp);

您正在将面板的布局设置为空,那么您没有指定滚动条边界。由于您的面板中只有一个组件,即滚动条,因此我建议使用 FlowLayout

您的代码中有几个错误:

  1. 您正在使用 null 布局,不鼓励这样做,因为它产生的问题多于解决方案,特别是当您尝试使用 JScrollPanes 时,因为它们采用 preferredSizeComponent 来决定是否添加滚动条。有关此的更多信息,请参阅 Why is it frowned upon to use a null layout in Swing?。要解决此问题,请删除此行:

     frame.setLayout(null);
    

    而是在组件之间使用布局管理器或 combinations of them along with borders for extra spacing

    虽然 null 布局似乎是为 Swing 新手设计复杂 GUI 的最佳、最简单和最快速的方法,但您在其中取得的进展越多,您就会发现与使用它们相关的问题越多(就是这样)

  2. 您正在从 JFrame 扩展您的 class 并且您也在其中创建了 JFrame 的实例,请使用一个或另一个。当你扩展 JFrame 你说你的 class 是一个 JFrame 因此它不能放在另一个 Container 因为 JFrame是刚性容器。我建议忘记 extends JFrame 部分,因为无论如何您都不会使用此操作生成的 JFrame 并保留您创建的对象。有关此问题的更详细答案,请参阅 。

  3. 您在添加所有元素之前让您的 GUI 可见,这可能会导致您的 GUI 不显示所有元素,直到您将鼠标悬停在它们上面,这一行:

     frame.setVisible(true);
    

    应该是您程序中的最后一行

  4. 您没有将您的程序放在 Event Dispatch Thread (EDT) 上,这使得您的应用程序不是线程安全的,您可以通过在 main 方法上编写它来修复它。

     SwingUtilities.invokeLater(new Runnable() {
         @Override
         public void run() {
             //Place your constructor here
         }
     });
    
  5. 您正在为 textArea 而不是 scrollPane 设置边界,但您真的不应该手动设置边界(再次参见第 1 点)。


现在,您可以制作一个带有 JTextAreaJScrollPane 的简单 GUI,如下所示:

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class ScrollPaneToTextArea {

    private JTextArea textArea;
    private JFrame frame;
    private JScrollPane scroll;
    
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ScrollPaneToTextArea().createAndShowGui();
            }
        });
    }
    
    public void createAndShowGui() {
        frame = new JFrame("ScrollPane to TextArea");
        textArea = new JTextArea(10, 20); //Rows and cols to be displayed
        scroll = new JScrollPane(textArea);
//      scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        frame.add(scroll); //We add the scroll, since the scroll already contains the textArea
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

生成此输出并在需要时添加滚动条(即当文本超出其在视图中可以处理的行数时)

如果您希望垂直滚动条始终出现,您可以取消注释该行:

scroll = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

这将产生以下输出:

您可以在 docs and JTextArea also in their own docs 中阅读有关 JScrollPane 的更多信息。