JTextArea没有滚动条

时间:2013-03-16 20:35:46

标签: java swing jscrollpane jtextarea preferredsize

private JPanel contentPane;
public Driver() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 867, 502);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);
    textArea.setBounds(10, 11, 831, 393);
    JScrollPane scroll = new JScrollPane(textArea);
    scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    textArea.setText("dfgf");
    contentPane.add(scroll);
}

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Driver frame = new Driver();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

为什么这不会显示带有滚动条的textArea

我的问题是我甚至没有看到textArea。但如果我改为contentPane.add(scroll);,我可以看到textArea但没有滚动。

3 个答案:

答案 0 :(得分:3)

“为什么这不显示带有滚动条的textArea?”

因为滚动窗格的视口有自己的布局管理器,正在改变使用&在文本区域中的位置以满足其需求

视口将使用文本区域的首选大小属性来确定如何布局。您可以通过向文本区域添加文本和/或根据需要设置行/列属性来实现此值。

更新了示例

enter image description here

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestScrollPane03 {

    public static void main(String[] args) {
        new TestScrollPane03();
    }

    public TestScrollPane03() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JTextArea textArea = new JTextArea(100, 50);
                JScrollPane scrollPane = new JScrollPane(textArea);

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(scrollPane);
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }    
}

根据对问题的更改进行了更新

正如camickr已经指出的那样,你遇到的问题与你如何布置组件有关。

contentPane.setLayout(null);只是您遇到的众多问题之一。停止使用null布局并开始使用适当的布局管理器。

答案 1 :(得分:2)

  

为什么这不会显示带有滚动条的textArea

滚动条实际上​​会显示但没有“拇指握柄”,直到您为JTextArea添加足够的文字,以便将其大小扩展到JScrollPane视口大小之外。

您正在为null contentPane使用JPanel布局。这绝不是一个好主意。在这里,JScrollPane的默认大小为0 x 0,因此永远不会出现。有很多layout managers适用于各种尺寸和尺寸。定位要求。

答案 2 :(得分:2)

当添加到滚动窗格的组件的首选大小大于滚动窗格的大小时,将自动显示滚动条。您需要使用布局管理器才能自动发生

final JTextArea textArea = new JTextArea();
textArea.setBounds(10, 11, 831, 393);
JScrollPane scroll = new JScrollPane(textArea);

您不应该使用setBounds()。相反,你会做类似的事情:

final JTextArea textArea = new JTextArea(5, 30);
JScrollPane scroll = new JScrollPane(textArea);

现在,当您加载超过5行文本时,将出现滚动条。当然,所有这些都假设您正在使用适当的布局管理器。

相关问题