如何在JFrame中的任何位置设置按钮的位置 [英] How to set the location of a button anywhere in your JFrame

查看:1211
本文介绍了如何在JFrame中的任何位置设置按钮的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的就是将按钮放在应用程序的左下方.有人可以给我举个例子吗?

What I want to do is put the button down the bottom left of the application. Could somebody just give me an example of how to do it?

这就是我所拥有的:

这是我的代码:

        super("Test");

    /**Create Components**/
    JPanel addPanel = new JPanel();
    JButton addButton= new JButton("Add");

    /**Add Components**/
    addPanel.add(addButton);
    this.add(addPanel);

    /**Set Components Properties**/
    addButton.setLocation(12, 371);
    addButton.setPreferredSize(new Dimension(116, 40));
    addPanel.setLocation(12, 371);
    addPanel.setPreferredSize(new Dimension(116, 40));

    /**Frame Properties**/
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setPreferredSize(new Dimension(dimension1, dimension2));
    this.setResizable(false);   
    this.pack();
    this.setVisible(true);

推荐答案

尝试 BorderLayout

addPanel.setLayout(new BorderLayout());
addPanel.add(addButton,BorderLayout.SOUTH);

即使在addPanel内部,您也可以使用网格布局

Even inside you addPanel you can have another panel(say bottomLeft) with Grid Layout

bottomLeft.setLayout(new GridLayout(1,3,200,0));
bottomLeft.add(addPanel)

这篇关于如何在JFrame中的任何位置设置按钮的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆