zoukankan      html  css  js  c++  java
  • Java图形化界面设计——布局管理器之null布局(空布局)


             一般容器都有默认布局方式,但是有时候需要精确指定各个组建的大小和位置,就需要用到空布局。

             操作方法:

    1)       首先利用setLayout(null)语句将容器的布局设置为null布局(空布局)。

    2)       再调用组件的setBounds(int x, int y, int width,int height)方法设置组件在容器中的大小和位置,单位均为像素。

    x为控件左边缘离窗体左边缘的距离

    y为控件上边缘离窗体上边缘的距离

    width为控件宽度

    height为控件高度

     

    实例:使用空布局精确定位组件的位置

    // NullLayoutDemo.Java

    import java.awt.*;

    import javax.swing.*;

    public class NullLayoutDemo{

      JFrame fr;

      JButton a,b;

      NullLayoutDemo() {

        fr = new JFrame();

        fr.setBounds(100,100,250,150);

             //设置窗体为空布局

        fr.setLayout(null);

        a=new JButton("按钮a");

        b=new JButton("按钮b");

        fr.getContentPane().add(a);

             //设置按钮a的精确位置

        a.setBounds(30,30,80,25);

        fr.getContentPane().add(b);

        b.setBounds(150,40,80,25);

        fr.setTitle("NullLayoutDemo");

        fr.setVisible(true);

             fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

             fr.setLocationRelativeTo(null);                  //让窗体居中显示

      }

      public static void main(String args[]){

        new NullLayoutDemo();

      }

    }

     

    程序运行结果如下:

  • 相关阅读:
    reorder-list
    HMM基础
    binary-tree-preorder-traversal
    binary-tree-postorder-traversal
    GMM基础
    (七)打印机驱动设置—认识打印机接口
    (八)打印机驱动设置—串口的设置
    (五)打印机驱动设置—没有开不了的钱箱
    (六)打印机驱动设置—装完驱动后没有打印机图标
    (四)揭开打印机驱动的神秘面纱
  • 原文地址:https://www.cnblogs.com/aipan/p/6409529.html
Copyright © 2011-2022 走看看