防止BoxLayout扩展子级 [英] Keep BoxLayout From Expanding Children

查看:119
本文介绍了防止BoxLayout扩展子级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JPanel中垂直堆叠一些JComponent,以便它们堆叠在顶部,而多余的空间在底部.我正在使用BoxLayout.每个组件都将包含一个JTextArea,如有必要,该JTextArea应该允许包装文本.因此,基本上,我希望每个组件的高度都应为显示(可能是包裹的)文本所需的最小高度.

以下是我正在做的包含的代码示例:

import javax.swing.*;
import java.awt.*;
public class TextAreaTester {
    public static void main(String[] args){
        new TextAreaTester();
    }
    public TextAreaTester(){
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
        panel.setPreferredSize(new Dimension(100,400));
        for(int i = 0; i<3; i++){
            JPanel item = new JPanel(new BorderLayout());
            JTextArea textarea = new JTextArea("this is a line of text I want to wrap if necessary");
            textarea.setWrapStyleWord(true);
            textarea.setLineWrap(true);
            textarea.setMaximumSize( textarea.getPreferredSize() );
            item.add(textarea,BorderLayout.NORTH);
            panel.add(item);
        }
        panel.add(Box.createGlue());
        frame.add(panel);
        frame.setVisible(true);
        frame.pack();  
    }
}

子级JPanels正在扩展以填充垂直空间.我尝试使用胶水是因为我认为那是胶水的作用,但它似乎什么也没做.有什么帮助吗?

注意:我发现的问题看起来几乎完全相同,但是没有一个我可以应用的答案.

解决方案

一种解决方案:使用Borderlayout将JPanels与外部JPanel嵌套在一起,并使用JPanel将BoxLayout添加到这一个BorderLayout.NORTH,也称为BorderLayout.PAGE_START:

编辑Kleopatra:

import javax.swing.*;
import java.awt.*;

public class TextAreaTester {
   public static void main(String[] args) {
      new TextAreaTester();
   }

   public TextAreaTester() {
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
      // panel.setPreferredSize(new Dimension(100,400));
      for (int i = 0; i < 3; i++) {
         JPanel item = new JPanel(new BorderLayout());
         // item.setLayout(new BoxLayout(item,BoxLayout.LINE_AXIS));
         JTextArea textarea = new JTextArea(
               "this is a line of text I want to wrap if necessary", 3, 35);
         textarea.setWrapStyleWord(true);
         textarea.setLineWrap(true);
         // textarea.setMaximumSize(textarea.getPreferredSize());
         // item.setMaximumSize( item.getPreferredSize() );
         item.add(new JScrollPane(textarea), BorderLayout.NORTH);
         panel.add(item);
      }
      panel.add(Box.createGlue());

      JPanel mainPanel = new JPanel(new BorderLayout()) {
         private final int prefW = 100;
         private final int prefH = 400;

         @Override
         public Dimension getPreferredSize() {
            return new Dimension(prefW, prefH);
         }
      };
      // mainPanel.setPreferredSize(new Dimension(100, 400));
      mainPanel.add(panel, BorderLayout.PAGE_START);

      frame.add(mainPanel);
      frame.setVisible(true);
      // frame.getContentPane().add(jp);
      frame.pack();
   }
}

I want to stack some JComponents vertically inside a JPanel so they stack at the top and any extra space is at the bottom. I'm using a BoxLayout. The components will each contain a JTextArea that should allow the text to wrap if necessary. So, basically, I want the height of each of these components to be the minimum necessary for displaying the (possibly wrapped) text.

Here's a contained code example of what I'm doing:

import javax.swing.*;
import java.awt.*;
public class TextAreaTester {
    public static void main(String[] args){
        new TextAreaTester();
    }
    public TextAreaTester(){
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel,BoxLayout.PAGE_AXIS));
        panel.setPreferredSize(new Dimension(100,400));
        for(int i = 0; i<3; i++){
            JPanel item = new JPanel(new BorderLayout());
            JTextArea textarea = new JTextArea("this is a line of text I want to wrap if necessary");
            textarea.setWrapStyleWord(true);
            textarea.setLineWrap(true);
            textarea.setMaximumSize( textarea.getPreferredSize() );
            item.add(textarea,BorderLayout.NORTH);
            panel.add(item);
        }
        panel.add(Box.createGlue());
        frame.add(panel);
        frame.setVisible(true);
        frame.pack();  
    }
}

The child JPanels are expanding to fill the vertical space. I tried using glue because I thought that's what glue was for, but it seems to do nothing at all. Any help?

Note: I have found questions that look almost identical, but none with answers I can apply.

解决方案

One solution: nest JPanels with the outer JPanel using Borderlayout and adding the BoxLayout using JPanel to this one BorderLayout.NORTH, also known as BorderLayout.PAGE_START:

Edit for Kleopatra:

import javax.swing.*;
import java.awt.*;

public class TextAreaTester {
   public static void main(String[] args) {
      new TextAreaTester();
   }

   public TextAreaTester() {
      JFrame frame = new JFrame();
      JPanel panel = new JPanel();
      panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
      // panel.setPreferredSize(new Dimension(100,400));
      for (int i = 0; i < 3; i++) {
         JPanel item = new JPanel(new BorderLayout());
         // item.setLayout(new BoxLayout(item,BoxLayout.LINE_AXIS));
         JTextArea textarea = new JTextArea(
               "this is a line of text I want to wrap if necessary", 3, 35);
         textarea.setWrapStyleWord(true);
         textarea.setLineWrap(true);
         // textarea.setMaximumSize(textarea.getPreferredSize());
         // item.setMaximumSize( item.getPreferredSize() );
         item.add(new JScrollPane(textarea), BorderLayout.NORTH);
         panel.add(item);
      }
      panel.add(Box.createGlue());

      JPanel mainPanel = new JPanel(new BorderLayout()) {
         private final int prefW = 100;
         private final int prefH = 400;

         @Override
         public Dimension getPreferredSize() {
            return new Dimension(prefW, prefH);
         }
      };
      // mainPanel.setPreferredSize(new Dimension(100, 400));
      mainPanel.add(panel, BorderLayout.PAGE_START);

      frame.add(mainPanel);
      frame.setVisible(true);
      // frame.getContentPane().add(jp);
      frame.pack();
   }
}

这篇关于防止BoxLayout扩展子级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆