自定义布局: EdgeLayout : 定制布局 « 图形用户界面 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 图形用户界面 » 定制布局屏幕截图 
自定义布局: EdgeLayout

/*
Code from Desktop Java Live Source

URL: http://www.sourcebeat.com/downloads/

*/


import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class EdgeLayoutExample {
    public static JPanel createPanel() {
        JPanel outerPanel = new JPanel();
        outerPanel.setLayout(new EdgeLayout());
        outerPanel.add(new JButton("West1"), EdgeLayout.WEST);
        outerPanel.add(new JButton("North1"), EdgeLayout.NORTH);
        outerPanel.add(new JButton("West2"), EdgeLayout.WEST);
        outerPanel.add(new JButton("North2"), EdgeLayout.NORTH);
        outerPanel.add(new JButton("East1"), EdgeLayout.EAST);
        outerPanel.add(new JButton("South1"), EdgeLayout.SOUTH);
        outerPanel.add(new JButton("West3"), EdgeLayout.WEST);
        outerPanel.add(new JButton("West4"), EdgeLayout.WEST);
        outerPanel.add(new JButton("South2"), EdgeLayout.SOUTH);
        outerPanel.add(new JButton("South3"), EdgeLayout.SOUTH);
        outerPanel.add(new JButton("Center1"), EdgeLayout.CENTER);
        return outerPanel;
    }
    public static void main(String[] a){
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(1);
      f.add(createPanel());
      f.pack();
      f.setVisible(true);
    
}


class EdgeLayout implements LayoutManager2, java.io.Serializable {
    private List components = new ArrayList();
    private HashMap constraints = new HashMap();

    public static final String CENTER = "center";
    public static final String NORTH = "north";
    public static final String SOUTH = "south";
    public static final String EAST = "east";
    public static final String WEST = "west";

    public void addLayoutComponent(Component comp, Object constraints) {
        synchronized (comp.getTreeLock()) {
            if (constraints instanceof String && comp != null) {
                this.components.add(comp);
                this.constraints.put(comp, constraints);
            else {
                throw new IllegalArgumentException("Invalid component constraints.");
            }
        }
    }

    public Dimension maximumLayoutSize(Container target) {
        //Return a very large size since this layout manager will fill all available space.
        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

    public float getLayoutAlignmentX(Container target) {
        //Centered on the X
        return (float0.5;
    }

    public float getLayoutAlignmentY(Container target) {
        //Centered on the Y
        return (float0.5;
    }

    public void invalidateLayout(Container target) {
        //There is no caching in EdgeLayout to there is nothing to invalidate
    }

    public void addLayoutComponent(String name, Component comp) {
        throw new IllegalArgumentException("EdgeLayout only supports addition with contraints.");
    }

    public void removeLayoutComponent(Component comp) {
        synchronized (comp.getTreeLock()) {
            this.components.remove(comp);
            this.constraints.remove(comp);
        }
    }

    public Dimension preferredLayoutSize(Container parent) {
        synchronized (parent.getTreeLock()) {
            int width = 0;
            int height = 0;

            //Add the preferred widths of all EAST/WEST components
            //Add the preferred height of all NORTH/SOUTH components
            for (int i = 0; i < this.components.size(); i++) {
                Component c = (Componentthis.components.get(i);
                if (this.constraints.get(c).equals(WEST|| this.constraints.get(c).equals(EAST)) {
                    width += c.getPreferredSize().getWidth();
                else {
                    height += c.getPreferredSize().getHeight();
                }
            }

            width += parent.getInsets().right + parent.getInsets().left;
            height += parent.getInsets().top + parent.getInsets().bottom;

            return new Dimension(width, height);
        }
    }

    public Dimension minimumLayoutSize(Container parent) {
        synchronized (parent.getTreeLock()) {
            int width = 0;
            int height = 0;

            //Add the minimum  widths of all EAST/WEST components
            //Add the minimum height of all NORTH/SOUTH components
            for (int i = 0; i < this.components.size(); i++) {
                Component c = (Componentthis.components.get(i);
                if (this.constraints.get(c).equals(WEST|| this.constraints.get(c).equals(EAST)) {
                    width += c.getMinimumSize().getWidth();
                else {
                    height += c.getMinimumSize().getHeight();
                }
            }

            width += parent.getInsets().right + parent.getInsets().left;
            height += parent.getInsets().top + parent.getInsets().bottom;

            return new Dimension(width, height);
        }
    }

    public void layoutContainer(Container parent) {
        synchronized (parent.getTreeLock()) {
            Insets insets = parent.getInsets();
            int top = insets.top;
            int left = insets.left;

            Dimension minimumSize = minimumLayoutSize(parent);

            int height = minimumSize.height;
            int width = minimumSize.width;

            int availableHeight = parent.getHeight() - insets.bottom - insets.top;
            int availableWidth = parent.getWidth() - insets.left - insets.right;
            if (height < availableHeight) {
                height = availableHeight;
            }
            if (width < availableWidth) {
                width = availableWidth;
            }

            int bottom = availableHeight;
            int right = availableWidth;

            Dimension preferredSize = preferredLayoutSize(parent);

            int preferredWidthAvailable = width - preferredSize.width;
            int preferredHeightAvailable = height - preferredSize.height;


            Component centerComp = null;

            for (int i = 0; i < this.components.size(); i++) {
                Component c = (Componentthis.components.get(i);
                String constraint = (Stringthis.constraints.get(c);

                if (constraint.equals(CENTER)) {
                    centerComp = c;
                else {
                    int compHeight;
                    int compWidth;
                    int xOrigin;
                    int yOrigin;


                    if (constraint.equals(NORTH|| constraint.equals(SOUTH)) {
                        compWidth = width;

                        if (preferredHeightAvailable > 0) {
                            int preferredHeightNeeded = c.getPreferredSize().height - c.getMinimumSize().height;
                            if (preferredHeightAvailable > preferredHeightNeeded) {
                                compHeight = c.getPreferredSize().height;
                                preferredHeightAvailable -= preferredHeightNeeded;
                            else {
                                compHeight = c.getMinimumSize().height + preferredHeightAvailable;
                                preferredHeightAvailable = 0;
                            }
                        else {
                            compHeight = c.getMinimumSize().height;
                        }
                        height = height - compHeight;

                        xOrigin = left;

                        if (constraint.equals(NORTH)) {
                            yOrigin = top;
                            top += compHeight;
                        else {
                            yOrigin = bottom - compHeight;
                            bottom = yOrigin;
                        }
                    else {
                        compHeight = height;
                        if (preferredWidthAvailable > 0) {
                            int preferredWidthNeeded = c.getPreferredSize().width - c.getMinimumSize().width;
                            if (preferredWidthAvailable > preferredWidthNeeded) {
                                compWidth = c.getPreferredSize().width;
                                preferredWidthAvailable -= preferredWidthNeeded;
                            else {
                                compWidth = c.getMinimumSize().width + preferredWidthAvailable;
                                preferredWidthAvailable = 0;
                            }
                        else {
                            compWidth = c.getMinimumSize().width;
                        }
                        width = width - compWidth;

                        yOrigin = top;

                        if (constraint.equals(WEST)) {
                            xOrigin = left;
                            left += compWidth;
                        else {
                            xOrigin = right - compWidth;
                            right = xOrigin;
                        }
                    }
                    c.setSize(compWidth, compHeight);
                    c.setBounds(xOrigin, yOrigin, compWidth, compHeight);
                }
                if (centerComp != null) {
                    c.setSize(width, height);
                    c.setBounds(left, top, width, height);
                }
            }
        }
    }
}

           
       
Related examples in the same category
1. 自定义布局管理器自定义布局管理器
2. ColumnLayoutColumnLayout
3. Applet图形用户界面的演示。TreeLayout布局管理器
4. Java J2SE的相对布局管理器
5. Basically two (or more) columns of different, but constant, widths
6. GraphPaperLayoutGraphPaperLayout
7. 自定义布局演示自定义布局演示
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.