【问题标题】:Java swing and expand window/textareaJava swing 和展开窗口/文本区域
【发布时间】:2010-02-20 17:22:16
【问题描述】:

在下面的示例中,我希望能够在最大化或最小化窗口时扩展文本区域。现在,textarea 设置为 cols/rows。如果我点击最大化,文本区域应该随着窗口的扩大而扩大。

注意:这是一个伪示例。我可能会添加更多组件,这就是我使用 GridBagLayout 管理器的原因。

下面的版本给了我想要的:

package org.berlin.pino.test.functional.jogl;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class BasicText {

    public static JPanel buildPanel() {

        final JPanel panel = new JPanel(new GridBagLayout());
        final GridBagConstraints gc = new GridBagConstraints();

        final JTextArea text = new JTextArea("Text");
        final JScrollPane scrollPane = new JScrollPane(text); 
        gc.fill = GridBagConstraints.BOTH;
        gc.weightx = 1;
        gc.weighty = 1;        

        // Add the textarea -> scroll pane -> to the panel -> to the jframe                       
        panel.add(scrollPane, gc);        
        return panel;
    }

    public static void main(String[] args) {


        JFrame frame = new JFrame("Hello World!");
        frame.setLayout(new GridBagLayout());
        final GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.BOTH;
        gc.weightx = 1;
        gc.weighty = 1;

        frame.add(buildPanel(), gc);
        frame.setPreferredSize(new Dimension(300, 300));
        frame.setLocation(200, 100);
        frame.setBackground(Color.white);               

        frame.pack();
        frame.setVisible(true);
    }

} // End of the class //

【问题讨论】:

    标签: java swing textarea


    【解决方案1】:

    在您的GridBagConstraints 上使用

    gc.weightx = 1d;
    gc.weighty = 1d;
    

    JavaDoc for weightx/weighty 说:

    指定如何分配额外的水平/垂直空间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 2013-09-03
      • 2011-01-06
      • 2013-03-29
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      相关资源
      最近更新 更多