我编写了这个非常快速的程序来使用Map 的字段来读取和写入字段数据。该地图由组件和元数据组成,关于如何布局以及其信息是否可导出。
还远未完成,但只需一点文件 IO,只要传入的数据与应用程序解析逻辑中定义的“模式”匹配,就可以存储和检索信息并将其发送到表单。
图1
点击“编辑”按钮后的窗口。
图2
将所有字段的数据导出为文本后输出;使用“添加”按钮。
代码
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.JTextComponent;
public class MusicApp extends JPanel {
private static final long serialVersionUID = 6555747177061710030L;
private static final String APP_TITLE = "Music App";
private static final int APP_WIDTH = 800;
private static final int APP_HEIGHT = 600;
private static class GridItem {
private JComponent component;
private boolean isExportable;
private int xPos;
private int yPos;
private int colSpan;
private int rowSpan;
public GridItem(JComponent component, boolean isExportable, int xPos, int yPos) {
this(component, isExportable, xPos, yPos, 1, 1);
}
public GridItem(JComponent component, boolean isExportable, int xPos, int yPos, int colSpan, int rowSpan) {
this.component = component;
this.isExportable = isExportable;
this.xPos = xPos;
this.yPos = yPos;
this.colSpan = colSpan;
this.rowSpan = rowSpan;
}
}
private int appWidth;
private int appHeight;
private Map<String, GridItem> componentMap;
private GridBagLayout layout;
private GridBagConstraints constraints;
public MusicApp(int width, int height) {
super();
this.appWidth = width;
this.appHeight = height;
this.init();
this.createChildren();
}
protected void init() {
this.constraints = new GridBagConstraints();
this.layout = new GridBagLayout();
this.componentMap = new LinkedHashMap<String, GridItem>();
// Disable size for now.
//this.setPreferredSize(new Dimension(appWidth, appHeight));
this.setLayout(this.layout);
this.constraints.ipadx = 3;
this.constraints.ipady = 3;
this.constraints.insets = new Insets(8, 4, 8, 4);
//JLabel itemLabel, descriptionLabel, artistLabel, albumLabel, priceLabel;
//JTextField itemCode, description, artist, album, price;
//JButton addButton,editButton, deleteButton;
this.constraints.anchor = GridBagConstraints.LAST_LINE_END;
componentMap.put("itemLabel", new GridItem(new JLabel("Item"), false, 0, 0, 3, 1));
this.constraints.fill = GridBagConstraints.HORIZONTAL;
componentMap.put("artistLabel", new GridItem(new JLabel("Artist"), false, 0, 1));
componentMap.put("artistText", new GridItem(new JTextField(), true, 1, 1, 2, 1));
componentMap.put("albumLabel", new GridItem(new JLabel("Album"), false, 0, 2));
componentMap.put("albumText", new GridItem(new JTextField(), true, 1, 2, 2, 1));
componentMap.put("priceLabel", new GridItem(new JLabel("Price"), false, 0, 3));
componentMap.put("priceText", new GridItem(new JTextField(), true, 1, 3, 2, 1));
componentMap.put("descriptionLabel", new GridItem(new JLabel("Description"), false, 0, 4));
componentMap.put("descriptionText", new GridItem(new JTextField(20), true, 1, 4, 2, 1));
componentMap.put("addButton", new GridItem(new JButton("Add"), false, 0, 5));
componentMap.put("editButton", new GridItem(new JButton("Edit"), false, 1, 5));
componentMap.put("deleteButton", new GridItem(new JButton("Delete"), false, 2, 5));
((JButton) componentMap.get("addButton").component).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(grabFieldData());
}
});;
((JButton) componentMap.get("editButton").component).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String[] lines = {
"artistText: Led Zeppelin",
"albumText: Houses of the Holy",
"priceText: 12.99",
"descriptionText: The fifth studio album by British rock band Led Zeppelin, released by Atlantic Records on 28 March 1973."
};
setFieldData(lines);
}
});;
((JButton) componentMap.get("deleteButton").component).addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clearFieldData();
}
});;
}
protected void createChildren() {
Iterator<Map.Entry<String, GridItem>> it;
for (it = componentMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, GridItem> item = it.next();
GridItem gridItem = item.getValue();
this.constraints.gridx = gridItem.xPos;
this.constraints.gridy = gridItem.yPos;
this.constraints.gridwidth = gridItem.colSpan;
this.constraints.gridheight = gridItem.rowSpan;
this.add(gridItem.component, this.constraints);
}
}
private String grabFieldData() {
StringBuffer buff = new StringBuffer();
Iterator<Map.Entry<String, GridItem>> it;
for (it = componentMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, GridItem> item = it.next();
GridItem gridItem = item.getValue();
if (gridItem.isExportable) {
if (gridItem.component instanceof JTextComponent) {
buff.append(item.getKey()).append(": ")
.append(((JTextComponent) gridItem.component).getText())
.append("\n");
}
}
}
return buff.toString();
}
private void clearFieldData() {
Iterator<Map.Entry<String, GridItem>> it;
for (it = componentMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, GridItem> item = it.next();
GridItem gridItem = item.getValue();
if (gridItem.isExportable) {
if (gridItem.component instanceof JTextComponent) {
((JTextComponent) gridItem.component).setText("");
}
}
}
}
private void setFieldData(String[] textLines) {
clearFieldData();
for (String line : textLines) {
String[] values = line.split(":\\s*");
if (values.length == 2) {
GridItem gridItem = componentMap.get(values[0]);
if (gridItem.isExportable && gridItem.component instanceof JTextComponent) {
JTextComponent field = ((JTextComponent) gridItem.component);
field.setText(values[1]);
field.setCaretPosition(0);
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame(APP_TITLE);
f.setContentPane(new MusicApp(APP_WIDTH, APP_HEIGHT));
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
});
}
}