JTextArea在需要时不滚动

时间:2016-04-03 10:13:21

标签: java swing scroll jtextarea

更新:我设法通过删除

解决了这个问题
f.pack()

在构造函数方法的底部。如果这是一个解决问题的坏方法,请告诉我。

我有一个JPanel,我已经添加了一个JTextArea。此文本区域应滚动,当文本添加到文本区域时,它应自动滚动到底部。

但是,当我为窗格输入足够的文本以开始滚动时,会发生这种情况。

滚动前: enter image description here

滚动后应该发生: enter image description here

以下是相关代码:

public class GameGUI2 extends JFrame implements ActionListener                                                                                                 
{                                                                                                                                                              
   private static final long serialVersionUID = 1L;                                                                                                           
   JFrame f = new JFrame("Dungeons of Doom");                                                                                                                 
   JPanel textPanel = new JPanel();                                                                                                                           
   JPanel leftPanel = new JPanel(new GridBagLayout());                                                                                                        
   JPanel rightPanel = new JPanel(new GridBagLayout());                                                                                                       
   JPanel moveButtonPanel = new JPanel(new GridBagLayout());                                                                                                  
   JPanel extraButtonPanel = new JPanel(new GridBagLayout());                                                                                                 
   JPanel mapPanel = new JPanel();                                                                                                                            
   JTextArea mapTextArea = new JTextArea(10, 10);                                                                                                             
   JTextArea textArea = new JTextArea(20, 50);                                                                                                                
   JScrollPane scrollPane = new JScrollPane(textArea);

   // Extra stuff to do with updating the map

   // Making the buttons

   public GameGUI2()                                                                                                                                          
   {                                                                                                                                                          
       // Making the GridBagConstraints                                                                                                     

       // Setting the insets for the constraints                                                                                                                                                                                                                                                     

       f.setBounds(100, 100, 1000, 600);                       

       // Adding a scrolling text area to a panel.                                                                                                                                                    
       textPanel.add(scrollPane);                                                                                                              

       // More work with constraints                                                                                               

       f.getContentPane().add(leftPanel, BorderLayout.WEST);                                                                                                  
       f.getContentPane().add(rightPanel, BorderLayout.EAST);                                                                                                 

       textArea.setEditable(false);                                                                                                                           
       mapTextArea.setEditable(false);                                                                                                                        

       this.setVisible(false);                                                                                                                               

       // Constraint work for the buttons and map

       leftPanel.add(moveButtonPanel, leftTopC);                                                                                                              
       leftPanel.add(extraButtonPanel, leftBottomC);                                                                                                          
       leftPanel.add(mapPanel, mapC);                                                                                                                         

       mapPanel.add(mapTextArea, mapC);                                                                                                                       

       textC.gridx = 0;                                                                                                                                       
       textC.gridy = 0;                                                                                                                                       
       textC.gridwidth = 3;                                                                                                                                   
       textPanel.add(textArea, textC);                                                                                                                        

       rightPanel.add(textPanel, textC);                                                                                                                      

       // Adding buttons to panel                                                                                                                    

       // Adding action listeners for the buttons                                                                                                                                                                                                                                                  

       f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);                                                                                                   
       f.pack();                                                                                                                                              
       f.setVisible(true);                                                                                                                                    
   }                     

   public void displayInGUI(String fromServer)                                                                                                                
   {                                                                                                                                                          
      textArea.append(fromServer);         
      // Making the text area scroll to the bottom automatically.                                                                                                                   
      textArea.setCaretPosition(textArea.getDocument().getLength());                                                                                         
   }  

为什么我的textArea不滚动?我误解了什么吗?

我知道有类似的问题。有人说问题来自于设置我的JTextArea的首选大小,所以我尝试了改变这一行:

JTextArea textArea = new JTextArea(20, 50);

要:

JTextArea textArea = new JTextArea(); 

并删除:

textC.gridx = 0;                                                  
textC.gridy = 0;                                                  
textC.gridwidth = 3;                                            
textPanel.add(textArea, textC);

但后来发生了这件事:

enter image description here

非常感谢。

0 个答案:

没有答案