首页javajscrollpaneJava Swing - 如何创建JScrollPane并设置滚动条策略

Java Swing - 如何创建JScrollPane并设置滚动条策略

我们想知道如何创建JScrollPane并设置滚动条策略。
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main {

  public static void main(String[] args) {
    JFrame window = new JFrame();
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setResizable(false);
    JTextArea textArea = new JTextArea(25, 30);

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

    window.add(textScroll);
    window.pack();
    window.setVisible(true);
  }
}