2012-04-14 82 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