JAVA编程:编写一个图形界面的Application程序

编写一个图形界面的Application程序.包括一个文本框和一个按钮,在文本框中输入若干字符串,然后将其保存在文件中.
1.越简单越好.
2.要有说明语句.

第1个回答  2008-12-06
//:SaveContents.java
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class SaveContents extends JFrame implements ActionListener{
public static void main(String[] args) {new SaveContents().setVisible(true);}
private JTextField cont;
private JButton save;
private String filePath="c:/cont.txt";//文件的路径
public SaveContents(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(null);
this.setSize(205,130);
this.setLocationRelativeTo(null);
cont = new JTextField();
cont.setBounds(10,10,180,20);
add(cont);
save=new JButton("Save");
save.setBounds(110,50,78,22);
add(save);
save.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
try {save();} catch (Exception e1) {}
}
//保存内容
private void save() throws Exception{
File f = new File(filePath);
FileWriter fw = new FileWriter(f);
fw.write(cont.getText());
fw.flush();
fw.close();
}
}
第2个回答  2008-12-06
import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

class MyWindow extends JFrame implements ActionListener{
JTextArea text; JButton button;
FileWriter writefile;BufferedWriter out;
MyWindow(){
super("缓冲式样流的输出");
Container con=this.getContentPane();
text=new JTextArea(20,30);
text.setBackground(Color.cyan);
button=new JButton("写文件");
button.addActionListener(this);
con.setLayout(new BorderLayout());
con.setSize(400,400);con.setVisible(true);
con.add(text,"Center"); con.add(button,"South");
try{
writefile =new FileWriter("line.txt");
out=new BufferedWriter(writefile);
}catch(IOException e){}
}
public void actionPerformed(ActionEvent e){
String s;
if(e.getSource()==button){
try{
out.write(text.getText(),0,(text.getText()).length());
out.flush();
text.setText(null);

}catch(IOException ex)
{
System.out.println(ex);
}
}
}
}

public class Example9_3{
public static void main(String []args){
MyWindow myWin=new MyWindow();
myWin.setVisible(true);
}
}本回答被提问者采纳
相似回答