【问题标题】:How to make a JPanel expand to max width in another JPanel如何使 JPanel 在另一个 JPanel 中扩展到最大宽度
【发布时间】:2012-12-24 22:27:57
【问题描述】:

我觉得我需要重新表述一下这个问题。

更新了下面的问题。

我有一个 JPanel,其中包含:

myjpanel.setLayout(new BoxLayout(selectors, BoxLayout.PAGE_AXIS));

它包含以下三个面板:

JPanel 的大小固定为 'x' 和 'y'

没有固定大小的JPanel

JPanel 没有固定大小和小高度

第二个 JPanel 包含一个 JTable,因此它会展开以填充整个高度并将底部面板一直向下推,正如预期的那样。

像这样:

t
t
m
m
m
m
m
b

t = 顶部面板, m = 中间面板, b = 底部面板。

这行得通。但是底部面板感觉不像填充父级的整个宽度,这是一个问题。

ttt
mmm
 b 

我想要这个,面板填满整个宽度:

ttt
mmm
bbb

或者这个,底部面板居中:

ttt
mmm
b

下面的老问题:

我有一个 JPanel,其中包含:

.setLayout(new BoxLayout(selectors, BoxLayout.PAGE_AXIS));

在其中,还有另外三个 JPanel。前两个是固定大小的,中间一个不是。

我希望我的底部面板仅采用所需的高度,但使用外部 JPanel 的所有可用宽度。

我尝试过使用胶水但无济于事,我宁愿不设置首选尺寸和最小/最大尺寸。 有没有办法只使用布局管理器和框架来告诉组件“填充整个父级宽度”。我宁愿不开始做一些技巧,比如设置大小和覆盖方法。

注意:我不能在内面板中添加任何胶水或填充物,只能修改外面板及其布局管理器。

尝试 1:

使用myPanel.setLayout(new GridLayout(3, 1)); 没有产生预期的结果。它做了一个这样的网格:

XX
 X

但我期待:

X
X
X

尝试 2:

使用myPanel.setLayout(new GridLayout(0,1)); 没有产生预期的结果。它做了一个这样的网格:

x
x
x

但所有面板的大小都相同,忽略了限制。

【问题讨论】:

  • 目前不可调整大小。它已经存在了
  • 我正在使用问题中所说的 BoxLayout,但我会尝试使用边框布局。一会儿

标签: java swing jpanel layout-manager boxlayout


【解决方案1】:

最简单的方法是使用另一个布局管理器,例如 GridLayout,它会自动调整组件大小以填充父容器。

myPanel.setLayout(new GridLayout(0, 1));

【讨论】:

  • ... 或 JGoodies 的 FormLayout
  • 这会与共享容器的两个固定大小的组件一起使用吗?
  • 另外,左对齐内部组件也会让我非常满意,但我似乎不能这样做,因为 setAllignmentX 方法不做蹲下
  • 我试过GridLayout,它没有垂直排列组件,而是将它们放在顶部2个,底部1个
  • @MartinNielsen:你必须像new GridLayout(0, 1);一样使用它
【解决方案2】:

你可以使用GridBagLayout,为此,使用

gridBagObject.fill = GridBagConstraints.HORIZONTAL

一位example 为您提供帮助,与 GridBagLayout 相关。

如 cmets 中所问,与此相关

BoxLayout 是另一种选择,它尊重组件的首选尺寸。如果 GridBagLayout 这么难的话,你可以试试 :-)

使用 GridBagLayout 的代码,更清晰:

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

/**
 * Created with IntelliJ IDEA.
 * User: Gagandeep Bali
 * Date: 1/10/13
 * Time: 7:43 PM
 */
public class GridBagExample
{
    private void displayGUI()
    {
        JFrame frame = new JFrame("GridBagLayout Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        CustomPanel topPanel = new CustomPanel(Color.BLUE.darker().darker());
        CustomPanel middlePanel = new CustomPanel(Color.CYAN.darker().darker());
        CustomPanel bottomPanel = new CustomPanel(Color.DARK_GRAY);

        JPanel contentPane = new JPanel();
        contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.FIRST_LINE_START;
        gbc.fill = GridBagConstraints.VERTICAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.weighty = 0.3;

        contentPane.add(topPanel, gbc);

        gbc.gridy = 1;
        contentPane.add(middlePanel, gbc);

        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.BOTH;
        contentPane.add(bottomPanel, gbc);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String... args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new GridBagExample().displayGUI();
            }
        });
    }
}

class CustomPanel extends JPanel
{
    public CustomPanel(Color backGroundColour)
    {
        setOpaque(true);
        setBackground(backGroundColour);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return (new Dimension(200, 150));
    }
}

输出:

【讨论】:

  • 没有更简单的方法吗?请记住,它只有三个垂直行中的组件。它不应该被要求去大炮。我只希望我的底部组件使用全宽。当然我不需要为此去 gridbagconstraints 吗?
  • @MartinNielsen:请看一下我在回答中编辑的 BoxLayout。
  • 嗯。我不认为你读过这个问题。我正在使用 BoxLayout!
【解决方案3】:

如果使用BorderLayout 并且b 面板位于SOUTHPAGE_END 中,它会填满整个宽度。

【讨论】:

  • BorderLayout 完美运行。谢谢你,该死的 BoxLayout :)
【解决方案4】:

这里有一些自定义的 JPanel 用于使用静态输入值进行扩展和收缩。
Java timer 用于延迟和加快面板显示时间。

 public class SlidingPanel extends JPanel {
 private int paneHeight;
 private int paneWidth;
 private int comHeight;
 private int comWidth;
 private Timer everyspeedmillisec;

/**custom class for slide up and down actions for JPanel
 * 
 */

public SlidingPanel(LayoutManager layout,Dimension panedim) {

    super(layout);  
    setPreferredSize(panedim);
    paneHeight = (int)panedim.getHeight();;
    paneWidth  = (int) panedim.getWidth(); 

}

    /**function for expanding Jpanel
 * 
 */

public void expand_sft_det(int speed ,JPanel slidingcom)
{

 //Add Jpanel for sliding 

    this.add(slidingcom);

//获取面板的高度和宽度,即我们想要显示的幻灯片高度。

 comHeight=(int)slidingcom.getMinimumSize().getHeight();

 comWidth=(int) slidingcom.getMinimumSize().getWidth(); 

//用一些静态值初始化计时器 - 中速)

   everyspeedmillisec = new Timer(30, new ActionListener() {


       private int count_timer=0; 

       public void actionPerformed(ActionEvent e) {

       count_timer++;

         if( paneHeight < comHeight){

             setPreferredSize(new Dimension(paneWidth, paneHeight));

             paneHeight+=20;

             repaint();

             revalidate();

         }
         else

             everyspeedmillisec.stop() ;
    }


  });


  everyspeedmillisec.start(); 


}

/**收缩Jpanel的函数 * */

public void shrink_sft_det(int speed ,JPanel slidingcom) {

 comHeight    = (int)slidingcom.getMinimumSize().getHeight();

     comWidth     = (int)slidingcom.getMinimumSize().getWidth();

//向上滑动的高度

     paneHeight=0;

     everyspeedmillisec = new Timer(30, new ActionListener() {

     private int count_timer=0;

 public void actionPerformed(ActionEvent e) {

     count_timer++; 

     if( paneHeight < comHeight){

           setPreferredSize(new Dimension(comWidth,comHeight));

           comHeight-=20;

           repaint();

           revalidate();

     }
     else
       everyspeedmillisec.stop() ;

 }

});
everyspeedmillisec.start(); 

}

//声明类并使用收缩onclick事件

public class SoftPanel extends JPanel implements ActionListener
, MouseListener {

JPanel MoredetPane;

JPanel SlidedetPane;

private JLabel SoftDOCLabel;

private JLabel SoftLocation;

private JLabel SoftUpdates;

private boolean mr_det_flag =true;



MoredetPane = new SlidingPanel(new GridLayout(1,1,5,5),new Dimension(300,1));

MoredetPane.setOpaque(false);

//Add some components 

        SlidedetPane = new JPanel(new GridLayout(3,2,50,25));

        SlidedetPane.setPreferredSize(new Dimension(300,200));

        SlidedetPane.setOpaque(false);

                SoftDOCLabel = new JLabel("Software Date") ;

            SoftLocation = new JLabel("Software Location") ;

        SoftUpdates  = new JLabel("Software Updates") ;


        SlidedetPane.add(SoftDOCLabel);

        SlidedetPane.add(new JLabel(softbean.getSoftDOC()));

        SlidedetPane.add(SoftLocation);

        SlidedetPane.add(new JLabel(softbean.getSoftPath()));  

        SlidedetPane.add( SoftUpdates);

        SlidedetPane.add(new JLabel(""));  

//Onclick events

      modedetails_Label.addMouseListener(new MouseAdapter() {


       @Override
       public void mousePressed(MouseEvent clickeve) {

           if(mr_det_flag){

              mr_det_flag=false;

                  ((SlidingPanel)MoredetPane).expand_sft_det(200,SlidedetPane);

                  add(SlidingPanel);
            }
            else{

            mr_det_flag=true;

          ((SlidingPanel) MoredetPane).shrink_sft_det(0,SlidedetPane);

            }


          });


    add( MoredetPane );

     }

【讨论】:

    猜你喜欢
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多