java - 是否可以用 Java 构建通用表单

标签 java forms performance generics

是否可以在 Java 中构建通用表单,以便在每个域类的不同数量的字段上插入不同类型的数据?如果可能的话,应用程序的运行速度是否会比为每个域类创建单独的表单更快? 谢谢

最佳答案

简短回答:有可能。 然而,这是一个需要仔细考虑的设计决策,在决定是动态还是手动构建表单时,您需要考虑许多因素。

您需要创建多少个表单,所需的表单数量稍后会增加吗? 如果这是真的,那么您可能会考虑开发一种 DSL(领域特定语言),它可以让您轻松设计表单,并拥有一个知道如何读取 DSL 并相应地创建表单的类。

如果表单数量有限并且您只需要创建一次(以后不会添加表单),那么您可能会考虑手动开发它们(如果比编写动态创建表单的代码更快,当然编写根据某些参数动态创建表单的代码需要更多思考)。

我将为您提供一个非常简单的示例来说明我上面的意思。下面是我开发的一个方法示例,用于根据您选择的年份和月份动态创建日历,并显示可以单击的按钮,并让您选择一个复选框,该复选框指定您选择的日期是否为“带薪”休假。 注意:这只是构建日历的方法,而不是具有其他功能的整个表单。但这部分是动态构造的,所以我只附上这个。

private void constructCalendar()
{
    int index = 1;
    int day = 1;
    int columns = ((GridLayout) daysPanel.getLayout()).getColumns();
    int rows = ((GridLayout) daysPanel.getLayout()).getRows();
    int selectedYear = Integer.parseInt(year.getValue().toString());
    int month = monthsList.getSelectedIndex();
    GregorianCalendar gc = new GregorianCalendar(selectedYear, month, 1);

    String[] days = select.getWeekDays();

    for (int i = 1; i <= columns; i++)
    {
        JLabel lab = new JLabel(days[i - 1]);
        lab.setHorizontalAlignment(SwingConstants.CENTER);
        lab.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        lab.setFont(new Font("Cambria", Font.BOLD, 14));
        if (i == 6 || i == 7)
        {
            lab.setForeground(Color.red);
        }
        daysPanel.add(lab);
    }
    for (int i = columns; i >= 2; i--)
    {
        for (int j = 1; j <= rows; j++)
        {
            if (index >= (gc.get(GregorianCalendar.DAY_OF_WEEK) + 5) % 7 + 1 && day <= gc.getActualMaximum(GregorianCalendar.DAY_OF_MONTH))
            {
                daysPanel.add(new LeaveItem("" + day, "Paid", addList, removeList, year.getValue().toString(), "" + (month + 1), leaveDays));
                day++;
            } else
            {
                daysPanel.add(new JLabel());
            }
            index++;
        }
    }

}

在每个框(代表一天)中添加一个 LeaveItem。这是 LeaveItem 类的代码。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;
import java.util.HashMap;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JToggleButton;


public class LeaveItem extends JPanel implements ActionListener
{

    private JToggleButton button;
    private JCheckBox isPaid;
    private HashMap<String, Integer> addList;
    private HashMap<String, Integer> removeList;
    private HashMap<String, Integer> leaveDays;
    private String year;
    private String month;
    private boolean insertIntoAddList = true;
    private String key = "";

    public LeaveItem(String buttonText, String checkBoxText, HashMap<String, Integer> addList, HashMap<String, Integer> removeList,
            String year, String month, HashMap<String, Integer> leaveDays)
    {
        this.addList = addList;
        this.removeList = removeList;
        this.leaveDays = leaveDays;
        this.year = year;
        this.month = (month.length() == 1 ? "0" : "") + month;
        button = new JToggleButton(buttonText);
        GregorianCalendar gc = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(buttonText));
        if (gc.get(GregorianCalendar.DAY_OF_WEEK) == GregorianCalendar.SUNDAY
                || gc.get(GregorianCalendar.DAY_OF_WEEK) == GregorianCalendar.SATURDAY)
        {
            Font currentFont = button.getFont();
            button.setFont(new Font(currentFont.getName(), Font.BOLD, currentFont.getSize()));
            button.setForeground(Color.red);
        }
        button.setBackground(Color.white);
        isPaid = new JCheckBox(checkBoxText);
        button.setFocusPainted(false);
        isPaid.setBackground(new Color(255, 255, 200));
        this.setLayout(new BorderLayout());
        this.add(button, BorderLayout.CENTER);
        isPaid.setSelected(true);
        isPaid.addActionListener(this);
        button.addActionListener(this);
        this.setBorder(BorderFactory.createEtchedBorder());
        this.setVisible(true);
        key = this.year + "-" + this.month + "-" + ((button.getText().length() == 1 ? "0" : "") + button.getText());
        Integer tempLeaveDay = this.leaveDays.get(key);
        if (tempLeaveDay != null)
        {
            isPaid.setSelected(tempLeaveDay == 1 ? true : false);
            insertIntoAddList = false;
            button.doClick();
            insertIntoAddList = true;
        } else if (this.addList.containsKey(key))
        {
            isPaid.setSelected(this.addList.get(key) == 1 ? true : false);
            button.doClick();
        }
    }

    public JToggleButton getButton()
    {
        return this.button;
    }

    public JCheckBox getCheckBox()
    {
        return this.isPaid;
    }

    public boolean isPaid()
    {
        return this.isPaid.isSelected();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        if (source instanceof JToggleButton)
        {
            if (button.isSelected())
            {
                this.add(isPaid, BorderLayout.SOUTH);
                removeList.remove(key);
                if (insertIntoAddList)
                {
                    addList.put(key, isPaid.isSelected() ? 1 : 0);
                }
            } else
            {
                this.remove(isPaid);
                addList.remove(key);
                removeList.put(key, isPaid.isSelected() ? 1 : 0);
            }
        } else if (source instanceof JCheckBox)
        {
            //if (addList.containsKey(key))
            {
                addList.put(key, isPaid.isSelected() ? 1 : 0);
            }
        }
        this.validate();
    }
}

关于java - 是否可以用 Java 构建通用表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35345297/

相关文章:

html - 将跨度视为输入元素

Java 8 数组列表。哪个更快?在索引 0 处插入一项或使用一项创建新列表并将All 添加到新列表?

performance - 为什么 diff(!=,<>) 比 equal(=,==) 更快?

c# - Enumerable.Range 与 for 循环的性能

java - 在Java中打印文本文件的确切内容

forms - IE10 内嵌框阴影缩放与边框半径

java - 如何在Dom解析器中修改XML数据

html - 文本表单框 HTML 背景图像

java - 日期在美国服务器中返回错误的年份

java - Spring ScheduledTasks 未触发