首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向JTextArea添加JScrollBar

向JTextArea添加JScrollBar
EN

Stack Overflow用户
提问于 2014-05-27 04:21:47
回答 2查看 3.4K关注 0票数 0

我正在尝试制作一个GUI服务器端到客户端的消息程序。我编写了网络端,但这与问题无关。我正在尝试用JScrollBar制作一个JTextArea卷轴。我该怎么做呢?下面是我的客户端代码(删除了大部分网络代码):

代码语言:javascript
复制
public class MyClient extends JFrame
{

    public Client client;
    public static Scanner scanner;      
    public JTextField textField;
    public JLabel label;        
    public static String string;        
    public static JTextArea textArea;           
    public String username;                 
    public JScrollBar scrollBar;        
    public JScrollPane scrollPane;

    public MyClient()
    {                   
        setTitle("Client");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new FlowLayout());                    
        textArea = new JTextArea("");           
        scrollBar = new JScrollBar();           
        label = new JLabel("Please enter your message");
        add(label);         
        textField = new JTextField(70);
        add(textField);
        textField.addActionListener(new ActionListener()
        {               
            @Override
            public void actionPerformed(ActionEvent e) 
            {                   
                textArea.append(username + ": " + textField.getText() + "\n");                  textField.setText(null);                    
            }               
        }); 

        add(textArea);          
        add(scrollBar, BorderLayout.EAST);          
        string = textField.getText();                       
        scanner = new Scanner(System.in);           
    }

     class MyAdjustmentListener implements AdjustmentListener 
     {

            public void adjustmentValueChanged(AdjustmentEvent e) 
            {                   
               label.setText("    New Value is " + e.getValue() + "      ");
               repaint();                  
            }               
        }

    public static void main(String[] args) throws IOException
    {           
        SwingUtilities.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {                   
                MyClient myClient = new MyClient();
                myClient.setVisible(true);
                myClient.setResizable(false);                   
            }

        });         
    }       
}
EN

回答 2

Stack Overflow用户

发布于 2014-05-27 04:29:01

您将需要一个JScrollPane而不是JScrollBar,请尝试以下代码:

代码语言:javascript
复制
JTextArea textArea = new JTextArea ("");

JScrollPane scroll = new JScrollPane (textArea, 
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

在上面的代码中,您将一个textArea分配给一个ScrollPane,并使其滚动verticallyhorizontally

另一种方法是创建包含TextArea的ScrollPane,然后设置垂直滚动= always on:

代码语言:javascript
复制
JTextArea textArea= new JTextArea();     
JScrollPane scroll= new JScrollPane(textArea); 

scroll. setVerticalScrollBarPolicy( JScrollPane. VERTICAL_SCROLLBAR_ALWAYS ); 

点击这里阅读教程:Tutorial Link

票数 2
EN

Stack Overflow用户

发布于 2014-05-27 04:26:45

只需在JScrollPane中添加JTextArea,然后在容器中添加JScrollPane,而不是添加JTextArea本身。

不需要添加JScrollBar,如果需要,默认情况下会显示滚动条。

示例代码:

代码语言:javascript
复制
textArea = new JTextArea("");
scrollPane = new JScrollPane(textArea);

在此处查找工作示例How can we add JScrollPane on JTextArea in java?

我在你的代码中注意到了一些要点:

  1. JFrame默认布局更改为FlowLayout

setLayout(新的FlowLayout());

  • 现在根据BorderLayout属性添加组件

add(scrollBar,BorderLayout.EAST);

  • 声明了很多实例变量,但从未在代码中使用过。

注意:首先在容器中添加组件,例如JPanel,然后最后在JFrame中添加JPanel

请再读一遍A Visual Guide to Layout Managers,并为您的应用程序设计选择合适的布局管理器。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23877214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档