<p>首先把相关容器的布局方式设为 setLayout(null);</p>
Java Swing 绝对布局管理方法,null布局
原创
©著作权归作者所有:来自51CTO博客作者taoweiji2008的原创作品,请联系作者获取转载授权,否则将追究法律责任
<p>然后调用组件的 setBounds() 方法</p>
<p>设置button的位置为(100,100) 长宽分别为 60,25</p>
<p>jButton.setBounds(new Rectangle(100, 100, 60, 25));</p>
<p><img src="http://p_w_picpaths.cnitblog.com/blog/391736/201302/17145934-59cb1cb5863046349fe65e1b714a5bdf.jpg" alt="" /></p>
<p> </p>
<div class="cnblogs_Highlighter">
<pre class="brush:java;gutter:false;">import java.awt.Container;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class Log extends JFrame {
public static void main(String[] args) {
Log log = new Log();
}
private JButton btLog;
private JTextField tfUser;
private JPasswordField tfPwd;
private JCheckBox pwdKeep;
private JComboBox adminType;
public Log() {
super("固定资产管理系统");
super.setSize(380, 292);
super.setVisible(true);
super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
centered(this);
btLog = new JButton("登 录");
btLog.setBounds(new Rectangle(93, 220, 180, 30));//参数分别是坐标x,y,宽,高
this.setLayout(null);//设置布局管理器为空
this.add(btLog);
tfUser = new JTextField();
tfUser.setBounds(new Rectangle(73, 115, 220, 25));
this.add(tfUser);
tfPwd = new JPasswordField();
tfPwd.setBounds(new Rectangle(73, 150, 220, 25));
this.add(tfPwd);
pwdKeep = new JCheckBox("记住密码");
pwdKeep.setBounds(new Rectangle(68, 185, 110, 25));
this.add(pwdKeep);
adminType = new JComboBox(new String[] { "普通职员", "管理员", "高级管理员" });
adminType.setBounds(new Rectangle(183, 185, 100, 25));
this.add(adminType);
}
//布局居中方法
public void centered(Container container) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int w = container.getWidth();
int h = container.getHeight();
container.setBounds((screenSize.width - w) / 2,
(screenSize.height - h) / 2, w, h);
}
}
</pre>
</div>
<p> </p>
![](https://ucenter.51cto.com/images/noavatar_middle.gif)
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
承前启后,Java对象内存布局和对象头JVM Java