簡體   English   中英

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

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

我的JFrame由三個主要部分組成,頂部滾動窗格中的橫幅,底部包含JTextArea中心,底部包含JTextField。 調整框架大小時,我調整了JTextArea中的列和行。 當使框架變大時,JTextArea會在視覺上擴展,但會移除滾動條。 然后,如果我縮小框架,則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

            }
        });

為什么ScrollPane無法重新適應TextField的大小更改。 如果需要,下面的代碼其余部分。

  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

            }
        });

   }
 }

無需使用ComponentListener來調整組件的大小。 這就是您用來動態調整組件大小的布局管理器的工作。

您不應該首先將文本區域添加到JPanel。 相反,當使用文本區域時,通常可以使用以下代碼將文本區域直接添加到JScrollPane的視口中:

JScrollPane scrollPane = new JScrollPane( textArea );

然后,使用以下代碼將滾動窗格添加到框架:

frame.add(scrollPane, BorderLayout.CENTER);

如您所見,您也不應使用諸如“ Center”之類的硬編碼字面量。 而是使用布局管理器提供的變量。 由於您使用的是BorderLayout,因此請使用BorderLayout類中定義的變量。

另外,您不應使用靜態變量來創建GUI。 我建議您閱讀Layout Manager上Swing教程中的這一節。 本教程將為您提供更多信息,示例代碼將向您展示如何更好地構建程序結構,從而無需使用靜態變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM