存储在arraylist中的Java GUI
使用GUI时,如何在arraylist中添加、删除和搜索项目?我想将用户输入添加到程序arraylist中。 另外,我如何检查名为INFO.TXT的文件是否存在——如果存在——将INFO.TXT文件中的所有信息设置为TEXTAREA——请参见以下内容:存储在arraylist中的Java GUI,java,user-interface,Java,User Interface,使用GUI时,如何在arraylist中添加、删除和搜索项目?我想将用户输入添加到程序arraylist中。 另外,我如何检查名为INFO.TXT的文件是否存在——如果存在——将INFO.TXT文件中的所有信息设置为TEXTAREA——请参见以下内容: import java.awt.*; import java.awt.event.*; import java.io.BufferedWriter; import java.io.FileWriter; import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import javax.swing.*;
import java.io.File;
import java.io.*;
import static java.lang.System.out;
public class FinalMainFrame extends
JFrame {
BufferedReader br = null;
private Container pane;
private JTextField textField;
public FinalMainFrame() {
pane=getContentPane();
pane.setLayout(null);
JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu();
JMenuItem menuFileExit = new
JMenuItem();
menuFile.setText("File");
menuFileExit.setText("Exit");
menuFileExit.addActionListener
( (ActionEvent e) -> {
FinalMainFrame.this.windowClosed();
});
menuFile.add(menuFileExit);
menuBar.add(menuFile);
setTitle("Final Project...");
setJMenuBar(menuBar);
setSize(new Dimension(250, 200));
JLabel label = new JLabel("Enter Info:");
label.setFont(new Font("Serif",
Font.PLAIN, 18));
pane.add(label);
label.setLocation(0, 10);
label.setSize(100, 30);
textField = new JTextField(20);
pane.add(textField);
textField.setSize(100, 30);
textField.setLocation(120, 10);
JButton button1 = new JButton("Save");
pane.add(button1);
button1.setLocation(30, 70);
button1.setSize(150, 50);
button1.addActionListener
(
new ActionListener() {
public void
actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(
null,"You pressed: " +
e.getActionCommand() );
try{
FileReader read = new
FileReader("info.txt");
BufferedReader br = new
BufferedReader(read);
String txt = "";
String line = br.readLine();
while (line != null){
txt += line;
line = br.readLine();
out.println(line);
}
String text = textField.getText();
FileWriter stream = new
FileWriter("info.txt");
try (BufferedWriter out = new
BufferedWriter(stream)) {
out.write(text);
}
JOptionPane.showMessageDialog(
null,"You entered: " + text );
}
catch (Exception ex) {}
}
}
);
this.addWindowListener
(
new WindowAdapter() {
public void
windowClosing(WindowEvent e) {
FinalMainFrame.this.windowClosed();
}
}
);
}
protected void windowClosed() {
System.exit(0);
}
}
当谈论GUI时,您应该考虑事件。因此,在单击事件或其他事件时,将用户输入添加到您的arraylist。请发布您的代码。它将帮助你得到正确的答案,也将帮助其他将面临同样问题的人