java swing 之绝对布局

Swing 中窗体具有默认的布局管理器,如JFrame 使用是边界布局。虽然布局管理器能够简化程序的开发,但是为了获取最大的灵活性,可以使用绝对布局,即不使用任何布局管理器,是哦那个该布局的步骤如下:
(1) 使用Container.setLayout(null)方式取消布局管理器。

(2)使用Component.setBounds()方法来设置每个控件的大小与位置。

/**
 * java 图形界面之绝对布局
 * @author gao
 */
package com.gao;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
 
public class AbsoluteLayoutDemo extends JFrame {
   private JPanel contentPane;//创建面板
   private JButton button1;//创建按钮1
   private JButton button2;//创建按钮2
   public AbsoluteLayoutDemo()
   {
       this.setTitle("绝对布局");//设置标题名字
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//默认退出
       this.setBounds(100, 100, 250, 100);//设置窗体的大小
       this.contentPane=new JPanel();//初始化面板
       this.contentPane.setLayout(null);//设置布局NULL
       this.button1=new JButton("按钮1");//给按钮名字
       this.button1.setBounds(6,6,90,30);//设置按钮名字
       this.contentPane.add(button1);//加入面板中
       this.button2=new JButton("按钮2");
       this.contentPane.add(button2);
       this.button2.setBounds(138, 26, 90, 30);
       this.add(this.contentPane);
       this.setVisible(true);
       
   }
   public static void main(String[]args)
   {
     AbsoluteLayoutDemo example=new AbsoluteLayoutDemo();
   }
}

--------------------- 
作者:Sunshine_gao 
来源:CSDN 
原文:https://blog.csdn.net/guanjungao/article/details/24842777 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值