简体   繁体   English

将JTextArea更改为JScrollPane使其不可见

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

I am having an issue with JScrollPanes and JTextArea objects and getting them to work together. 我遇到了JScrollPanes和JTextArea对象的问题,并让它们一起工作。

If I just add a JTextArea to my JPanel, it works fine and shows up where I tell it to. 如果我只是将JTextArea添加到我的JPanel中,它可以正常工作并显示我告诉它的位置。 However, if I change the contentPane.add(textArea) to contentPane.add(new JScrollPane(textArea)), the textArea is not longer visible and there is no sign of the textarea either. 但是,如果我将contentPane.add(textArea)更改为contentPane.add(new JScrollPane(textArea)),则textArea不再可见,并且没有textarea的迹象。

Here is my code: 这是我的代码:

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));




    }

Try using the constructor for JTextArea that uses 2 int values: 尝试使用使用2个int值的JTextArea的构造函数:

textArea = new JTextArea(rows, columns);

From the tutorial : 教程

The two arguments to the JTextArea constructor are hints as to the number of rows and columns, respectively, that the text area should display. JTextArea构造函数的两个参数分别是文本区域应显示的行数和列数的提示。 The scroll pane that contains the text area pays attention to these hints when determining how big the scroll pane should be. 在确定滚动窗格应该有多大时,包含文本区域的滚动窗格会注意这些提示。

EDIT: The sample above is a hint for the LayoutManager, but I just noticed that you're not using one. 编辑:上面的示例是LayoutManager的提示,但我只是注意到你没有使用它。 Unless you have a really good reason not to, you should be. 除非你有充分的理由不这样做,否则你应该这样做。

This is because you have to set the bounds of the JScrollPane and you have to make the scroll pane visible instead of the text area. 这是因为您必须设置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