BorderLayout West Center East全部等宽

whi*_*ant 1 java swing awt layout-manager border-layout

我正在向面板动态添加对象,然后添加到我的边框布局.我需要西部,中部和东部的大小相同.这是我到目前为止:

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++;
}
}
Run Code Online (Sandbox Code Playgroud)

我希望这会奏效,但事实并非如此.我已经做了一些搜索的,似乎无法找到任何东西,说你可以或不可以设置的大小WEST,CENTEREAST面板.有谁知道如何做到这一点 ?

And*_*son 5

我需要西部,中部和东部都是相同的大小..

单行GridLayoutBorderLayout.CENTER一个的嵌套布局将实现这一目标.