java BoxLayout面板的对齐方式 [英] java BoxLayout panel's alignment

查看:1860
本文介绍了java BoxLayout面板的对齐方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览过,并没有找到专门针对我的情况定制的解决方案。我有一个显示在对话框中的面板:

I have browsed around and haven't found a solution that specifically tailors to my situation. I have a panel that I display in a dialog box:

//create dialog panel
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(headerPanel);
panel.add(type1Panel);
panel.add(type2Panel);
panel.add(type3Panel);
panel.add(type4Panel);
panel.add(type5Panel);
panel.add(type6Panel);

int result = JOptionPane.showConfirmDialog(null, panel, "Please enter values.", JOptionPane.OK_CANCEL_OPTION);

最后两个面板的尺寸,类型5& type6,大小相同,所以它们看起来很好。但是,标题和前4个面板的大小不同,我希望它们全部左对齐。到目前为止,我还没有找到一个很好的解决方案,如何解决这个问题。

The size of the last two panels, type5 & type6, are of equal size so they look fine. However, the header and first 4 panels are of different sizes and I would like them all to be left aligned. As of yet I haven't found a good solution as how to fix this.

问题是,我怎么能保持对齐前5个面板,而不是最后2个?如果不是,我怎么能将它们全部对齐? setalignmentx()不适用于面板。我已经尝试过使用GridLayout,但是gui主窗口的宽度相当大,并且不适合屏幕,因此BoxLayout沿Y轴。感谢任何帮助或建议。

Question is, how can I left align the first 5 panels, but not last 2? If not how can I left align them all? The setalignmentx() isn't available for panels. I've tried using GridLayout, but then the width of the gui's main window is rather large and doesn't fit nicely onto the screen, hence the BoxLayout along Y axis.Thanks for any help or suggestions.

推荐答案

这是一个示例,它将保留所有添加到用作容器的面板的JPanel。

Here is an example that will left align all the JPanels added to the panel used as a container.

   JPanel a = new JPanel();
   JPanel b = new JPanel();
   JPanel c = new JPanel();

   a.setBackground( Color.RED );
   b.setBackground( Color.GREEN  );
   c.setBackground( Color.BLUE );

   a.setMaximumSize( new Dimension(  10, 10) );
   b.setMaximumSize( new Dimension(  50, 10) );

   a.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
   b.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
   c.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0

   JPanel panel = new JPanel();
   panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
   panel.add(a);
   panel.add(b);
   panel.add(c); 

   int result = JOptionPane.showConfirmDialog(null, panel, "Please enter values.", JOptionPane.OK_CANCEL_OPTION);

这篇关于java BoxLayout面板的对齐方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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