【发布时间】:2013-11-16 16:17:12
【问题描述】:
我正在尝试将两个按钮放置在左上侧。不过,它们始终位于中心顶部。
我试过这个:
jp = new JPanel();
jp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//c.anchor = GridBagConstraints.BASELINE_TRAILING;
c.anchor = GridBagConstraints.WEST;
c.gridx = 0;
c.gridy = 0;
jp.add(test, c);
c.gridy++;
jp.add(atest, c);
add(jp);
但它仍然在中间,而不是在左侧 (http://i.imgur.com/MYF8dqr.png)。
这是我拍的。红色是我希望按钮的样子。
更新:
ArrayList<String> atest = new ArrayList<String>();
JLabel[ ] asd = new JLabel[100];
int temp = 0;
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.FIRST_LINE_START;
c.weightx = 1.0;
c.weighty = 1.0;
c.gridx = 0;
c.gridy = 0;
atest.add("Hello");
atest.add("haelp");
atest.add("yummy");
atest.add("whats wrong");
for(String server : servers)
{
asd[temp] = new JLabel();
asd[temp].setText(server);
jp.add(asd[temp], c);
c.weighty++;
c.gridy++;
temp++;
}
我试图从数组中读取字符串并将其作为标签一个接一个地添加到左侧。 效果不太好,结果如下: http://prntscr.com/26a9rb 如果 gridbaglayout 是不好的方式,我应该选择哪种方式?
【问题讨论】:
-
你可以试试其他布局
-
@HussainAkhtarWahid 哪一个最适合这个?
标签: java swing user-interface layout-manager gridbaglayout