繁体   English   中英

将JTextArea更改为JScrollPane使其不可见

[英]Changing JTextArea to JScrollPane Causes it to not be visible

我遇到了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));




    }

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

textArea = new JTextArea(rows, columns);

教程

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

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

这是因为您必须设置JScrollPane的边界,并且必须使滚动窗格可见而不是文本区域。

textArea = new JTextArea();
textArea.setWrapStyleWord(true);
textArea.setEditable(false);

JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setBounds(25, 11, 560, 276);
scrollPane.setVisible(true);

contentPane.add(scrollPane);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM