![](/img/trans.png)
[英]In Java, is there a way to center a group of buttons with variable size(Using Swing)?
[英]Why buttons are not in the center in Java Swing?
屏幕截圖在這里: http : //imgur.com/a/33rhe
我正在使用此代碼:
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addComponent(greeting)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addGap(0,0,Short.MAX_VALUE)
.addComponent(english)
.addComponent(german)
.addComponent(french))
.addGroup(layout.createSequentialGroup()
.addGap(0,0,Short.MAX_VALUE)
.addComponent(quit))
);
因此,使用此GroupLayout.Alignment.CENTER
我希望按鈕可以集中顯示,但是它們不在渲染的中心。 問題是什么? 我做錯了什么?
我是新手,想了解那里發生的事情
我從不手動使用GroupLayout。 它通常由IDE使用。 所以我不知道為什么GroupLayout沒有按照您的期望做。
相反,我在需要時使用嵌套布局手動構建GUI。
因此,我將改為:
JPanel buttonPanel = new JPanel( new GridLayout(...) );
buttonPanel.add(...);
frame.setLayout( new GridBagLayout() );
frame.add(buttonPanel, new GridBagConstraints());
現在,“ buttonPanel”將在垂直和水平方向居中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.