【问题标题】:Java. How to create a JPanel with resizable text area and fixed buttons area?爪哇。如何创建具有可调整大小的文本区域和固定按钮区域的 JPanel?
【发布时间】:2016-05-18 15:01:08
【问题描述】:

如何在 Java 中获得这样的布局?

|---------------------------------------------- -|
|带标签的固定尺寸“顶部”面板
|----------------------------------------------|
|可调整大小的文本区域
|它扩展到大多数父级
|面板(固定区域除外)
|
|----------------------------------------------|
|固定 JTextArea(总是 2 行
|全画幅)
|----------------------------------------------|
|右居中按钮
|----------------------------------------------|

我尝试了下面的代码,但我无法让它工作。元素是浮动的,文本区域是冲突的。我不知道如何将文本区域扩展到所有可用的框架,除了固定区域。

 JPanel top = new JPanel(new FlowLayout(FlowLayout.LEFT , 10, 10));
 top.add(label1);
  ....
 top.add(labelLast);
 add(top);
 JTextArea area1 = new JTextArea();
 add(area1);
 JTextArea area2 = new JTextArea();
 add(area2);
 JPanel bottom= new JPanel(new FlowLayout(FlowLayout.RIGHT , 10, 10));
 bottom.add(button1);
  ....
 bottom.add(buttonLast);
 add(bottom);

【问题讨论】:

    标签: java swing jpanel layout-manager


    【解决方案1】:

    在主面板上使用BorderLayout

    1. 创建一个面板并将其添加到PAGE_START。然后设置面板的布局,给面板添加组件。
    2. 使用 JTextArea 创建一个 JScrollPane 并将其添加到 CENTER
    3. 使用 JTextArea 创建一个 JScrollPane 并将其添加到 PAGE_END

    阅读 How to Use BorderLayout 上的 Swing 教程部分,了解更多信息和工作示例。

    固定的 JTextArea(总是 2 行全帧宽度)

    当你创建你使用的 JTextArea 时:

    JTextArea textArea = new JTextArea(2, 20);
    

    指定固定的行数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-02
      • 1970-01-01
      • 2020-05-27
      • 2011-11-02
      相关资源
      最近更新 更多