[英]Java GUI storing in an arraylist
How do I add,remove and search for items in an arraylist when using GUI?使用 GUI 时如何添加、删除和搜索数组列表中的项目? I would like to add user input into the programs arraylist.我想将用户输入添加到程序数组列表中。 Also how do I CHECK TO SEE IF A FILE CALLED INFO.TXT EXISTS -- IF IT DOES -- SET ALL INFO IN info.txt FILE INTO TEXTAREA -- See below:另外我如何检查一个名为 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.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);
}
}
When talking about GUI you should think of events.在谈论 GUI 时,您应该想到事件。 So add user input to your arraylist on a click event or etc.因此,在单击事件等时将用户输入添加到您的数组列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.