JScrollPane用法以及注意点

  JScrollPane滚动条组件,注意你想添加一个组件到里面,下面的方法不行:

JTextArea jta=new JTextArea();
JScrollPane jsp=new JScrollPane();
jsp.add(jta);

jta是不会显示的。这个很特殊,有2中方法向JScrollPane添加组件:

1.在构造方法中把要添加的组件传进去。

JTextArea jta=new JTextArea();
JScrollPane jsp=new JScrollPane(jta);

2.使用 scrollPane.getViewport().add(label) 形式:

public class Notepad extends JFrame {
        ...
      private JMenuItem newMenu = new JMenuItem("New");
    private JScrollPane scrollPane = new JScrollPane();

        ...
        setTitle("MDI Test");
          scrollPane.getViewport().add(desktop);
       getContentPane().setLayout(new BorderLayout());

原文地址:https://www.cnblogs.com/youxin/p/2601806.html