简体   繁体   English

向右或向左添加JPanel

[英]add JPanel to right or left into JPanel

i work with java swing I've tried witj some layout but they didnt'work. 我使用Java swing我尝试过一些布局,但是他们没有用。 I have a mainPanel that contains some panel,or to the right or to the left ,One below other,obviously. 我有一个mainPanel,包含一些面板,或者在右边或左边,一个在另一个下面。

anyone knows how to do ? 有谁知道该怎么办? thanks to all 谢谢大家

在此处输入图片说明

it's a chat app, 这是一个聊天应用

If you are just displaying text in the panel you might be able to use a JTextPane with right/left aligned text as shown here: Java Swing JTextArea write both left and right 如果您只是在面板中显示文本,则可以使用JTextPane并使用左右对齐的文本,如下所示: Java Swing JTextArea可以左右书写

Or you want can use a GridBagLayout with one component per column. 或者,您可以将GridBagLayout与每列一个组件一起使用。 You would then need to use: 然后,您需要使用:

  1. the fill constraint on each component so that is fills the width available in the row. 每个组件上的fill约束,以便填充行中可用的宽度。
  2. then for each component you would use the anchor constraint so the component is either on the LINE_START or LINE_END . 那么对于每个组件,您将使用anchor约束,因此该组件位于LINE_STARTLINE_END

Read the section from the Swing tutorial on Using a GridBagLayout for more information on each of these constraints. 阅读Swing教程中有关使用GridBagLayout的部分,以获取有关每个约束的更多信息。

Or, you could use the Relative Layout which also allows for vertical layout of panels. 或者,您可以使用相对布局 ,该布局还允许面板的垂直布局。 In this case the code would be something like: 在这种情况下,代码将类似于:

RelativeLayout rl = new RelativeLayout(RelativeLayout.Y_AXIS);
rl.setFill(true);
setLayout( rl );

JPanel left = new JPanel(new FlowLayout(FlowLayout.LEFT) );
left.add(new JLabel("left"));
add(left);

JPanel right = new JPanel(new FlowLayout(FlowLayout.RIGHT));
right.add(new JLabel("right"));
add(right);

So you just need to manage the alignment of FlowLayout of each panel. 因此,您只需要管理每个面板的FlowLayout的对齐方式即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM