JAVA怎样实现输入到JTextField中数据的保存 20
展开全部
1、获取文本框中的文本
2、将内容写入文件内
具体示例如下所示:
public static void main(String [] args){
// getDatesByYearAndMonth(2015,2);
final JFrame frame = new JFrame("保存文本示例");
JButton button = new JButton("保存到C:/text.txt");
final JTextField text = new JTextField();
frame.add(text,BorderLayout.CENTER);
frame.add(button,BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String s = text.getText();//获取文本框中内容
FileWriter out;
try {
out = new FileWriter("C:/text.txt");
out.write(s);//将文本内容保存到文件中
out.close();
JOptionPane.showMessageDialog(frame, "C:/text.txt保存完毕");
} catch (IOException e1) {
e1.printStackTrace();
}
}});
frame.setSize(400, 62);
frame.setVisible(true);
}
2、将内容写入文件内
具体示例如下所示:
public static void main(String [] args){
// getDatesByYearAndMonth(2015,2);
final JFrame frame = new JFrame("保存文本示例");
JButton button = new JButton("保存到C:/text.txt");
final JTextField text = new JTextField();
frame.add(text,BorderLayout.CENTER);
frame.add(button,BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String s = text.getText();//获取文本框中内容
FileWriter out;
try {
out = new FileWriter("C:/text.txt");
out.write(s);//将文本内容保存到文件中
out.close();
JOptionPane.showMessageDialog(frame, "C:/text.txt保存完毕");
} catch (IOException e1) {
e1.printStackTrace();
}
}});
frame.setSize(400, 62);
frame.setVisible(true);
}
2016-06-27
展开全部
importjava.awt.event.*;importjavax.swing.*;publicclassjavextendsJFrameimplementsActionListener{JTextFieldtxt1=newJTextField(18);JButtonbt=newJButton("确定");JPanelpan=newJPanel();publicjav(){this.setTitle("JFrame");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setBounds(100,100,220,180);this.add(pan);pan.add(txt1);pan.add(bt);this.setVisible(true);bt.addActionListener(this);}publicvoidactionPerformed(ActionEvente){Strings;s=txt1.getText();System.out.println(s);}publicstaticvoidmain(String[]args){newjav();}}//看看这个代码符不符合要求
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener{
JTextField txt = new JTextField(20);
JButton but=new JButton("保存");
public Test(){
setLayout(new FlowLayout());
add(txt);
add(but);
but.addActionListener(this);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==but){
FileWriter fw =null;
try{
fw=new FileWriter("D:/ss.txt");
fw.write(txt.getText());
fw.flush();
fw.close();
JOptionPane.showMessageDialog(this, "保存成功");
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Test extends JFrame implements ActionListener{
JTextField txt = new JTextField(20);
JButton but=new JButton("保存");
public Test(){
setLayout(new FlowLayout());
add(txt);
add(but);
but.addActionListener(this);
setSize(300,300);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==but){
FileWriter fw =null;
try{
fw=new FileWriter("D:/ss.txt");
fw.write(txt.getText());
fw.flush();
fw.close();
JOptionPane.showMessageDialog(this, "保存成功");
}catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
追问
谢谢,还有请问怎样将注册信息添加到数据库中并通过用户名密码进行登录
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询