如何在框架中设置按钮的X,Y位置?

时间:2012-05-19 08:46:53

标签: java swing jframe jbutton layout-manager

如何设置JButton上的JFrame位置?

3 个答案:

答案 0 :(得分:4)

  

任何人都可以告诉我在Jframe上设置按钮位置的属性。

答案很简单--->

使用正确的Layout Manager,如果你真的问题编辑了描述,SSCCE will be better

答案 1 :(得分:2)

如果您有绝对布局(绝对不能,可怕的调整大小功能和坏习惯),您可以拨打.setBounds(int x, int y, int w, int h).setLocation(int x, int y)

答案 2 :(得分:0)

尝试使用合适的布局管理器,我最喜欢的是GridBagLayout因为它易于使用。您只需创建JPanel,然后为GridBagConstraints中的每个组件创建JFrame。这是代码(主类中的函数,它是JFrame的扩展名):

public void GridBagLayoutExample() {
    JPanel pane = new JPanel(new GridBagLayout()) //create a Panel with a layout
    JButton b = new JButton('Button1');
    GridBagConstraints bconstraints = new GridBagConstraints();
    //more code here
}

有关详细信息,请查看javadocs