2012-04-14 81 views
2

每當我將jbutton添加到我的容器時,它確實非常巨大。我想到了用label.setBounds()函數會工作,但它沒有無法在Java中重新調整JButton的尺寸

public Liability_Calculator(String s) 
{ 
    super(s); 
    setSize(325,200); 
    Color customColor = Color.WHITE; 

    c = getContentPane(); 
    c.setLayout(new BorderLayout()); 


    //the button 
    ok = new JButton("OK"); 

    //ok.setSize(50, 50); 

    //HERE IS WHERE I TRY AND RESIZE! 
    ok.setBounds(30,30,50,50); 

    c.add(ok, BorderLayout.SOUTH); 

    setVisible(true); 
} 

回答

2

建議:

  • 您將要在layout managers
  • 明白爲什麼你的GUI是讀了以這種方式運行
  • 並瞭解如何使用佈局管理器以您的優勢以簡單的方式創建更好看的GUI。
  • 您還需要避免在任何gui組件上設置邊界。

例如,一個JPanel使用FlowLayout(FlowLayout.CENTER))默認情況下,你可以通過將你確定的JButton到一個JPanel,然後JPanel的到的contentPane用它來你的優勢:

ok = new JButton("OK"); 
    // ok.setBounds(30, 30, 50, 50); 

    JPanel southPanel = new JPanel(); 
    southPanel.add(ok); 

    c.add(southPanel, BorderLayout.SOUTH); 

這將改變第一個圖像到第二個:

enter image description hereenter image description here