我想要做的是将按钮放在应用程序的左下角。有人可以举例说明如何操作吗?
这是我的:
这是我的代码:
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);
最佳答案
如果使用JFrame,首先将frame的layout设置为null
,如果使用panel,则将layout的panel设置为null
,然后使用setBounds()
方法:
button.setBounds(x,y,width,height);
看看我为你做的这个例子:
import javax.swing.*;
import java.awt.*;
public class ButtonLocationDemo extends JFrame{
private JButton button;
public ButtonLocationDemo(){
JPanel p = new JPanel();
button = new JButton("Button");
p.setLayout(null);
button.setBounds(40,100,100,60);
p.add(button);
getContentPane().add(p);
//setLayout(null);
setDefaultCloseOperation(3);
setSize(400,400);
setVisible(true);
}
public static void main(String...args){
new ButtonLocationDemo();
}
}
关于java - 如何在 JFrame 中的任意位置设置按钮的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16756903/