简体   繁体   English

JTextArea中的ScrollPane

[英]ScrollPane in the JTextArea

This is part of GUI project I'm working with and I'm trying to make JScrollPane to appear in the JTextArea when the text is longer than the size of the JTextArea . 这是GUI项目的一部分,我的工作,我试图让JScrollPane出现在JTextArea当文本长于大小JTextArea It looks fine to me but the JScrollPane still doesn't show up. 它对我来说很好,但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);

Can anyone verify this peace of code? 任何人都可以验证代码的安静吗?

The reason your JScrollPane isn't showing up is because you haven't added it to your GUI... 你的JScrollPane没有显示的原因是因为你没有将它添加到你的GUI ...

 panel.add(textArea);

should be 应该

 panel.add(scrollPane);

Why one might ask? 为什么有人会问?
Because in this line: JScrollPane scrollPane = new JScrollPane(textArea); 因为在这一行: JScrollPane scrollPane = new JScrollPane(textArea); we see that the JScrollPane's constructor takes in the JTextArea/etc thus removing any need to add the textArea to the GUI because the textArea is now part of the scrollPane which, in turn, should be added to the GUI. 我们看到JScrollPane's构造函数接受了JTextArea/etc因此删除了将textArea添加到GUI的任何需要,因为textArea现在是scrollPane一部分,而scrollPane又应该添加到GUI中。

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

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