首页 > 开发 > Java > 正文

Java Swing 绝对布局管理方法 null布局

2019-10-22 20:29:20
字体:
来源:转载
供稿:网友

首先把相关容器的布局方式设为 setLayout(null);

然后调用组件的 setBounds() 方法

设置button的位置为(100,100) 长宽分别为 60,25

jButton.setBounds(new Rectangle(100, 100, 60, 25));

Java Swing 绝对布局管理方法 null布局

  1. importjava.awt.Container; 
  2. importjava.awt.Dimension; 
  3. importjava.awt.Rectangle; 
  4. importjava.awt.Toolkit; 
  5.  
  6. importjavax.swing.JButton; 
  7. importjavax.swing.JCheckBox; 
  8. importjavax.swing.JComboBox; 
  9. importjavax.swing.JFrame; 
  10. importjavax.swing.JPasswordField; 
  11. importjavax.swing.JTextField; 
  12.  
  13. publicclassLog extendsJFrame { 
  14. publicstaticvoidmain(String[] args) { 
  15. Log log = newLog(); 
  16. privateJButton btLog; 
  17. privateJTextField tfUser; 
  18. privateJPasswordField tfPwd; 
  19. privateJCheckBox pwdKeep; 
  20. privateJComboBox adminType; 
  21.  
  22. publicLog() { 
  23. super("固定资产管理系统"); 
  24. super.setSize(380, 292); 
  25. super.setVisible(true); 
  26. super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  27. centered(this); 
  28. btLog = newJButton("登 录"); 
  29. btLog.setBounds(newRectangle(93, 220, 180, 30));//参数分别是坐标x,y,宽,高 
  30. this.setLayout(null);//设置布局管理器为空 
  31. this.add(btLog); 
  32. tfUser = newJTextField(); 
  33. tfUser.setBounds(newRectangle(73, 115, 220, 25)); 
  34. this.add(tfUser); 
  35. tfPwd = newJPasswordField(); 
  36. tfPwd.setBounds(newRectangle(73, 150, 220, 25)); 
  37. this.add(tfPwd); 
  38. pwdKeep = newJCheckBox("记住密码"); 
  39. pwdKeep.setBounds(newRectangle(68, 185, 110, 25)); 
  40. this.add(pwdKeep); 
  41. adminType = newJComboBox(newString[] { "普通职员""管理员""高级管理员"}); 
  42. adminType.setBounds(newRectangle(183, 185, 100, 25)); 
  43. this.add(adminType); 
  44.  
  45. //布局居中方法 
  46. publicvoidcentered(Container container) { 
  47. Toolkit toolkit = Toolkit.getDefaultToolkit(); 
  48. Dimension screenSize = toolkit.getScreenSize(); 
  49. intw = container.getWidth(); 
  50. inth = container.getHeight(); 
  51. container.setBounds((screenSize.width - w) / 2, 
  52. (screenSize.height - h) / 2, w, h); 
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表