我将对象动态添加到面板,然后添加到我的边框布局。我需要西部、中心和东部的大小都相等。这是我目前所拥有的:
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
和 大小的内容东
面板。
有谁知道如何做到这一点?
最佳答案
I need the West, Center and East all to be equal size..
单行GridLayout
在 nested layout 的 BorderLayout.CENTER
中将实现这一目标。
关于java - BorderLayout West Center East all 等宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16642045/