java中如何把窗体的文本框中输入的内容保存到.txt文件中去,不要用数据库。文件创建好后,不知怎么按钮连接。
package test1;
import java.io.*;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddCandidate {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args)throws IOException {
String fileName = "Hello.txt";
FileWriter writer = new FileWriter(fileName);
writer.write ("01\n");
writer.write ("月月");
writer.close();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddCandidate window = new AddCandidate();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public AddCandidate() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
void initialize() {
frame = new JFrame("输入选手数据");
frame.setVisible(true);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel lblNewLabel = new JLabel("请输入选手的编号");
JLabel lblNewLabel_1 = new JLabel("请输入选手姓名");
textField = new JTextField();
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
JButton btnNewButton = new JButton("确认");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("添加选手数据成功!");
}
});
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(64)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE))
.addContainerGap(261, Short.MAX_VALUE))
.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
.addContainerGap(268, Short.MAX_VALUE)
.addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
.addGap(71))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(30)
.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(14)
.addComponent(lblNewLabel_1, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
.addGap(26)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(btnNewButton)
.addContainerGap(36, Short.MAX_VALUE))
);
frame.getContentPane().setLayout(groupLayout);
}
}
你从工具箱里拖个按钮出来,双击,直接给你生成好按钮的回调事件函数
然后你在函数里写读取文本框,写入文件的代码
但是我需要保存这个数据,因为后面得查询它并且打分,如果直接在文本框里写文字,应该就不能在文件里查询了啊,仅仅是一个文本框。(不好意思,我没讲清楚)