简体   繁体   English

调整JTextArea的大小后如何调整JScrollPane的大小

[英]How do I resize JScrollPane after resizing a JTextArea

My JFrame Consists of three main parts a banner at top scrollpane containing a JTextArea center and a JTextField at the bottom. 我的JFrame由三个主要部分组成,顶部滚动窗格中的横幅,底部包含JTextArea中心,底部包含JTextField。 When I re-size the frame I adjust the columns and rows in my JTextArea. 调整框架大小时,我调整了JTextArea中的列和行。 When making the frame larger the JTextArea expands visually but removes the scroll-bar. 当使框架变大时,JTextArea会在视觉上扩展,但会移除滚动条。 Then if I make the frame smaller the JTextArea stays the same size. 然后,如果我缩小框架,则JTextArea保持不变。 This Is where I attempt to re-size my JTextArea. 这是我尝试调整JTextArea大小的地方。

    frame.addComponentListener(new ComponentAdapter() {//Waits for window to be resized by user
        public void componentResized(ComponentEvent e) {
            uneditTextArea.setRows(((int)((frame.getHeight()-140)/18.8)));//sets Textarea size based on window size
            uneditTextArea.setColumns(((int)((frame.getWidth()-100)/10.8)));
            frame.revalidate();//refreshes screen

            }
        });

Why would the ScrollPane not re adjust to the change in size of the TextField. 为什么ScrollPane无法重新适应TextField的大小更改。 The Rest of the code is below in case it is needed. 如果需要,下面的代码其余部分。

  public class window extends JFrame  
 {      
  private static JFrame frame = new JFrame("Lillian");      
  private static JButton inputButton = new JButton("Send");
  private static JTextField editTextArea = new JTextField(46);
  private static JTextArea uneditTextArea = new JTextArea(26,50);


  private static JPanel logoPanel = new JPanel();//Input text window
  private static JPanel itextPanel = new JPanel();//Input text window
  private static JPanel submitPanel = new JPanel();//Submit Button
  private static JPanel bottom = new JPanel();//will contain scrollpane
  private static JPanel middle = new JPanel();//willcontain itextpanel & submitbutton
  private static JPanel otextPanel = new JPanel();//Text Output
public static void runWindow() 
   { 
    ImageIcon logo = new ImageIcon("Lillian_resize.png");//banner
    ImageIcon icon = new ImageIcon("Lillian_icon.png");//application icon

    frame.setIconImage(icon.getImage());
    frame.setSize(660,640);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    
    frame.setVisible(true);

    logoPanel.setSize(10,10);
    JLabel logoLabel = new JLabel(logo);

    final JScrollPane  scrollPane = new JScrollPane(otextPanel);//adds text to panel will scrollbar    
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);//scrollbar only apears when more text than screen      
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);//scrollbar never apears 
    scrollPane.setBorder(BorderFactory.createEmptyBorder());    

    logoPanel.add(logoLabel);
    submitPanel.add(inputButton);
    itextPanel.add(editTextArea);
    otextPanel.add(uneditTextArea); 

    frame.getContentPane().add(logoPanel,"North");
    frame.getContentPane().add(middle);
    frame.getContentPane().add(bottom,"South");

    middle.add(scrollPane,"North");//adds panels to outer panel
    bottom.add(itextPanel, "West");
    bottom.add(submitPanel, "East");

    uneditTextArea.setLineWrap(true);
    uneditTextArea.setWrapStyleWord(true);          
    uneditTextArea.setEditable(false);
    uneditTextArea.setCaretPosition(uneditTextArea.getDocument().getLength());

    frame.revalidate();//refreshes screen
//---------------wait for action------------

    frame.addComponentListener(new ComponentAdapter() {//Waits for window to be resized by user
        public void componentResized(ComponentEvent e) {
            uneditTextArea.setRows(((int)((frame.getHeight()-140)/18.8)));//sets Textarea size based on window size
            uneditTextArea.setColumns(((int)((frame.getWidth()-100)/10.8)));
            frame.revalidate();//refreshes screen

            }
        });

   }
 }

There should be no need to use a ComponentListener to resize components. 无需使用ComponentListener来调整组件的大小。 That is the job of the layout managers that you use to dynamically resize the components. 这就是您用来动态调整组件大小的布局管理器的工作。

You should not be adding the text area to a JPanel first. 您不应该首先将文本区域添加到JPanel。 Instead when using text areas you would generally add the text area directly to the viewport of a JScrollPane by using code like: 相反,当使用文本区域时,通常可以使用以下代码将文本区域直接添加到JScrollPane的视口中:

JScrollPane scrollPane = new JScrollPane( textArea );

Then you add the scrollpane to the frame with code like: 然后,使用以下代码将滚动窗格添加到框架:

frame.add(scrollPane, BorderLayout.CENTER);

As you have noticed you should also NOT use hardcoded literals like "Center". 如您所见,您也不应使用诸如“ Center”之类的硬编码字面量。 Instead use the variables provided by the layout manager. 而是使用布局管理器提供的变量。 Since you are using a BorderLayout, use the variables defined in the BorderLayout class. 由于您使用的是BorderLayout,因此请使用BorderLayout类中定义的变量。

Also, you should NOT be using static variable to create your GUI. 另外,您不应使用静态变量来创建GUI。 I suggest you read the section from the Swing tutorial on Layout Manager . 我建议您阅读Layout Manager上Swing教程中的这一节。 The tutorial will give you more information and the example code will show you how to better structure your program so that you don't need to use static variables. 本教程将为您提供更多信息,示例代码将向您展示如何更好地构建程序结构,从而无需使用静态变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM