-7
A
回答
4
這裏是你的代碼
public class SwingSolution extends JFrame
{
private JPanel componentPanel = null;
private JButton buttonWithWidth2 = null;
private JButton button2 = null;
private JButton buttonWithHeight2 = null;
private JButton button4 = null;
private JButton button5 = null;
public JPanel getComponentPanel()
{
if(null == componentPanel)
{
componentPanel = new JPanel();
GridBagLayout gridBagLayout = new GridBagLayout();
componentPanel.setLayout(gridBagLayout);
GridBagConstraints constraint = new GridBagConstraints();
constraint.gridx = 0;
constraint.gridy = 0;
// Set gridwidth to 2 grids
constraint.gridwidth = 2;
buttonWithWidth2 = new JButton("Button Width 2");
componentPanel.add(buttonWithWidth2, constraint);
constraint.gridx = 2;
constraint.gridy = 0;
// set the gridwidth back to normal i.e. 1 grid
constraint.gridwidth = 1;
button2 = new JButton("Button 2");
componentPanel.add(button2, constraint);
constraint.gridx = 0;
constraint.gridy = 1;
// set the gridheight to 2
constraint.gridheight = 2;
buttonWithHeight2 = new JButton("Button Height 2");
componentPanel.add(buttonWithHeight2, constraint);
constraint.gridx = 1;
constraint.gridy = 1;
// set the gridheight back to normal i.e. 1 grid
constraint.gridheight = 1;
button4 = new JButton("Button 4");
componentPanel.add(button4, constraint);
constraint.gridx = 1;
constraint.gridy = 2;
button5 = new JButton("Button 5");
componentPanel.add(button5, constraint);
}
return componentPanel;
}
public static void main(String[] args)
{
JFrame frame = new JFrame();
SwingSolution main = new SwingSolution();
frame.setTitle("Simple example");
frame.setSize(300, 200);
frame.setLocationRelativeTo(null);
frame.setContentPane(main.getComponentPanel());
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
它應該給你輸出:
+0
提前致謝。現在我明白了GridBagLayout。 – 2013-05-05 13:06:19
相關問題
- 1. 如何在瀏覽器中創建用戶界面來鏡像Java Swing UI?
- 2. 在Windows中使用Java創建「用戶帳戶」界面
- 3. Java swing用戶界面崩潰調試
- 4. 如何使用JSON元素在Xamarin.Forms中創建用戶界面
- 5. 如何在java中創建界面
- 6. 使用Flixel創建用戶界面
- 7. 在java中使用swing創建熱鍵
- 8. 使用Swing在Java中創建MasterMind
- 9. 在Java中創建Outlook風格的用戶界面?
- 10. 如何使用swift 3.0自定義用戶界面創建tabbar
- 11. 如何使用JavaFX創建用戶界面
- 12. 從java中的域中分離用戶界面Swing
- 13. 如何在asp.net中創建數據錄入用戶界面?
- 14. 如何在Wix中爲MSI創建自定義用戶界面?
- 15. 如何在Siri的用戶界面中創建泡泡
- 16. 如何在印地語中創建android用戶界面?
- 17. 如何在Android中創建高級用戶界面?
- 18. 如何在android中創建動畫和用戶界面?
- 19. 如何在Android中創建氣泡遊戲用戶界面
- 20. 如何在asp.net中創建Windows用戶界面
- 21. 如何在Android中爲MAFLogon創建自定義用戶界面?
- 22. 如何在android中創建以下用戶界面?
- 23. 如何在Iphone中創建鋼琴用戶界面?
- 24. 如何在JSF中創建用戶界面?
- 25. 使用InctantUI在Codenameone中創建動態用戶界面
- 26. 在Java中使用if語句創建用戶友好的界面
- 27. 如何使用自定義JButton在Java(Swing)中創建JOptionPane.showOptionDialog框?
- 28. Java用戶界面
- 29. 創建通用用戶界面文件
- 30. java中的用戶界面
*請給一些代碼*。我們不是代碼寫作服務。 – christopher 2013-05-05 12:20:56
嘗試使用GridBagLayout ..閱讀關於gridwidth和gridheight。基本上我的意思是說,你可以將整個用戶界面劃分爲9行和12列......只是爲了讓你理解,使第一個網格,即x = 0,y = 0(我看到時間 - 08:58: 10),網格寬度爲4 ..同樣 – Mady 2013-05-05 12:23:20
@Mady:給我參考鏈接 – 2013-05-05 12:24:36