有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java添加滚动到文本区域

如何将滚动条添加到文本区域。我尝试过使用此代码,但它不起作用

middlePanel=new JPanel();
middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area"));

// create the middle panel components
display = new JTextArea(16, 58);
display.setEditable(false); // set textArea non-editable
scroll = new JScrollPane(display);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

//Add Textarea in to middle panel
middlePanel.add(scroll);
middlePanel.add(display);

共 (4) 个答案

  1. # 1 楼答案

    尝试将这两行代码添加到代码中。我希望它能奏效。这对我有用:)

    display.setLineWrap(true);
    display.setWrapStyleWord(true);
    

    输出的图片如下所示

    enter image description here

  2. # 2 楼答案

    使用java swing实现滚动条的最简单方法如下:

    1. 导航到“设计”视图
    2. 右键单击文本区域
    3. 选择环绕JScrollPane
  3. # 3 楼答案

    将JTextArea添加到JScrollPane后:

    scroll = new JScrollPane(display);
    

    您不需要像这样再次将其添加到其他容器中:

    middlePanel.add(display);
    

    只需删除最后一行代码,它就可以正常工作。像这样:

        middlePanel=new JPanel();
        middlePanel.setBorder(new TitledBorder(new EtchedBorder(), "Display Area"));
    
        // create the middle panel components
    
        display = new JTextArea(16, 58);
        display.setEditable(false); // set textArea non-editable
        scroll = new JScrollPane(display);
        scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    
        //Add Textarea in to middle panel
        middlePanel.add(scroll);
    

    JScrollPane只是另一个容器,它可以在组件需要时在组件周围放置滚动条,并且有自己的布局。当你想把任何东西包装成一个卷轴时,只需把它传递给JScrollPane构造函数:

    new JScrollPane( myComponent ) 
    

    或者像这样设置视图:

    JScrollPane pane = new JScrollPane ();
    pane.getViewport ().setView ( myComponent );
    

    附加:

    下面是一个完整的工作示例,因为您仍然没有让它工作:

    public static void main ( String[] args )
    {
        JPanel middlePanel = new JPanel ();
        middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );
    
        // create the middle panel components
    
        JTextArea display = new JTextArea ( 16, 58 );
        display.setEditable ( false ); // set textArea non-editable
        JScrollPane scroll = new JScrollPane ( display );
        scroll.setVerticalScrollBarPolicy ( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS );
    
        //Add Textarea in to middle panel
        middlePanel.add ( scroll );
    
        // My code
        JFrame frame = new JFrame ();
        frame.add ( middlePanel );
        frame.pack ();
        frame.setLocationRelativeTo ( null );
        frame.setVisible ( true );
    }
    

    以下是你得到的: enter image description here

  4. # 4 楼答案

    我天真的假设是滚动窗格的大小将自动确定

    唯一对我有效的解决方案是显式地查看JScrollPane的边界:

    import javax.swing.*;
    
    public class MyFrame extends JFrame {
    
        public MyFrame()
        {
            setBounds(100, 100, 491, 310);
            getContentPane().setLayout(null);
    
            JTextArea textField = new JTextArea();
            textField.setEditable(false);
    
            String str = "";
            for (int i = 0; i < 50; ++i)
                str += "Some text\n";
            textField.setText(str);
    
            JScrollPane scroll = new JScrollPane(textField);
            scroll.setBounds(10, 11, 455, 249);                     // <-- THIS
    
            getContentPane().add(scroll);
            setLocationRelativeTo ( null );
        }
    }
    

    也许它会帮助一些未来的游客:)