首页 > 解决方案 > 在 java 的 GUI 中,除了 .north、.south、.east、.west、.center 之外,我还能使用什么?

问题描述

   add(banner, BorderLayout.NORTH);
  add(bread, BorderLayout.CENTER);
  add(sandwiches, BorderLayout.EAST);
  add(drinks, BorderLayout.WEST);
  add(buttonPanel, BorderLayout.SOUTH);

我目前有一个 GUI,并使用了这 5 个位置分配。如果我想在右侧添加另一个面板,是否可以使用另一个?

标签: java

解决方案


我想在右侧添加另一个面板?

因此,您使用适当的布局管理器创建另一个子 JPanel:

JPanel east = new JPanel( new BorderLayout() );
east.add(sandwiches, BorderLayout.LINE_START);

JPanel anotherPanel = new JPanel( new GridLayout(0, 1) );
anotherPanel.add( new JButton("Button1") );
anotherPanel.add( new JButton("Button2") );

east.add(anotherPanel, BorderLayout.LINE_END);

//add(sandwiches, BorderLayout.EAST);
add(east, BorderLayout.EAST);

因此,“东”面板变成了一个嵌套面板,其中包含另外两个面板。


推荐阅读