⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingdesigner.java

📁 SWING的界面UI包 SWING的界面UI包
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        int type = location.getCursor();

        if (type != getCursor().getType()) {
            //设置当前形状
            setCursor(Cursor.getPredefinedCursor(type));
        }
    }

    private void initRootComponent() {
        try {
            BeanInfo panelInfo = Introspector.getBeanInfo(JPanel.class);
            JPanel panel = (JPanel) Util.instantiateBean(this, panelInfo, "root");
            panel.setSize(400, 300);
            setRootComponent(panel);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    //是否是添加模式

    public boolean isAddingMode() {
        return addingMode;
    }

    public void setAddingMode(boolean addingMode) {
        this.addingMode = addingMode;
    }

    public int getLeftOffset() {
        Insets insets = getOutlineInsets();
        return leftOffset + insets.left;
    }

    public int getOuterLeft() {
        return leftOffset + getOuterInsets().left;
    }

    public int getTopOffset() {
        Insets insets = getOutlineInsets();
        return topOffset + insets.top;
    }

    public int getOuterTop() {
        return topOffset + getOuterInsets().top;
    }

    public void setLeftOffset(int left) {
        leftOffset = left;
    }

    public void setTopOffset(int top) {
        topOffset = top;
    }

    //各种属性的setter和getter

    public Painter getPainter() {
        return painter;
    }

    public void setPainter(Painter p) {
        painter = p;
    }

    public Component getRootComponent() {
        return rootComponent;
    }

    public boolean isRootComponent(Component comp) {
        return comp == rootComponent;
    }

    Insets getOutlineInsets() {
        return getOutlineBorder().getBorderInsets(this);
    }

    Insets getOuterInsets() {
        return getOuterBorder().getBorderInsets(this);
    }

    //计算鼠标事件e所发生的位置相对根组件的位置关系

    public Location getLoc2Root(MouseEvent e) {
        int x = e.getX() - leftOffset;
        int y = e.getY() - topOffset;
        int width = rootComponent.getWidth();
        int height = rootComponent.getHeight();

        Insets insets = getOutlineInsets();
        if (x < width) {
            if ((y >= height) && (y <= (height + insets.bottom))) {
                return Location.bottom;
            } else {
                return Location.outer;
            }
        } else if (x <= (width + insets.right)) {
            if ((y >= 0) && (y < height)) {
                return Location.right;
            } else if ((y >= height) && (y <= (height + insets.bottom))) {
                return Location.right_bottom;
            } else {
                return Location.outer;
            }
        } else {
            return Location.outer;
        }
    }

    public void setRootComponent(Component component) {
        this.rootComponent = component;
        Util.addContainerNotify((Container) component);
        Util.initComponentAdapter(this, (Container) component);
        Util.layoutContainer((Container) rootComponent);
        TopContainerAdapter rootContainerAdapter = (TopContainerAdapter) AdapterBus.getComponentAdapter(this, component);
        inner_border = rootContainerAdapter.getBorder();
        outline_border = inner_border == null ? areaBorder : BorderFactory.createCompoundBorder(areaBorder, inner_border);
        selectionModel.reset();
        invalidateLayout();
        setInvalidated(true);
    }

    public StateModel getStateModel() {
        return stateModel;
    }

    public AddingModel getAddingModel() {
        return addingModel;
    }

    //获取e所在的组件

    public Component getComponentAt(MouseEvent e) {
        int x = e.getX() - getOuterLeft();
        int y = e.getY() - getOuterTop();
        return componentAt(x, y, rootComponent);
    }

    public Component getComponentAt(int x, int y) {
        return componentAt(x - getOuterLeft(), y - getOuterTop(), rootComponent);
    }

    public SelectionModel getSelectionModel() {
        return selectionModel;
    }

    private void fireComponentDragged(ArrayList<Component> dragged) {
        DesignerEvent evt = new DesignerEvent(this);
        evt.setDraggedComponents(dragged);
        getEditListenerTable().fireComponentMoved(evt);
    }

    private void fireStartDesigning() {
        DesignerEvent evt = new DesignerEvent(this);
        getStateListenerTable().fireStartDesigning(evt);
    }

    private void fireStopDesigning() {
        DesignerEvent evt = new DesignerEvent(this);
        getStateListenerTable().fireStopDesigning(evt);
    }

    public void startDraggingComponent(BeanInfoToggleButton tb) {
        BeanInfo beanInfo = tb.getBeanInfo();
        //根据所选择的组件的BeanInfo生成相应的AddingModel
        //AddingModel和StateModel不一样,适合当前选择的组件相关的
        addingModel = new AddingModel(this, beanInfo);
        addingMouseListener = new AddingMouseListener(this, beanInfo);
        this.setDropTarget(addingMouseListener);
        //触发状态添加模式事件
        fireStartDesigning();
        repaint();
    }

    public void startDraggingComponent(Component comp, int x, int y) {
        //根据所选择的组件的BeanInfo生成相应的AddingModel
        //AddingModel和StateModel不一样,适合当前选择的组件相关的
        addingModel = new AddingModel(comp, x, y);
        addingMouseListener = new AddingMouseListener(this, addingModel.getBeanInfo());
        Component parent = Util.findDesigningParent(comp);
        selectionModel.removeComponent(comp);
        selectionModel.setSelectedComponent(parent);
        this.setDropTarget(addingMouseListener);
        //触发状态添加模式事件
        fireStartDesigning();
        setInvalidated(true);
        repaint();
    }

    public void valueChanged(TreeSelectionEvent e) {
        ComponentTree tree = (ComponentTree) e.getSource();
        TreePath[] paths = tree.getSelectionPaths();

        if (paths != null) {
            ArrayList<Component> selected = new ArrayList<Component>();

            for (TreePath path : paths) {
                selected.add((Component) path.getLastPathComponent());
            }

            selectionModel.setSelectedComponents(selected);

            TreePath path = e.getNewLeadSelectionPath();
            Component comp = (Component) path.getLastPathComponent();

            if (!Util.isComponentVisible(comp) && !isRootComponent(comp)) {
                makeVisible(comp);
            } else {
                Component parent = comp;
                while (!(parent instanceof JInternalFrame || parent == null)) {
                    parent = parent.getParent();
                }
                if (parent != null) {
                    ((JInternalFrame) parent).toFront();
                }
            }
        }
    }

    private void invalidateLayout() {
        Container parent = this.getParent();
        if (parent != null) {
            parent.doLayout();
            parent.repaint();
        }
    }

    private void makeVisible(Component comp) {
        Component parent = Util.getFirstInvisibleParent(comp);
        if (isRootComponent(parent)) {
            parent = comp;
            while (!(parent instanceof JInternalFrame || parent == null)) {
                parent = parent.getParent();
            }
            if (parent != null) {
                JInternalFrame jif = (JInternalFrame) parent;
                jif.toFront();
            }
            return;
        }
        while (parent != null) {
            Container parentContainer = parent.getParent();

            if (parentContainer == null) {
                //parent.setVisible(true);
                break;
            } else {
                ComponentAdapter adapter = AdapterBus.getComponentAdapter(this, parentContainer);

                if (adapter instanceof ContainerAdapter) {
                    ContainerAdapter containerAdapter = (ContainerAdapter) adapter;
                    containerAdapter.showComponent(parent);
                } else {
                    parent.setVisible(true);
                }

                parent = Util.getFirstInvisibleParent(parent);
            }
        }
    }

    public void addDesignerStateListener(DesignerStateListener listener) {
        getStateListenerTable().addListener(listener);
    }

    public void addDesignerEditListener(DesignerEditListener listener) {
        getEditListenerTable().addListener(listener);
    }

    public void treeChanged(ComponentTreeEvent evt) {
        Util.layoutContainer((Container) getRootComponent());
        this.repaint();
    }

    public void refreshDesignerUI() {
        Util.layoutContainer((Container) getRootComponent());
        repaint();
    }

    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 10;
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        return 100;
    }

    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }

    public Action[] getActions() {
        if (designer_actions == null) {
            initializeActions();
        }
        return designer_actions;
    }

    private void initializeActions() {
        designer_actions = new Action[]{new EditingAction(this), new ViewAction(this, palette), new AlignmentAction(this), new SameSizeAction(this)};
        initActionListener(designer_actions);
    }

    Border getOutlineBorder() {
        return outline_border;
    }

    Border getOuterBorder() {
        return areaBorder;
    }

    Rectangle getOutlineBounds() {
        Insets insets = getOuterBorder().getBorderInsets(this);
        int w = rootComponent.getWidth() + insets.left + insets.right;
        int h = rootComponent.getHeight() + insets.top + insets.bottom;
        return new Rectangle(leftOffset, topOffset, w, h);
    }

    Rectangle getContentBounds() {
        Insets insets = getOuterInsets();
        int x = leftOffset + insets.left;
        int y = topOffset + insets.top;
        int w = rootComponent.getWidth();
        int h = rootComponent.getHeight();
        if (rootComponent instanceof RootPaneContainer) {
            if (inner_border != null) {
                insets = inner_border.getBorderInsets(rootComponent);
                x += insets.left;
                y += insets.top;
            }
            Component component = ((RootPaneContainer) rootComponent).getContentPane();
            w = component.getWidth();
            h = component.getHeight();
        }
        return new Rectangle(x, y, w, h);
    }

    public LookAndFeel getDesignLnf() {
        return designLnf;
    }

    public void setDesignLnf(LookAndFeel designLnf) {
        this.designLnf = designLnf;
        setInvalidated(true);
    }

    public boolean isInvalidated() {
        return invalidated;
    }

    public void setInvalidated(boolean invalidated) {
        this.invalidated = invalidated;
    }

    public Image getDesigerBuffer() {
        if (buffer == null) {
            int w = getWidth() > BUFFER_WIDTH ? getWidth() : BUFFER_WIDTH;
            int h = getHeight() > BUFFER_HEIGHT ? getHeight() : BUFFER_HEIGHT;
            buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        } else if (buffer.getWidth() < getWidth() ||
                buffer.getHeight() < getHeight()) {
            buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
        }
        return buffer;
    }
    private static int BUFFER_WIDTH = 1024;
    private static int BUFFER_HEIGHT = 1024;
    private BufferedImage buffer;

    public LookAndFeel getIdeLnf() {
        return ideLnf;
    }

    public void setIdeLnf(LookAndFeel ideLnf) {
        this.ideLnf = ideLnf;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -