Browse Source

feature:横向纵向居中布局

pull/2/head
guy 3 years ago
parent
commit
7ab3e061d9
  1. 4
      src/main/java/com/tptj/tool/hg/fineui/swing/FineUIEngine.java
  2. 1
      src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/CenterAdaptType.java
  3. 18
      src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalAdaptType.java
  4. 19
      src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/HorizontalType.java
  5. 1
      src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/VerticalAdaptType.java
  6. 45
      src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/BaseFineUILayout.java
  7. 43
      src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/CenterLayout.java
  8. 25
      src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/HorizontalLayout.java
  9. 24
      src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/VerticalLayout.java
  10. 138
      src/main/resources/com/tptj/tool/hg/fineui/swing/demo/layout.horizontal.js

4
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 com.tptj.tool.hg.fineui.swing.element.JPanelElement;
import javax.swing.*; import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import java.awt.event.WindowListener; 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.border.js");
// .importJs("com/tptj/tool/hg/fineui/swing/demo/layout.tape.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.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; return null;
}).run(); }).run();

1
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.Element;
import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; 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.HorizontalLayout;
import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout;

18
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.Element;
import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; 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 com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout;
import javax.swing.*; import javax.swing.*;
@ -20,10 +22,16 @@ public class HorizontalAdaptType extends AbstractLayoutType{
@Override @Override
public void layout(Element child, int idx) { public void layout(Element child, int idx) {
Box box = new Box(BoxLayout.X_AXIS); int w = child.getRectStyleAttr("width", BaseFineUILayout.MAX_SIZE, BaseFineUILayout.NOT_EXIST);
box.add(Box.createHorizontalGlue()); // 如果设置了宽度就需要居中,否则和vertical布局一致
box.add( child.getWrapper() ); if (w > 0) {
box.add(Box.createHorizontalGlue()); JComponent wrapComponent = new JPanel();
element.getComponent().add(box); 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());
}
} }
} }

19
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; 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.HorizontalLayout;
import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -11,18 +12,14 @@ import java.awt.*;
* @version 10.0 * @version 10.0
* Created by 秃破天际 on 2021/11/1 * Created by 秃破天际 on 2021/11/1
**/ **/
public class HorizontalType extends AbstractLayoutType{ public class HorizontalType extends CenterAdaptType {
@Override @Override
public LayoutManager init() { public LayoutManager init() {
return new HorizontalLayout(element, FlowLayout.LEFT, 0, 0); wrapComponent = new JPanel();
} wrapComponent.setOpaque(false);
wrapComponent.setLayout(new HorizontalLayout(element, FlowLayout.LEFT, 0, 0));
@Override element.getComponent().add(wrapComponent);
public void layout(Element child, int idx) { return new VerticalLayout(element, VerticalFlowLayout.TOP, 0, 0, true);
Box box = new Box(BoxLayout.Y_AXIS);
box.add(child.getWrapper());
box.add(Box.createVerticalGlue());
element.getComponent().add(box);
} }
} }

1
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; 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.VerticalFlowLayout;
import com.tptj.tool.hg.fineui.swing.element.layout.impl.HorizontalLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.HorizontalLayout;
import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout; import com.tptj.tool.hg.fineui.swing.element.layout.impl.VerticalLayout;

45
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.AbstractElement;
import com.tptj.tool.hg.fineui.swing.element.Element; import com.tptj.tool.hg.fineui.swing.element.Element;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.List; import java.util.List;
@ -71,20 +72,7 @@ public abstract class BaseFineUILayout implements LayoutManager2 {
p_height = rect.y + rect.height; p_height = rect.y + rect.height;
} }
} }
int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); return BaseFineUILayout.preferredLayoutSize(element, new Dimension(p_width, p_height));
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;
} }
@Override @Override
@ -105,5 +93,34 @@ public abstract class BaseFineUILayout implements LayoutManager2 {
protected abstract Rectangle getFineUIRect(Element child, int width, int height); 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));
}
} }

43
src/main/java/com/tptj/tool/hg/fineui/swing/element/layout/impl/CenterLayout.java

@ -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);
}
}

25
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; 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.Element;
import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout;
import java.awt.*; import java.awt.*;
import java.util.List;
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;
public class HorizontalLayout extends FlowLayout { public class HorizontalLayout extends FlowLayout {
private Element element; private Element element;
@ -19,24 +17,15 @@ public class HorizontalLayout extends FlowLayout {
@Override @Override
public Dimension preferredLayoutSize(Container parent) { public Dimension preferredLayoutSize(Container parent) {
Dimension dim = super.preferredLayoutSize(parent); Dimension dim = super.preferredLayoutSize(parent);
int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); return BaseFineUILayout.preferredLayoutSize(element, dim);
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;
} }
@Override @Override
public void layoutContainer(Container parent) { public void layoutContainer(Container parent) {
super.layoutContainer(parent); super.layoutContainer(parent);
List<AbstractElement> children = element.children();
for (Element child : children) {
BaseFineUILayout.optimiseElement(child);
}
} }
} }

24
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; 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.Element;
import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout; import com.tptj.tool.hg.fineui.swing.element.VerticalFlowLayout;
import java.awt.*; import java.awt.*;
import java.util.List;
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;
public class VerticalLayout extends VerticalFlowLayout { public class VerticalLayout extends VerticalFlowLayout {
private Element element; private Element element;
@ -19,24 +18,15 @@ public class VerticalLayout extends VerticalFlowLayout {
@Override @Override
public Dimension preferredLayoutSize(Container parent) { public Dimension preferredLayoutSize(Container parent) {
Dimension dim = super.preferredLayoutSize(parent); Dimension dim = super.preferredLayoutSize(parent);
int w = element.getRectStyleAttr("width", MAX_SIZE, NOT_EXIST); return BaseFineUILayout.preferredLayoutSize(element, dim);
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;
} }
@Override @Override
public void layoutContainer(Container parent) { public void layoutContainer(Container parent) {
super.layoutContainer(parent); super.layoutContainer(parent);
List<AbstractElement> children = element.children();
for (Element child : children) {
BaseFineUILayout.optimiseElement(child);
}
} }
} }

138
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
});
})();
Loading…
Cancel
Save