ScrollPane in the JTextArea



这是我正在与之合作的GUI项目的一部分,当文本长于JTextArea的大小时,我正在尝试使JScrollPane出现在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);

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

您的JScrollPane未显示的原因是因为您尚未将其添加到GUI中...

 panel.add(textArea);

应该是

 panel.add(scrollPane);

为什么有人会问?
因为在这一行中: JScrollPane scrollPane = new JScrollPane(textArea);我们看到JScrollPane's构造函数采用JTextArea/etc,从而删除将textArea添加到GUI中的任何需要,因为textArea现在是scrollPane的一部分,又应将其添加到GUI中。

相关内容

最新更新