将JScrollBar添加到JTextArea

时间:2014-05-26 20:21:47

标签: java swing jtextarea scrollable jscrollbar

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

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);                   
            }

        });         
    }       
}

2 个答案:

答案 0 :(得分:2)

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

JTextArea textArea = new JTextArea ("");

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

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

另一种方法,创建包含TextArea的ScrollPane,然后设置垂直滚动=始终打开:

JTextArea textArea= new JTextArea();     
JScrollPane scroll= new JScrollPane(textArea); 

scroll. setVerticalScrollBarPolicy( JScrollPane. VERTICAL_SCROLLBAR_ALWAYS ); 

请阅读此处获取教程:Tutorial Link

答案 1 :(得分:1)

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

无需添加JScrollBar,默认情况下会显示滚动条。

示例代码:

textArea = new JTextArea("");
scrollPane = new JScrollPane(textArea);

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


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

  1. JFrame默认布局更改为FlowLayout

    setLayout(new FlowLayout()); 
    
  2. 现在根据BorderLayout属性

    添加组件
    add(scrollBar, BorderLayout.EAST);   
    
  3. 声明了许多实例变量,但从未在代码中使用过。

  4. 注意:首先在JPanel等容器中添加组件,然后最后在JPanel中添加JFrame

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