我必须创建一个GUI来获取多个文本字段的输入,当用户单击一个按钮时,它会将所有输入添加到文本文件中。所有文本字段都与mp3文件的所有部分有关。 (艺术家/专辑等)然后,一旦将mp3文件添加到文本文件中,我就必须创建一个按钮来编辑或删除它们。我对如何为多个文本字段制作“添加”按钮感到困惑。以下是我到目前为止的情况:
public musicLib()
{
setLayout(new FlowLayout());
// Song Row
itemLabel = new JLabel("Item Code: ");
add(itemLabel);
itemCode = new JTextField(10);
add(itemCode);
descriptionLabel = new JLabel("Description: ");
add(descriptionLabel);
description = new JTextField(10);
add(description);
artistLabel = new JLabel("Artist: ");
add(artistLabel);
artist = new JTextField(10);
add(artist);
albumLabel = new JLabel("Album: ");
add(albumLabel);
album = new JTextField(10);
add(album);
// Buttons
addButton = new JButton("Add");
add(addButton);
Event e = new Event(); error"no suitable constructor found"
addButton.addActionListener(e); // error"incompatible types"
addButton.addActionListener(new ActionListener()
{
String nl = "\n",
data = "";
public void dataWriter(String data, String fileName) throws IOException {
File file = getFileStreamPath(fileName); // error"cannot find symbol"
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream writer = openFileOutput(file.getName(), Context.MODE_PRIVATE); // error"cannot find symbol"
for (String string: data) // error"for- each not applicable to expression type"
{
writer.write(string.getBytes());
writer.flush();
}
writer.close();
}
}
非常感谢你们的帮助。我只需要一些帮助就可以通过一个按钮将多个文本字段输入添加到文本文件中。提前谢谢!
答案 0 :(得分:2)
这个块,将成为初始gui设置功能的一部分:
\b(?:0|1|2|3|99) ([^0|1|2|3|99].*?) (?:0|1|2|3|4|5)\b
这是你的文件编写者:
/**
* On click listener for the add button
*/
addButton.addActionListener(new ActionListener() {
String nl = "\n",
data = "";
public void actionPerformed(ActionEvent ae){
data += "Label: " + albumLabel.getText() + nl;
data += "Artist: " + albumArtist.getText() + nl;
/*
* Repeat the same for any other text fields you have
*/
dataWriter(data, "test.txt");
}
});
答案 1 :(得分:1)
如果我正确理解你,你想要那样的东西;
StringBuilder sb = new StringBuilder();
sb.append("ItemLabel: "+itemLabel.getText()+"\n");
sb.append("Description: "+description.getText()+"\n");
并添加您需要的内容。当您写入文件
时out.write(sb.toString());
答案 2 :(得分:1)
我写了这个非常快速的程序来使用Map
字段读取和写入字段数据。该地图包含有关如何布局以及其信息是否可输出的组件和元数据。
它还远未完成,但只要输入数据与应用程序解析逻辑中定义的“模式”相匹配,只需一个小文件IO,就可以存储和检索信息并将其发送到表单。
单击“编辑”按钮后的窗口。
将所有字段的数据导出到文本后的输出;使用“添加”按钮。
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);
}
});
}
}