将JTextArea更改为JScrollPane使其不可见

use*_*716 3 java swing jframe

我遇到了JScrollPanes和JTextArea对象的问题,并让它们一起工作.

如果我只是将JTextArea添加到我的JPanel中,它可以正常工作并显示我告诉它的位置.但是,如果我将contentPane.add(textArea)更改为contentPane.add(new JScrollPane(textArea)),则textArea不再可见,并且没有textarea的迹象.

这是我的代码:

public docToolGUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 611, 487);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(253, 323, 86, 20);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblEnterRootDirectory = new JLabel("Enter Root Directory");
        lblEnterRootDirectory.setBounds(253, 293, 127, 20);
        contentPane.add(lblEnterRootDirectory);

        JButton btnGo = new JButton("Go");
        btnGo.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                new ToolWorker().execute();
            }
        });
        btnGo.setBounds(253, 361, 89, 23);
        contentPane.add(btnGo);

        textArea = new JTextArea();
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        textArea.setBounds(25, 11, 560, 276);
        contentPane.add(new JScrollPane(textArea));




    }
Run Code Online (Sandbox Code Playgroud)

spl*_*bob 5

尝试使用使用2个int值的JTextArea的构造函数:

textArea = new JTextArea(rows, columns);
Run Code Online (Sandbox Code Playgroud)

教程:

JTextArea构造函数的两个参数分别是文本区域应显示的行数和列数的提示.在确定滚动窗格应该有多大时,包含文本区域的滚动窗格会注意这些提示.

编辑:上面的示例是LayoutManager的提示,但我只是注意到你没有使用它.除非你有充分的理由不这样做,否则你应该这样做.