diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/FineUIEngine.java b/src/main/java/com/tptj/tool/hg/fineui/swing/FineUIEngine.java index 291280a..69b6de9 100644 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/FineUIEngine.java +++ b/src/main/java/com/tptj/tool/hg/fineui/swing/FineUIEngine.java @@ -11,7 +11,6 @@ import com.tptj.tool.hg.fineui.swing.element.Element; import com.tptj.tool.hg.fineui.swing.element.JPanelElement; import javax.swing.*; -import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; @@ -228,7 +227,8 @@ public class FineUIEngine { // .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.border.js"); // .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.tape.js"); // .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.grid.js"); - .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.window.js"); +// .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.window.js"); + .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.horizontal.js"); return null; }).run(); diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/CenterAdaptType.java b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/CenterAdaptType.java index 4e9d4b3..9111424 100644 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/CenterAdaptType.java +++ b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/CenterAdaptType.java @@ -2,7 +2,6 @@ package com.tptj.tool.hg.fineui.swing.element.layout; import com.tptj.tool.hg.fineui.swing.element.Element; import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; -import com.tptj.tool.hg.fineui.swing.element.layout.impl.CenterLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.HorizontalLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout; diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalAdaptType.java b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalAdaptType.java index b166c37..a657dd2 100644 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalAdaptType.java +++ b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalAdaptType.java @@ -2,6 +2,8 @@ package com.tptj.tool.hg.fineui.swing.element.layout; import com.tptj.tool.hg.fineui.swing.element.Element; import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; +import com.tptj.tool.hg.fineui.swing.element.layout.impl.BaseFineUILayout; +import com.tptj.tool.hg.fineui.swing.element.layout.impl.HorizontalLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout; import javax.swing.*; @@ -12,7 +14,7 @@ import java.awt.*; * @version 10.0 * Created by 秃破天际 on 2021/11/1 **/ -public class HorizontalAdaptType extends AbstractLayoutType{ +public class HorizontalAdaptType extends AbstractLayoutType { @Override public LayoutManager init() { return new VerticalLayout(element, VerticalFlowLayout.TOP, 0, 0, true); @@ -20,10 +22,16 @@ public class HorizontalAdaptType extends AbstractLayoutType{ @Override public void layout(Element child, int idx) { - Box box = new Box(BoxLayout.X_AXIS); - box.add(Box.createHorizontalGlue()); - box.add( child.getWrapper() ); - box.add(Box.createHorizontalGlue()); - element.getComponent().add(box); + int w = child.getRectStyleAttr("width", BaseFineUILayout.MAX_SIZE, BaseFineUILayout.NOT_EXIST); + // 如果设置了宽度就需要居中,否则和vertical布局一致 + if (w > 0) { + JComponent wrapComponent = new JPanel(); + wrapComponent.setOpaque(false); + wrapComponent.setLayout(new HorizontalLayout(element, FlowLayout.CENTER, 0, 0)); + wrapComponent.add(child.getWrapper()); + element.getComponent().add(wrapComponent); + } else { + element.getComponent().add(child.getWrapper()); + } } } diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalType.java b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalType.java index a1e5bb4..f9bc93f 100644 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalType.java +++ b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalType.java @@ -1,7 +1,8 @@ package com.tptj.tool.hg.fineui.swing.element.layout; -import com.tptj.tool.hg.fineui.swing.element.Element; +import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.HorizontalLayout; +import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout; import javax.swing.*; import java.awt.*; @@ -11,18 +12,14 @@ import java.awt.*; * @version 10.0 * Created by 秃破天际 on 2021/11/1 **/ -public class HorizontalType extends AbstractLayoutType{ +public class HorizontalType extends CenterAdaptType { @Override public LayoutManager init() { - return new HorizontalLayout(element, FlowLayout.LEFT, 0, 0); - } - - @Override - public void layout(Element child, int idx) { - Box box = new Box(BoxLayout.Y_AXIS); - box.add(child.getWrapper()); - box.add(Box.createVerticalGlue()); - element.getComponent().add(box); + wrapComponent = new JPanel(); + wrapComponent.setOpaque(false); + wrapComponent.setLayout(new HorizontalLayout(element, FlowLayout.LEFT, 0, 0)); + element.getComponent().add(wrapComponent); + return new VerticalLayout(element, VerticalFlowLayout.TOP, 0, 0, true); } } diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/VerticalAdaptType.java b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/VerticalAdaptType.java index 41939d4..2c569c4 100644 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/VerticalAdaptType.java +++ b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/VerticalAdaptType.java @@ -1,6 +1,5 @@ package com.tptj.tool.hg.fineui.swing.element.layout; -import com.tptj.tool.hg.fineui.swing.element.Element; import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.HorizontalLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout; diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/BaseFineUILayout.java b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/BaseFineUILayout.java index d3739df..2092c29 100644 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/BaseFineUILayout.java +++ b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/BaseFineUILayout.java @@ -3,6 +3,7 @@ package com.tptj.tool.hg.fineui.swing.element.layout.impl; import com.tptj.tool.hg.fineui.swing.element.AbstractElement; import com.tptj.tool.hg.fineui.swing.element.Element; +import javax.swing.*; import java.awt.*; import java.util.List; @@ -71,20 +72,7 @@ public abstract class BaseFineUILayout implements LayoutManager2 { p_height = rect.y + rect.height; } } - int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); - int h = element.getRectStyleAttr("height", MAX_SIZE, NOT_EXIST); - Dimension realDim = new Dimension(w, h); - if (w == MAX_SIZE || w == NOT_EXIST) { - realDim.width = p_width; - } - if (h == MAX_SIZE || h == NOT_EXIST) { - realDim.height = p_height; - } - if (element.hasScrollbar()) { - element.getWrapper().setPreferredSize(realDim); - return new Dimension(p_width, p_height); - } - return realDim; + return BaseFineUILayout.preferredLayoutSize(element, new Dimension(p_width, p_height)); } @Override @@ -105,5 +93,34 @@ public abstract class BaseFineUILayout implements LayoutManager2 { protected abstract Rectangle getFineUIRect(Element child, int width, int height); + public static Dimension preferredLayoutSize(Element element, Dimension dimension) { + int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); + int h = element.getRectStyleAttr("height", MAX_SIZE, NOT_EXIST); + Dimension realDim = new Dimension(w, h); + if (w == MAX_SIZE || w == NOT_EXIST) { + realDim.width = dimension.width; + } + if (h == MAX_SIZE || h == NOT_EXIST) { + realDim.height = dimension.height; + } + if (element.hasScrollbar()) { +// element.getWrapper().setPreferredSize(realDim); + return dimension; + } + return realDim; + } + public static void optimiseElement(Element element) { + int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); + int h = element.getRectStyleAttr("height", MAX_SIZE, NOT_EXIST); + int ml = element.getRectStyleAttr("margin-left", MAX_SIZE, 0); + int mr = element.getRectStyleAttr("margin-right", MAX_SIZE, 0); + int mt = element.getRectStyleAttr("margin-top", MAX_SIZE, 0); + int mb = element.getRectStyleAttr("margin-bottom", MAX_SIZE, 0); + Dimension dimension = new Dimension(w, h); + if (element.hasScrollbar()) { + element.getWrapper().setPreferredSize(dimension); + } + element.getWrapper().setBorder(BorderFactory.createEmptyBorder(mt, ml, mb, mr)); + } } diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/CenterLayout.java b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/CenterLayout.java deleted file mode 100644 index 893e45a..0000000 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/CenterLayout.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.tptj.tool.hg.fineui.swing.element.layout.impl; - -import com.tptj.tool.hg.fineui.swing.element.Element; - -import javax.swing.*; -import java.awt.*; - -import static com.tptj.tool.hg.fineui.swing.element.layout.impl.BaseFineUILayout.MAX_SIZE; -import static com.tptj.tool.hg.fineui.swing.element.layout.impl.BaseFineUILayout.NOT_EXIST; - -/** - * @author 秃破天际 - * @version 10.0 - * Created by 秃破天际 on 2021/11/1 - **/ -public class CenterLayout extends BoxLayout { - protected Element element; - - public CenterLayout(Element target, int axis) { - super(target.getComponent(), axis); - element = target; - } - - @Override - public Dimension preferredLayoutSize(Container parent) { - Dimension dim = super.preferredLayoutSize(parent); - int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); - int h = element.getRectStyleAttr("height", MAX_SIZE, NOT_EXIST); - Dimension realDim = new Dimension(w, h); - if (w == MAX_SIZE || w == NOT_EXIST) { - realDim.width = dim.width; - } - if (h == MAX_SIZE || h == NOT_EXIST) { - realDim.height = dim.height; - } - return realDim; - } - - @Override - public void layoutContainer(Container parent) { - super.layoutContainer(parent); - } -} diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/HorizontalLayout.java b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/HorizontalLayout.java index fc05311..f2228d9 100644 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/HorizontalLayout.java +++ b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/HorizontalLayout.java @@ -1,12 +1,10 @@ package com.tptj.tool.hg.fineui.swing.element.layout.impl; +import com.tptj.tool.hg.fineui.swing.element.AbstractElement; import com.tptj.tool.hg.fineui.swing.element.Element; -import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; import java.awt.*; - -import static com.tptj.tool.hg.fineui.swing.element.layout.impl.BaseFineUILayout.MAX_SIZE; -import static com.tptj.tool.hg.fineui.swing.element.layout.impl.BaseFineUILayout.NOT_EXIST; +import java.util.List; public class HorizontalLayout extends FlowLayout { private Element element; @@ -19,24 +17,15 @@ public class HorizontalLayout extends FlowLayout { @Override public Dimension preferredLayoutSize(Container parent) { Dimension dim = super.preferredLayoutSize(parent); - int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); - int h = element.getRectStyleAttr("height", MAX_SIZE, NOT_EXIST); - Dimension realDim = new Dimension(w, h); - if (w == MAX_SIZE || w == NOT_EXIST) { - realDim.width = dim.width; - } - if (h == MAX_SIZE || h == NOT_EXIST) { - realDim.height = dim.height; - } - if (element.hasScrollbar()) { - element.getWrapper().setPreferredSize(realDim); - return dim; - } - return realDim; + return BaseFineUILayout.preferredLayoutSize(element, dim); } @Override public void layoutContainer(Container parent) { super.layoutContainer(parent); + List children = element.children(); + for (Element child : children) { + BaseFineUILayout.optimiseElement(child); + } } } diff --git a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/VerticalLayout.java b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/VerticalLayout.java index 3163eb5..f4c5d93 100644 --- a/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/VerticalLayout.java +++ b/src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/VerticalLayout.java @@ -1,12 +1,11 @@ package com.tptj.tool.hg.fineui.swing.element.layout.impl; +import com.tptj.tool.hg.fineui.swing.element.AbstractElement; import com.tptj.tool.hg.fineui.swing.element.Element; import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; import java.awt.*; - -import static com.tptj.tool.hg.fineui.swing.element.layout.impl.BaseFineUILayout.MAX_SIZE; -import static com.tptj.tool.hg.fineui.swing.element.layout.impl.BaseFineUILayout.NOT_EXIST; +import java.util.List; public class VerticalLayout extends VerticalFlowLayout { private Element element; @@ -19,24 +18,15 @@ public class VerticalLayout extends VerticalFlowLayout { @Override public Dimension preferredLayoutSize(Container parent) { Dimension dim = super.preferredLayoutSize(parent); - int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); - int h = element.getRectStyleAttr("height", MAX_SIZE, NOT_EXIST); - Dimension realDim = new Dimension(w, h); - if (w == MAX_SIZE || w == NOT_EXIST) { - realDim.width = dim.width; - } - if (h == MAX_SIZE || h == NOT_EXIST) { - realDim.height = dim.height; - } - if (element.hasScrollbar()) { - element.getWrapper().setPreferredSize(realDim); - return dim; - } - return realDim; + return BaseFineUILayout.preferredLayoutSize(element, dim); } @Override public void layoutContainer(Container parent) { super.layoutContainer(parent); + List children = element.children(); + for (Element child : children) { + BaseFineUILayout.optimiseElement(child); + } } } diff --git a/src/main/resources/com/tptj/tool/hg/fineui/swing/demo/layout.horizontal.js b/src/main/resources/com/tptj/tool/hg/fineui/swing/demo/layout.horizontal.js new file mode 100644 index 0000000..ddba842 --- /dev/null +++ b/src/main/resources/com/tptj/tool/hg/fineui/swing/demo/layout.horizontal.js @@ -0,0 +1,138 @@ +!(function () { + + var V = BI.inherit(BI.Widget, { + render: function () { + return { + type: "bi.vertical_adapt", + columnSize: [300, "fill"], + hgap: 30, + scrollable: true, + css: { + background: "rgb(200,200,200)" + }, + items: [{ + type: "bi.label", + text: "垂直居中布局" + }, { + type: "bi.label", + text: "垂直居中布局" + }] + } + } + }); + BI.shortcut("my.vertical_adapt", V); + var H = BI.inherit(BI.Widget, { + render: function () { + return { + type: "bi.horizontal", + columnSize: [300, 300, 300, 300, 300, 300, 300, 300], + // scrollx: false, + hgap: 30, + css: { + background: "rgb(150,150,200)" + }, + items: [{ + type: "bi.label", + text: "水平布局" + }, { + type: "bi.label", + text: "水平布局" + }, { + type: "bi.label", + text: "水平布局" + }, { + type: "bi.label", + text: "水平布局" + }, { + type: "bi.label", + text: "水平布局" + }, { + type: "bi.label", + text: "水平布局" + }, { + type: "bi.label", + text: "水平布局" + }, { + type: "bi.label", + text: "水平布局" + }, { + type: "bi.label", + text: "水平布局" + }] + } + } + }); + BI.shortcut("my.horizontal", H); + var C = BI.inherit(BI.Widget, { + render: function () { + return { + type: "bi.center_adapt", + hgap: 30, + css: { + background: "rgb(150,150,150)" + }, + items: [{ + type: "bi.label", + text: "水平垂直居中布局" + }, { + type: "bi.label", + text: "水平垂直居中布局" + }, { + type: "bi.label", + text: "水平垂直居中布局" + }] + } + } + }); + BI.shortcut("my.center_adapt", C); + + var Widget = BI.inherit(BI.Widget, { + props: { + width: 600, + height: 300 + }, + render: function () { + return { + type: "bi.vertical", + vgap: 20, + hgap: 20, + horizontalAlign: "center", + items: [{ + type: "my.vertical_adapt", + height: 100 + }, { + type: "my.horizontal", + width: 400, + height: 100 + }, { + type: "my.center_adapt", + height: 100 + }, { + type: "my.vertical_adapt", + height: 100 + }, { + type: "my.horizontal", + height: 100 + }, { + type: "my.center_adapt", + height: 100 + }, { + type: "my.vertical_adapt", + height: 100 + }, { + type: "my.horizontal", + height: 100 + }, { + type: "my.center_adapt", + height: 100 + }] + } + } + }); + BI.shortcut("my.app", Widget); + + BI.createWidget({ + type: "my.app", + element: body + }); +})(); \ No newline at end of file