Chinaunix首页 | 论坛 | 博客
  • 博客访问: 403249
  • 博文数量: 79
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 897
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-10 23:22
文章分类

全部博文(79)

文章存档

2011年(1)

2010年(6)

2009年(39)

2008年(33)

我的朋友

分类: Java

2008-10-23 17:54:16

    在使用JAVA开发图形界面时,经常会苦于控件的布局设计。最近开发一个小工具的时候,要设计出一个简单的界面,但是现有的布局管理器件不能满足需求,于是自己设计了一个,虽然说比较简单,但是个人觉得很实用。
    简单的说明一下该布局管理器的用法:
    根据控件的行号和列号进行从上到下,从左到右的布局。布局的过程中要考虑控件与控件之间的"左间距","上间距",还要考虑本身的"宽度","高度"。控制好这几个参数,就能布局出比较清晰,抑或比较复杂的用户界面。
    布局管理器设计草图如下:

中间用红线标明的几个参数"左间距","上间距","控件的高度","控件的宽度",也是几个核心的参数。当然还有控件所在的行号和列号也很重要。

其中可以看出并不是每一列都必须对的很整齐,可以调整参数实现复杂的布局。在以下示例二中可以更加具体的体现出来。

示例一截图如下:

LocationLayout.java源代码如下:

 


import java.awt.*;
import javax.swing.*;
/**
 * 控件位置布局管理器
 * @author caiqq
 *
 */

public class LocationLayout {
    /** X坐标的位置 */
    private int startX=25;
    /** Y坐标的位置 */
    private int startY=25;
    /** 上一行控件的高度 */
    private int lastRowHeight=50;
    /** 上一列控件的宽度 */
    private int lastColumnWidth=50;
    /** 上一列的编号 */
    private int lastRowNumber=0;
    /** 左间距 */
    static public int leftDistance=25;
    /** 上间距 */
    static public int upDistance=25;
    /** 控件的宽 */
    private int objectWidth=25;
    /** 控件的高 */
    private int objectHeight=25;
    
    /**
     * 默认构造函数
     */

     public LocationLayout()
    {
        
    }
     /**
     * 控件居中
     * @param component
     * 需要居中的控件
     */

    static public void setCenter(Component component)
    {
        int windowWidth = component.getWidth(); //获得窗口宽

        int windowHeight = component.getHeight(); //获得窗口高

        Toolkit kit = Toolkit.getDefaultToolkit();         //定义工具包

        Dimension screenSize = kit.getScreenSize();         //获取屏幕的尺寸

        int screenWidth = screenSize.width;         //获取屏幕的宽

        int screenHeight = screenSize.height;         //获取屏幕的高

        component.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);//设置窗口居中显示

    }
    
    /**
     * 设置控件左间距
     * @param left
     * 左间距    
     */

    public void setLeftDistance(int left)
    {
        leftDistance=left;    
    }
    
    /**
     * 设置控件上间距
     * @param up
     * 上间距    
     */

    public void setUpDistance(int up)
    {        
        upDistance=up;
    }

    /**
     * 设置控件的大小
     * @param width
     * 控件的宽
     * @param height
     * 控件的高
     */

     public void setObjectSize(int width,int height)
    {
        objectWidth=width;
        objectHeight=height;
    }

   /**
    * 根据控件的行号,列号,左边距,上边距,控件的宽,高实现控件的布局
    * @param object
    * 控件--支持各种控件
    * @param rowLine
    * 控件所在的行号
    * @param columnLine
    * 控件所在的列号
    * @param objectWidth
    * 控件的宽
    * @param objectHeight
    * 控件的高
    */

     public void setPosition(Object object,int rowLine, int columnLine,int objectWidth,int objectHeight)
    {
        //调整Y坐标位置;

        if(rowLine==0)
        {
            startY=upDistance;
            lastRowHeight=0;
        }
        else if((lastRowNumber+1)==rowLine)//如果行号增加了,距离也随之增加

        {
            startY+=(upDistance+lastRowHeight);
        }    
        
        //调整X坐标位置;

        if(columnLine==0)
        {
            startX=leftDistance;
            lastColumnWidth=0;
        }
        else
        {
            startX+=leftDistance+lastColumnWidth;            
        }    
        
        lastColumnWidth=objectWidth;
        lastRowHeight=objectHeight;        
        lastRowNumber=rowLine;    
        
        if(object==null)
            return;
        
        //JButton

        if(object.toString().startsWith("javax.swing.JButton"))
        {
            JButton JButton=(JButton)object ;
            JButton.setLocation(new Point(startX, startY));    
            JButton.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JLabel

        else if(object.toString().startsWith("javax.swing.JLabel"))
        {
            JLabel JLabel=(JLabel)object ;
            JLabel.setLocation(new Point(startX, startY));    
            JLabel.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JTextField

        else if(object.toString().startsWith("javax.swing.JTextField"))
        {
            JTextField JTextField=(JTextField)object ;
            JTextField.setLocation(new Point(startX, startY));    
            JTextField.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JPasswordField

        else if(object.toString().startsWith("javax.swing.JPasswordField"))
        {
            JPasswordField JPasswordField=(JPasswordField)object ;
            JPasswordField.setLocation(new Point(startX, startY));    
            JPasswordField.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JComboBox

        else if(object.toString().startsWith("javax.swing.JComboBox"))
        {
            JComboBox JComboBox=(JComboBox)object ;
            JComboBox.setLocation(new Point(startX, startY));    
            JComboBox.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JList

        else if(object.toString().startsWith("javax.swing.JList"))
        {
            JList JList=(JList)object ;
            JList.setLocation(new Point(startX, startY));    
            JList.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JRadioButton

        else if(object.toString().startsWith("javax.swing.JRadioButton"))
        {
            JRadioButton JRadioButton=(JRadioButton)object ;
            JRadioButton.setLocation(new Point(startX, startY));    
            JRadioButton.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JCheckBox

        else if(object.toString().startsWith("javax.swing.JCheckBox"))
        {
            JCheckBox JCheckBox=(JCheckBox)object ;
            JCheckBox.setLocation(new Point(startX, startY));    
            JCheckBox.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JTree

        else if(object.toString().startsWith("javax.swing.JTree"))
        {
            JTree JTree=(JTree)object ;
            JTree.setLocation(new Point(startX, startY));    
            JTree.setSize(new Dimension(objectWidth, objectHeight));
        }
        //JTextArea

        else if(object.toString().startsWith("javax.swing.JTextArea"))
        {
            JTextArea JTextArea=(JTextArea)object ;
            JTextArea.setLocation(new Point(startX, startY));    
            JTextArea.setSize(new Dimension(objectWidth, objectHeight));
        }
    }
}

测试代码testFrame.java如下:

 

import javax.swing.*;
public class testFrame extends JFrame {
    
    private JLabel jLabel1 = null;
    private JLabel jLabel2 = null;
    private JLabel jLabel3 = null;
    private JLabel jLabel4 = null;
    private JLabel jLabel5 = null;
    private JComboBox dataBaseTypeBox = null;    
    private JTextField urlTextField = null;
    private JTextField dataBaseTextField = null;
    private JTextField accountTextField = null;
    private JPasswordField PasswordField = null;
    private JButton testConnButton = null;
    private JPanel jContentPane = null;
    public testFrame()
    {
        setTitle("布局管理器");    
        setSize(320, 350);
        initialize();
        setLayout();        
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    }
    /**
     * 初始化控件
     */

    private void initialize() {        
        jLabel1 = new JLabel();
        jLabel1.setText("数据库类型:");
        jLabel2 = new JLabel();
        jLabel2.setText("数据库URL:");
        jLabel3 = new JLabel();
        jLabel3.setText("数据库名:");
        jLabel4 = new JLabel();
        jLabel4.setText("帐户:");
        jLabel5 = new JLabel();
        jLabel5.setText("密码:");
        dataBaseTypeBox=new JComboBox();
        dataBaseTypeBox.addItem("Oracle");
        dataBaseTypeBox.addItem("DB2");
        urlTextField=new JTextField();
        dataBaseTextField=new JTextField();
        accountTextField=new JTextField();
        PasswordField=new JPasswordField();
        testConnButton=new JButton();
        testConnButton.setText("测试连接");
        jContentPane = new JPanel();
        jContentPane.setLayout(null);
        jContentPane.add(dataBaseTypeBox);
        jContentPane.add(urlTextField);
        jContentPane.add(dataBaseTextField);
        jContentPane.add(accountTextField);
        jContentPane.add(PasswordField);
        jContentPane.add(testConnButton);
        jContentPane.add(jLabel1);
        jContentPane.add(jLabel2);
        jContentPane.add(jLabel3);
        jContentPane.add(jLabel4);
        jContentPane.add(jLabel5);        
        setContentPane(jContentPane);
    }
    /**
     * 显示窗口
     */

    private void setLayout()
    {
        LocationLayout locationLayout=new LocationLayout();
        //窗口居中

        locationLayout.setCenter(this);
        //使用默认的左边距25,上边距25

        //第0行控件        

        locationLayout.setPosition(jLabel1, 0, 0,70, 25);
        locationLayout.setPosition(dataBaseTypeBox, 0, 1, 150, 25);
        //第1行控件

        locationLayout.setPosition(jLabel2, 1, 0, 70, 25);
        locationLayout.setPosition(urlTextField, 1, 1,150, 25);
        //第2行控件

        locationLayout.setPosition(jLabel3, 2, 0,70, 25);
        locationLayout.setPosition(dataBaseTextField, 2, 1, 150, 25);    
        //第3行控件

        locationLayout.setPosition(jLabel4, 3, 0, 70, 25);
        locationLayout.setPosition(accountTextField, 3, 1, 150, 25);
        //第4行控件

        locationLayout.setPosition(jLabel5, 4, 0, 70, 25);
        locationLayout.setPosition(PasswordField, 4, 1, 150, 25);
        //调整左边距为120

        //第5行控件

        locationLayout.setLeftDistance(120);
        locationLayout.setPosition(testConnButton, 5, 0, 90, 25);        
        setVisible(true);
    }
    public static void main(String[] arg)
    {
        new testFrame();
    }
}

代码分析:

  仔细分析测试代码中的setLayout()方法,可以看出:
  1、使用本布局管理器,首先得设置默认的布局管理器为"null",即jContentPane.setLayout(null); 
 2、在标明控件名行号和列号时,一定要一行一行的递增,一列一列的递增,并且第一行和第一列都是从"0"开始。

示例二截图如下:
这是我做数据导入小工具时的图形界面,相对来说比上个界面要复杂些,但也是用的这个布局管理器完成的。

文件: 源代码和文档.rar
大小: 9KB
下载: 下载
如果有兴趣可以与我交流:QQ:409703052
阅读(3422) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-07-16 18:53:25

KK娱乐视频网,快乐齐分享 www.yulekk.com 搞笑视频,动漫视频,美女写真,靓丽车模,美女翻唱,精彩MV,经典DV