diff --git a/designer-base/src/main/java/com/fr/design/layout/TableLayoutHelper.java b/designer-base/src/main/java/com/fr/design/layout/TableLayoutHelper.java index e664524026..5e31ba2f7f 100644 --- a/designer-base/src/main/java/com/fr/design/layout/TableLayoutHelper.java +++ b/designer-base/src/main/java/com/fr/design/layout/TableLayoutHelper.java @@ -1,16 +1,13 @@ package com.fr.design.layout; - +import com.fine.theme.utils.FineUIScale; import com.fr.design.constants.LayoutConstants; -import com.fr.design.gui.ibutton.UIButton; import com.fr.design.gui.ilable.UILabel; import com.fr.design.i18n.Toolkit; -import com.fr.design.utils.gui.GUICoreUtils; -import com.fr.stable.StringUtils; - -import javax.swing.*; -import java.awt.*; +import javax.swing.JPanel; +import java.awt.Component; +import java.awt.Container; public class TableLayoutHelper { @@ -18,8 +15,6 @@ public class TableLayoutHelper { public static final int FILL_LASTCOLUMN = 1; public static final int FILL_LASTROW = 2; public static final int FILL_LASTCOL_AND_ROW = 3; - private static final int FIVE = 5; - private static final int TEN = 10; private TableLayoutHelper() { } @@ -287,6 +282,8 @@ public class TableLayoutHelper { layoutRowSize[i * 2 + 1] = rowSize[i]; } + scaleIfNeed(layoutRowSize, layoutColumnSize); + layoutSize[0] = layoutColumnSize; layoutSize[1] = layoutRowSize; @@ -296,6 +293,28 @@ public class TableLayoutHelper { return resultPane; } + /** + * 缩放必要的尺寸。 + * 尺寸参数有三种,小于0有三种预定义类型,0~1之间为比例参数,大于等于1为普通px。 + * 这里只对px进行缩放处理 + * 注意:当前只对从TableLayoutHelper创建的TableLayout进行尺寸缩放。 + */ + private static void scaleIfNeed(double[] layoutRowSize, double[] layoutColumnSize) { + for (int i = 0; i < layoutRowSize.length; i++) { + double size = layoutRowSize[i]; + if (size >= 1.0) { + layoutRowSize[i] = FineUIScale.scale((float) size); + } + } + + for (int i = 0; i < layoutColumnSize.length; i++) { + double size = layoutColumnSize[i]; + if (size >= 1.0) { + layoutColumnSize[i] = FineUIScale.scale((float) size); + } + } + } + /** * 创建具有不同垂直间距的TableLayout面板 * @@ -363,73 +382,4 @@ public class TableLayoutHelper { }; return createTableLayoutPane(comp, row, column); } - - public static void main(String[] args) { - JFrame jf = new JFrame("test"); - jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); - JPanel content = (JPanel) jf.getContentPane(); - content.setLayout(new GridLayout(2, 3)); - double p = TableLayout.PREFERRED; - double f = TableLayout.FILL; - JPanel jp1 = TableLayoutHelper.createTableLayoutPane(createTestComponents("jp1"), TableLayoutHelper.FILL_NONE); - JPanel jp2 = TableLayoutHelper.createGapTableLayoutPane(createTestComponents("jp2"), - TableLayoutHelper.FILL_LASTCOL_AND_ROW, (double)2 * TEN, (double)2 * TEN); - JPanel jp3 = TableLayoutHelper.createGapTableLayoutPane(createTestComponents("jp3"), - new double[]{f, p, f, p}, new double[]{f, f}, 4, 4); - JPanel jp4 = TableLayoutHelper.createGapTableLayoutPane(createTestComponents("jp4"), - new double[]{p, FIVE * TEN, p, p, p, p}, new double[]{p, f}, new int[][]{{1, 3}, {1, 1}, {1, 1}, {1, 1}, {1, 1}}, 1, FIVE); - - JPanel jp5 = TableLayoutHelper.createDiffVGapTableLayoutPane(createTestComponents("jp5"), - new double[]{p, p, p, p, p}, new double[]{p, f}, 4, new double[]{4, 8, 14, 4}); - JPanel jp6 = new JPanel(); - jp1.setBorder(BorderFactory.createLineBorder(Color.red)); - jp2.setBorder(BorderFactory.createLineBorder(Color.red)); - jp3.setBorder(BorderFactory.createLineBorder(Color.red)); - jp4.setBorder(BorderFactory.createLineBorder(Color.red)); - jp5.setBorder(BorderFactory.createLineBorder(Color.red)); - jp6.setBorder(BorderFactory.createLineBorder(Color.red)); - - - content.add(jp1); - content.add(jp2); - content.add(jp3); - content.add(jp4); - content.add(jp5); - content.add(jp6); - - GUICoreUtils.centerWindow(jf); - jf.setSize(900, 600); - jf.setVisible(true); - } - - private static Component[][] createTestComponents(String name) { - UILabel label1 = new UILabel(name + "laaaable1"); - UILabel label2 = new UILabel(name + "lable2"); - UILabel label3 = new UILabel(name + "lable3"); - UILabel label4 = new UILabel(name + "lable4"); - UILabel label5 = new UILabel(name + "lable5"); - UIButton button1 = new UIButton(name + "button1"); - UIButton button2 = new UIButton(name + "button2"); - label1.setSize(label1.getPreferredSize()); - label1.setBorder(BorderFactory.createLineBorder(Color.blue)); - label2.setSize(label2.getPreferredSize()); - label2.setBorder(BorderFactory.createLineBorder(Color.blue)); - label3.setSize(label3.getPreferredSize()); - label3.setBorder(BorderFactory.createLineBorder(Color.blue)); - label4.setSize(label4.getPreferredSize()); - label4.setBorder(BorderFactory.createLineBorder(Color.blue)); - label5.setSize(label5.getPreferredSize()); - label5.setBorder(BorderFactory.createLineBorder(Color.blue)); - button1.setSize(button1.getPreferredSize()); - button2.setSize(button2.getPreferredSize()); - button1.setBackground(Color.darkGray); - button2.setBackground(Color.darkGray); - return new Component[][]{ - new Component[]{label1, button1}, - new Component[]{label2, null}, - new Component[]{label3}, - new Component[]{null, label4}, - new Component[]{button2, label5} - }; - } -} \ No newline at end of file +} diff --git a/designer-base/src/test/java/com/fr/design/gui/storybook/components/TablelayoutStoryBoard.java b/designer-base/src/test/java/com/fr/design/gui/storybook/components/TablelayoutStoryBoard.java new file mode 100644 index 0000000000..c99995d60d --- /dev/null +++ b/designer-base/src/test/java/com/fr/design/gui/storybook/components/TablelayoutStoryBoard.java @@ -0,0 +1,169 @@ +package com.fr.design.gui.storybook.components; + +import com.fine.swing.ui.layout.Layouts; +import com.fr.design.gui.ibutton.UIButton; +import com.fr.design.gui.ilable.UILabel; +import com.fr.design.gui.itextfield.UITextField; +import com.fr.design.gui.storybook.StoryBoard; +import com.fr.design.layout.TableLayout; +import com.fr.design.layout.TableLayoutHelper; +import org.jetbrains.annotations.NotNull; + +import javax.swing.BorderFactory; +import javax.swing.JPanel; +import java.awt.Color; +import java.awt.Component; + +import static com.fine.swing.ui.layout.Layouts.cell; +import static com.fine.swing.ui.layout.Layouts.row; +import static com.fr.design.layout.TableLayoutHelper.createDiffVGapTableLayoutPane; +import static com.fr.design.layout.TableLayoutHelper.createGapTableLayoutPane; +import static com.fr.design.layout.TableLayoutHelper.createTableLayoutPane; + +/** + * 表格布局展示 + *
+ * TableLayout 来自开源项目 + * TableLayout + * + * @author vito + * @since 11.0 + * Created on 2024/1/31 + */ +public class TablelayoutStoryBoard extends StoryBoard { + + + private static final double P = TableLayout.PREFERRED; + private static final double F = TableLayout.FILL; + + public TablelayoutStoryBoard() { + + super("Tablelayout"); + add( + cell(new UILabel("简单布局")).with(this::h3), + layout1(), + + cell(new UILabel("TableLayoutHelper 快速创建")).with(this::h3), + row( + cell(createTableLayoutPane(createTestComponents("jp1"), TableLayoutHelper.FILL_NONE)) + .with(it -> it.setBorder(BorderFactory.createLineBorder(Color.red))).weight(1), + + cell(createGapTableLayoutPane(createTestComponents("jp2"), + TableLayoutHelper.FILL_LASTCOL_AND_ROW, 20, 20)) + .with(it -> it.setBorder(BorderFactory.createLineBorder(Color.red))).weight(1), + + cell(createGapTableLayoutPane(createTestComponents("jp3"), + new double[]{F, P, F, P}, new double[]{F, F}, 4, 4)) + .with(it -> it.setBorder(BorderFactory.createLineBorder(Color.red))).weight(1) + ), + row( + cell(createGapTableLayoutPane(createTestComponents("jp4"), + new double[]{P, 50, P, P, P, P}, new double[]{P, F}, new int[][]{{1, 3}, {1, 1}, {1, 1}, {1, 1}, {1, 1}}, 1, 5)) + .with(it -> it.setBorder(BorderFactory.createLineBorder(Color.red))).weight(1), + + cell(createDiffVGapTableLayoutPane(createTestComponents("jp5"), + new double[]{P, P, P, P, P}, new double[]{P, F}, 4, new double[]{4, 8, 14, 4})) + .with(it -> it.setBorder(BorderFactory.createLineBorder(Color.red))).weight(1) + ) + + + ); + } + + + private static Component[][] createTestComponents(String name) { + UILabel label1 = new UILabel(name + "laaaable1"); + UILabel label2 = new UILabel(name + "lable2"); + UILabel label3 = new UILabel(name + "lable3"); + UILabel label4 = new UILabel(name + "lable4"); + UILabel label5 = new UILabel(name + "lable5"); + UIButton button1 = new UIButton(name + "button1"); + UIButton button2 = new UIButton(name + "button2"); + label1.setSize(label1.getPreferredSize()); + label1.setBorder(BorderFactory.createLineBorder(Color.blue)); + label2.setSize(label2.getPreferredSize()); + label2.setBorder(BorderFactory.createLineBorder(Color.blue)); + label3.setSize(label3.getPreferredSize()); + label3.setBorder(BorderFactory.createLineBorder(Color.blue)); + label4.setSize(label4.getPreferredSize()); + label4.setBorder(BorderFactory.createLineBorder(Color.blue)); + label5.setSize(label5.getPreferredSize()); + label5.setBorder(BorderFactory.createLineBorder(Color.blue)); + button1.setSize(button1.getPreferredSize()); + button2.setSize(button2.getPreferredSize()); + return new Component[][]{ + new Component[]{label1, button1}, + new Component[]{label2, null}, + new Component[]{label3}, + new Component[]{null, label4}, + new Component[]{button2, label5} + }; + } + + /** + * 图示 {@link /com/fr/design/gui/storybook/components/Preferred.gif} + */ + private Layouts.Cell layout1() { + + JPanel pane = getTableLayoutPanel(); + + // Create all controls + UILabel labelName = new UILabel("Name"); + UILabel labelAddress = new UILabel("Address"); + UILabel labelCity = new UILabel("City"); + UILabel labelState = new UILabel("State"); + UILabel labelZip = new UILabel("Zip"); + + UITextField textfieldName = new UITextField(10); + UITextField textfieldAddress = new UITextField(20); + UITextField textfieldCity = new UITextField(10); + UITextField textfieldState = new UITextField(2); + UITextField textfieldZip = new UITextField(5); + + UIButton buttonOk = new UIButton("OK"); + UIButton buttonCancel = new UIButton("Cancel"); + JPanel panelButton = new JPanel(); + panelButton.add(buttonOk); + panelButton.add(buttonCancel); + + // Add all controls + pane.add(labelName, "1, 1, 5, 1"); + pane.add(textfieldName, "1, 3, 5, 3"); + pane.add(labelAddress, "1, 5, 5, 5"); + pane.add(textfieldAddress, "1, 7, 5, 7"); + pane.add(labelCity, "1, 9"); + pane.add(textfieldCity, "1, 11"); + pane.add(labelState, "3, 9"); + pane.add(textfieldState, "3, 11"); + pane.add(labelZip, "5, 9"); + pane.add(textfieldZip, "5, 11"); + pane.add(panelButton, "1, 13, 5, 13"); + return cell(pane); + } + + @NotNull + private static JPanel getTableLayoutPanel() { + JPanel pane = new JPanel(); + // b - border + // f - FILL + // p - PREFERRED + // vs - vertical space between labels and text fields + // vg - vertical gap between form elements + // hg - horizontal gap between form elements + + double b = 10; + double f = TableLayout.FILL; + double p = TableLayout.PREFERRED; + double vs = 5; + double vg = 10; + double hg = 10; + + double[][] size = + {{b, f, hg, p, hg, p, b}, + {b, p, vs, p, vg, p, vs, p, vg, p, vs, p, vg, p, b}}; + + TableLayout layout = new TableLayout(size); + pane.setLayout(layout); + return pane; + } +} diff --git a/designer-base/src/test/resources/com/fr/design/gui/storybook/components/Preferred.gif b/designer-base/src/test/resources/com/fr/design/gui/storybook/components/Preferred.gif new file mode 100644 index 0000000000..88e9c2dfab Binary files /dev/null and b/designer-base/src/test/resources/com/fr/design/gui/storybook/components/Preferred.gif differ