4
A
回答
3
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class CenteredCombos {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
String[] values = {"Dog","Cat"};
// put the controls in a single column, with a little space.
JPanel controlPanel = new JPanel(new GridLayout(0,1,5,5));
for (int ii=0; ii<4; ii++) {
controlPanel.add(new JComboBox(values));
}
// the GUI as seen by the user (without frame)
// The GBL is used to center the controlPanel
JPanel gui = new JPanel(new GridBagLayout());
gui.setBorder(new EmptyBorder(2, 3, 2, 3));
gui.add(controlPanel);
// Make the BG panel more clear..
gui.setBackground(Color.WHITE);
JFrame f = new JFrame("Demo");
f.add(gui);
// Ensures JVM closes after frame(s) closed and
// all non-daemon threads are finished
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// See http://stackoverflow.com/a/7143398/418556 for demo.
f.setLocationByPlatform(true);
// ensures the frame is the minimum size it needs to be
// in order display the components within it
f.pack();
// should be done last, to avoid flickering, moving,
// resizing artifacts.
f.setVisible(true);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
3
使用適當的佈局管理器,例如BoxLayout
。欲瞭解更多信息,請參閱How to Use BoxLayout。
1
你可以使用MigLayout與像[max][min][max]
行和列的限制 - 會分裂你的根面板與「貪婪」邊境一個3x3的網格,並且在中間區域(那個那分/分鐘)下降一個JPanel有垂直flow layout,在其中您可以添加您的組件
相關問題
- 1. JCombo Box增加值
- 2. 使用數據庫刷新jcombo box java中的內容
- 3. 如何將JCombo Box添加到我的java代碼
- 4. JCombo Box不更新選定的項目/ Innerclass訪問
- 5. JCombo框搜索
- 6. 使用一個JComboBox來控制另一個Jcombo Box
- 7. jCombo盒隱藏數據的Java
- 8. jcombo getSelected在拖放
- 9. JCombo中的子菜單
- 10. 自動完成JCombo Box(Java2S)在更改事件上設置錯誤值
- 11. 使用JCombo
- 12. Java gui box creater
- 13. 將JCombo列添加到JTable
- 14. 有沒有辦法通過box:// url scheme在IOS上的Box應用中定位特定文件?
- 15. 刪除JCombo Box中的所有項目,並且在組合框爲空時不觸發事件
- 16. 如何在java中使用GridBagLayout和box?
- 17. 如何在Java Applet中獲取Richtext Box?
- 18. AES中的S-box CCM 128位
- 19. 如何使用Jcombo + Listener
- 20. 定位中的Java Swing
- 21. 在Java中的Gridpane定位Fx
- 22. jcombo無法填寫字符
- 23. Box V2 API - 鎖定/解鎖位置在哪裏?
- 24. Symfony2:在DropDown Box中的佔位符是可選的
- 25. SVG in viewBox,CSS中的「box-sizing:border-box;」當組合使用時,位置變爲點
- 26. 如何在將項目添加到組合框時停止觸發擺動JCombo Box項目監聽器。
- 27. CSS - 在Wordpress Jquery Box中的按鈕位置
- 28. BOX JAVA SDK:EventLog.getEnterpriseEvents返回錯誤
- 29. 將項添加到GUI jCombo框
- 30. 使GUI Box的底角保持原位
感謝。當我嘗試使用JPanel添加圖像時,它不會顯示此代碼。 – Marcello