我将对象动态添加到面板,然后添加到我的边框布局。我需要西部、中心和东部的大小都相同。这是我到目前为止所拥有的:
static int width = 300;//Screen width
static int height = width / 16 * 9;//Screen height
static int scale = 3;
private JFrame frame;
//player is an object from a Player Class will an arrayList of Items..
public void LoadPlayer(Player player){
int count = 1;
for (Items i : player.getAllItems()){
JPanel jp= new JPanel();
JLabel jlItem= new JLabel(i.getName());
BorderLayout bl = (BorderLayout) (mainPanel.getLayout()) ;
jp.add(jlItem);
jp.setBorder(BorderFactory.createLineBorder(Color.BLACK));
if (bl.getLayoutComponent(BorderLayout.WEST) == null){
mainPanel.add(jp,BorderLayout.WEST);
jp.setSize(frame.getWidth()/3, height);
System.out.println("adding item " + count+" to west panel");
}
else if (bl.getLayoutComponent(BorderLayout.CENTER) == null){
jp.setSize(frame.getWidth()/3, height);
mainPanel.add(jp,BorderLayout.CENTER);
System.out.println("adding item " + count+" to center panel");
}
else if (bl.getLayoutComponent(BorderLayout.EAST) == null){
mainPanel.add(jp.BorderLayout.EAST);
jp.setSize(frame.getWidth()/3, height);
System.out.println("adding item" + count+" to east panel");
}
count++;
}
}
我希望这会奏效,但没有。我做了一些搜索,似乎找不到任何说明您可以或不能设置WEST
,CENTER
和EAST
面板的大小的内容。有谁知道如何做到这一点 ?