【问题标题】:Java Swing JTextArea scrollbar not showing insideJava Swing JTextArea滚动条没有显示在里面
【发布时间】:2016-04-08 14:05:15
【问题描述】:

我正在尝试在 JTextArea 中添加滚动条,但滚动条没有显示在 textarea 中..

这是我的代码,

import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Test {

    public static void main(String[] args) {
        JFrame frame = new JFrame("MySql Console");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        frame.add(panel);
        panel.setLayout(null);

        JTextArea txtQuery = new JTextArea ();
        txtQuery.setBounds(10, 10, 365, 45);        
        JScrollPane scroll = new JScrollPane (txtQuery, 
           JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        //scroll.setPreferredSize(new Dimension(100, 50));
        panel.add(scroll);
        panel.add(txtQuery);

        frame.setVisible(true);
    }

}

尝试了一些来自互联网的替代方案,但仍然无法正常工作。

【问题讨论】:

    标签: java swing awt jtextarea


    【解决方案1】:
       panel.setLayout(null);
    
        JTextArea txtQuery = new JTextArea ();
        txtQuery.setBounds(10, 10, 365, 45);        
        JScrollPane scroll = new JScrollPane (txtQuery, 
           JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        //scroll.setPreferredSize(new Dimension(100, 50));
        panel.add(scroll);
        panel.add(txtQuery);
    
    1. 不要使用空布局。布局管理器将确定组件的首选大小,然后滚动窗格可以确定何时需要滚动条。当您向文本区域添加文本时,滚动条会自动出现。

    2. 不要将文本区域添加到面板。一个组件只能有一个父级。您已经将文本区域添加到滚动窗格中。

    3. 通过指定文本区域的行/列,为文本区域提供首选大小。

    所以你修改后的代码应该是这样的:

       //panel.setLayout(null);
    
        JTextArea txtQuery = new JTextArea (5, 20);
        //txtQuery.setBounds(10, 10, 365, 45);        
        JScrollPane scroll = new JScrollPane (txtQuery);
        //scroll.setPreferredSize(new Dimension(100, 50));
        panel.add(scroll);
        //panel.add(txtQuery);
    

    【讨论】:

      【解决方案2】:

      请将它们删除到下面一行,然后一切都会正常。

      panel.setLayout(null);
      

      为了更有效地工作您的应用程序,还要考虑@camickr 答案中的要点。

      【讨论】:

        【解决方案3】:

        将视口添加到您的 JTextArea 会有帮助吗?

        类似于jScrollPane.setViewportView(jTextArea); 的东西? 如果这不起作用,您可能需要制作一个 GroupLayout 块。考虑到所有事情,这并不一定很难。我有这个:

        javax.swing.GroupLayout layout = new 
        javax.swing.GroupLayout(getContentPane());
                getContentPane().setLayout(layout);
                layout.setHorizontalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(118, Short.MAX_VALUE)
                        .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(116, 116, 116))
                );
                layout.setVerticalGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(105, Short.MAX_VALUE)
                        .addComponent(jScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(99, 99, 99))
                );
        
                pack();
            }
        

        希望这会有所帮助!祝你好运:D

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-03-05
          • 1970-01-01
          • 2015-03-09
          • 2011-09-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-17
          相关资源
          最近更新 更多