0

这是我正在使用的 GUI 项目的一部分,我试图JScrollPaneJTextArea文本长于JTextArea. 对我来说看起来不错,但JScrollPane仍然没有出现。

    JTextArea textArea = new JTextArea();
    textArea.setLineWrap(true);
    textArea.setWrapStyleWord(true);
    textArea.setBounds(77, 27, 561, 146);
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setPreferredSize(new Dimension(380, 100));
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    JPanel panel= new JPanel()
    panel.add(textArea);

任何人都可以验证这种代码和平吗?

4

1 回答 1

2

你没有出现的原因JScrollPane是因为你没有将它添加到你的GUI中......

 panel.add(textArea);

应该

 panel.add(scrollPane);

为什么有人会问?
因为在这一行中: JScrollPane scrollPane = new JScrollPane(textArea);我们看到JScrollPane's构造函数接受了,因此消除了将添加到 GUI 的JTextArea/etc任何需要,因为现在是其中的一部分,反过来,应该将其添加到 GUI。textAreatextAreascrollPane

于 2014-01-24T00:31:19.797 回答