是否可以在Java中构建通用形式,以便为每个域类在不同数量的字段上插入不同类型的数据?并且,如果可能,该应用程序的运行速度是否比为每个域类创建单独的表单还要快?
谢谢
最佳答案
简短的答案:有可能。
但是,这是一个设计决策,需要仔细考虑,在决定要动态还是手动构建表单时,需要考虑许多因素。
您需要创建多少个表格,以后所需的表格数量会增加吗?
如果是这样,那么您可能会考虑开发DSL(特定于域的语言),这将使您可以轻松地设计表单,并拥有一个知道如何读取DSL并相应地创建表单的类。
如果表单数量有限并且您只需要创建一次(以后将不再添加表单),那么您可以考虑手动进行开发(如果比编写可动态创建的代码快,那当然可以编写代码)根据某些参数动态创建表单需要更多的思考)。
我将为您提供一个我上面所说的非常简单的示例。这是我开发的一种方法示例,该方法可根据您选择的年份和月份动态创建日历,并显示可单击的按钮,并让您选择checkbox
,该LeaveItem
指定您选择的日期是否为“有偿离开还是不离开。注意:这只是构造日历的方法,而不是具有其他功能的整个表单。但是这部分是动态构建的,因此我仅将其包含在内。
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
。这是类的代码。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();
}
}