Java 怎么重叠组件

2019年8月18日22:00:22 评论 22

1.简介

Java 怎么重叠组件?——请看下文。

2.代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;

import java.awt.Toolkit;

public class Main {
    private static Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit()
            .getScreenSize();

    static public void overlapComponents(Component top, Component bottom,
            int offset, boolean preserveZeroLocation) {
        
        
        
        
        Point location = top.getLocation();

        
        
        
        
        if (location.x == 0 && location.y == 0 && !preserveZeroLocation) {
            
            
            
            
            
            
            location = bottom.getLocation();

            if ((location.x > SCREEN_SIZE.width)
                    || (location.y > SCREEN_SIZE.height))
                centerComponent(top, new Point(0, 0), SCREEN_SIZE);
            else {
                location.x += offset;
                location.y += offset;
                fitComponentIntoScreen(top, location);
            }
        } else
            fitComponentIntoScreen(top, location);
    }

    static public void centerComponent(Component child,
            Point parentLocation, Dimension parentSize) {
        Dimension childSize = child.getSize();

        if (childSize.width > parentSize.width)
            childSize.width = parentSize.width;
        if (childSize.height > parentSize.height)
            childSize.height = parentSize.height;
        child.setLocation(parentLocation.x + parentSize.width / 2
                - childSize.width / 2, parentLocation.y + parentSize.height
                / 2 - childSize.height / 2);
    }

    static public void fitComponentIntoScreen(Component component,
            Point location) {
        Point newLocation = fitComponentInsideScreen(component, location);

        component.setLocation(newLocation);
    }

    static public void fitComponentIntoScreen(Component component) {
        fitComponentIntoScreen(component, component.getLocation());
    }

    static public Point fitComponentInsideScreen(Dimension componentSize,
            Point location) {
        Point newLocation = new Point(location.x, location.y);

        if (newLocation.x < 0)
            newLocation.x = 0;
        if (newLocation.y < 0)
            newLocation.y = 0;

        if ((newLocation.x + componentSize.width) > SCREEN_SIZE.width)
            newLocation.x = SCREEN_SIZE.width - componentSize.width;
        if ((newLocation.y + componentSize.height) > SCREEN_SIZE.height)
            newLocation.y = SCREEN_SIZE.height - componentSize.height;

        return (newLocation);
    }

    static public Point fitComponentInsideScreen(Component component,
            Point location) {
        return (fitComponentInsideScreen(component.getSize(), location));
    }
}

 

3.总结

示例代码演示了 Java 怎么重叠组件,具体步骤参见示例代码。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: