Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JTextArea更改为JScrollPane会导致其不可见_Java_Swing_Jframe - Fatal编程技术网

Java 将JTextArea更改为JScrollPane会导致其不可见

Java 将JTextArea更改为JScrollPane会导致其不可见,java,swing,jframe,Java,Swing,Jframe,我对JScrollPanes和JTextArea对象存在问题,并让它们一起工作 如果我只是在我的JPanel中添加一个JTextArea,它工作得很好,并显示在我告诉它的地方。但是,如果我将contentPane.add(textArea)更改为contentPane.add(new-JScrollPane(textArea)),则textArea不再可见,也没有textArea的迹象 这是我的密码: public docToolGUI() { setDefaultCloseO

我对JScrollPanes和JTextArea对象存在问题,并让它们一起工作

如果我只是在我的JPanel中添加一个JTextArea,它工作得很好,并显示在我告诉它的地方。但是,如果我将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));




    }

这是因为您必须设置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);

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

textArea = new JTextArea(rows, columns);
从 :

JTextArea构造函数的两个参数是关于 文本区域应分别显示的行数和列数 展示。包含文本区域的滚动窗格会注意 这些提示用于确定滚动窗格的大小


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

另外,忽略newToolWorker().execute(),它用于程序的不同部分。永远不要手动调整大小/定位,而是使用合适的LayoutManager do.not.do.any.manual.sizing.positioning。曾经另外,不需要setVisible(默认情况下组件是可见的)它有什么问题?这也是为了适应原来的帖子而写的。但是谢谢你阻止我以后这么做。