JTextArea没有滚动条 [英] JTextArea no scroll bar

查看:459
本文介绍了JTextArea没有滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

Why this doesn't not show the textArea with a scroll bar?

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

my problem is that I don't see even the textArea. but if I do contentPane.add(scroll); instead, I can see textArea but no scroll.

推荐答案

为什么不显示带有滚动条的textArea?"

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

Because the viewport of the scrollpane has its own layout manager and is changing the use & position in the text area to suit its needs

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

The viewport will use The text area's preferred size property to determine how to lay it out. You can effect this value by adding text to the text area and/or setting the row/column properties according to your needs.

已更新示例

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已经指出,您遇到的问题与组件的布局方式有关.

As camickr has already pointed out, the problem you are having is related to the how you are laying out your components.

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

This contentPane.setLayout(null); is just one of your many problems. Stop using null layouts and start using appropriate layout managers.

这篇关于JTextArea没有滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆