import java.awt.*;
import java.awt.event.*;
public class Mytest_35 extend WindowAdapter
implements ActionListener.TextListener
{
Framme f;
TextArea ta1;//多行文本框
Panel p1;
TextField tf1;//文本框
Button b1,b2,b3;
FileDialog fd;//文件对话框
File file1=null;//文件对象初始化为空值
public static void main(String[] args)
{
(new Mytest_35()).display();//主方法调用自定义的display()方法
}
public void display()//display()方法
{
//设置界面与监听
f = new Frame("EditFile");
f.setSize(680,400);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.addWindowListener(this);
tf1=new TextFiled();//文本框
tf1.setEnabled(false);
//设置文本行的初始字体
tf1.setFont(new Font("Dialog",0,20));
f.add(ta1);
//注册文本区的事件监听程序
ta1.addTexListener(this);
p1 =new Panel();//面板与布局
p1.setLayout(new FlowLayout(FloeLayout.LEFT));
b1=new Button("Open");
b2=new Button("Save");
b3=new Button("Save As");
p1.add(b1);
p1.add(b2);
p1.add(b3);
b2.setEnabled(false);
b3.setEnabled(false);
//注册按钮的事件监听程序
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
f.add(p1,"South");//面板贴上窗体
f.setVisible(ture);
}
//display()方法结束
//实现TestListener接口的方法。
//当对多行对文本区的内容编辑时触发
public void testValueChanged(TextEvent e)
{
//"保存"和"另存为"两个按钮点亮
b2.setEnabled(ture);
b3.setEnabled(ture);
}
//响应单击按钮的高级事件
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1)//单击[打开]按钮时
{
fd =new FileDialog(f,"Open",FileDialog.LOAD);
fd.setVisible(ture);//创建并显示打开文件对话框
if ((fd.getDirectory()!=null)
&& (fd.getFile()!=null))
{
//单行文本框显示文件路径名
tf1.setText(fd.getDirctory()+fd.getFile());
try //以缓冲区方式读取文件内容
{
file1 = new //文件对象赋值
File(fd.getDirectory(),fd.getFile());
//文本读入通道连向文件对象
FileReader fr = new FileReader(file1);
BufferedReader br= new BufferedReader (fr);//定义文件缓冲区
String aline;//按行读取文本,每行附加在多行文本区后
while ((aline=br.readLine())!=null)
ta1.append(aline+"\r\n");
fr.close();
br.close();//关闭文件缓冲区
}//输入输出异常捕获
catch (IOExcption ioe)
{
System.out.println(ioe);
}
} //结束if文件不为空
}//结束if单击[打开]按钮时
//单击[Save]或[SaveAs]按钮时
if ((e.getSource()==b2||(e.getSource()==b3))
{
//单击[SaveAs]按钮时,
// 或单击[Save]按钮且文件对象为空时
if((e.getSource()==b3) ||(e.getSource()==b2)&&(file==null))
{
//文件对话框对象,保存方式
fd = new
FileDialog(f,"Save",FileDislog.SAVE);
if(file1=null)
fd.setFile("Edit1.txt");//缺省文件名
else
fd.setFile(file1.getName());
fd.setVisible(true);
//创建并显示保存文件对话框
if ((fd.getDirctory()!=null)&&(fd.getFile!=null))
{
//单行文本框中显示文件路径和名称
tf1.setText(fd.getDirectory()+fd.getFile());
//文件对象的赋值
file1 = new
File(fd.getDirectory(),fd.getFile());
save(file1);
}
}
else //文件对象不为空时
save(file1);
}
}//响应单击按钮的高级事件结束
//自定义的save方法,参数为文件对象
public void save(File file1)
{
try //将文本区内容写入字符输出流
{
//文件写入通道连同文件对象
FileWriter fw = new FileWriter(file1);
fw.write(ta1.getText());//写入多行文本框的内容
fw.close();//关闭通道
b2.setEnabled(false);//点灭保存按钮
b3.setEnabled(false);
}
}
catch (IOExcption ioe)
{
//处理异常
System.out.println(ioe);
}
}
//响应关闭窗口事件
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
import java.awt.event.*;
public class Mytest_35 extend WindowAdapter
implements ActionListener.TextListener
{
Framme f;
TextArea ta1;//多行文本框
Panel p1;
TextField tf1;//文本框
Button b1,b2,b3;
FileDialog fd;//文件对话框
File file1=null;//文件对象初始化为空值
public static void main(String[] args)
{
(new Mytest_35()).display();//主方法调用自定义的display()方法
}
public void display()//display()方法
{
//设置界面与监听
f = new Frame("EditFile");
f.setSize(680,400);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.addWindowListener(this);
tf1=new TextFiled();//文本框
tf1.setEnabled(false);
//设置文本行的初始字体
tf1.setFont(new Font("Dialog",0,20));
f.add(ta1);
//注册文本区的事件监听程序
ta1.addTexListener(this);
p1 =new Panel();//面板与布局
p1.setLayout(new FlowLayout(FloeLayout.LEFT));
b1=new Button("Open");
b2=new Button("Save");
b3=new Button("Save As");
p1.add(b1);
p1.add(b2);
p1.add(b3);
b2.setEnabled(false);
b3.setEnabled(false);
//注册按钮的事件监听程序
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
f.add(p1,"South");//面板贴上窗体
f.setVisible(ture);
}
//display()方法结束
//实现TestListener接口的方法。
//当对多行对文本区的内容编辑时触发
public void testValueChanged(TextEvent e)
{
//"保存"和"另存为"两个按钮点亮
b2.setEnabled(ture);
b3.setEnabled(ture);
}
//响应单击按钮的高级事件
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1)//单击[打开]按钮时
{
fd =new FileDialog(f,"Open",FileDialog.LOAD);
fd.setVisible(ture);//创建并显示打开文件对话框
if ((fd.getDirectory()!=null)
&& (fd.getFile()!=null))
{
//单行文本框显示文件路径名
tf1.setText(fd.getDirctory()+fd.getFile());
try //以缓冲区方式读取文件内容
{
file1 = new //文件对象赋值
File(fd.getDirectory(),fd.getFile());
//文本读入通道连向文件对象
FileReader fr = new FileReader(file1);
BufferedReader br= new BufferedReader (fr);//定义文件缓冲区
String aline;//按行读取文本,每行附加在多行文本区后
while ((aline=br.readLine())!=null)
ta1.append(aline+"\r\n");
fr.close();
br.close();//关闭文件缓冲区
}//输入输出异常捕获
catch (IOExcption ioe)
{
System.out.println(ioe);
}
} //结束if文件不为空
}//结束if单击[打开]按钮时
//单击[Save]或[SaveAs]按钮时
if ((e.getSource()==b2||(e.getSource()==b3))
{
//单击[SaveAs]按钮时,
// 或单击[Save]按钮且文件对象为空时
if((e.getSource()==b3) ||(e.getSource()==b2)&&(file==null))
{
//文件对话框对象,保存方式
fd = new
FileDialog(f,"Save",FileDislog.SAVE);
if(file1=null)
fd.setFile("Edit1.txt");//缺省文件名
else
fd.setFile(file1.getName());
fd.setVisible(true);
//创建并显示保存文件对话框
if ((fd.getDirctory()!=null)&&(fd.getFile!=null))
{
//单行文本框中显示文件路径和名称
tf1.setText(fd.getDirectory()+fd.getFile());
//文件对象的赋值
file1 = new
File(fd.getDirectory(),fd.getFile());
save(file1);
}
}
else //文件对象不为空时
save(file1);
}
}//响应单击按钮的高级事件结束
//自定义的save方法,参数为文件对象
public void save(File file1)
{
try //将文本区内容写入字符输出流
{
//文件写入通道连同文件对象
FileWriter fw = new FileWriter(file1);
fw.write(ta1.getText());//写入多行文本框的内容
fw.close();//关闭通道
b2.setEnabled(false);//点灭保存按钮
b3.setEnabled(false);
}
}
catch (IOExcption ioe)
{
//处理异常
System.out.println(ioe);
}
}
//响应关闭窗口事件
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
解决方案 »
- 高分求问:平板电脑能用来做java的编程开发能用吗,如微软的win8平板。【悬赏100分】
- java如何解析指向xml的url
- spring 3.0.6和mybatis 3.1.1集成Mapped Statements collection does not contai
- 问关于x=x.replace('\r',' ');
- 有关JTable的问题
- Eclipse+Tomcat4.1,启动时出现问题!
- 请问谁有用JAVA读取硬盘和网卡序列号的源程序,分不够另加!!
- 高手们:帮我看看这是怎么回事吧。调用EJB。
- spring中NoClassDefFoundError怎么破
- 求各位大神,怎么把这xml解释后,按属性param_name名,一条一条存进数据库?最好有例子
- 加入log4j之后,不能编译JAVA源文件了。
- JSTL 中调用自定义类的方法?
import java.awt.Button;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;public class Mytest_35 extends WindowAdapter implements ActionListener,
TextListener {
Frame f;
TextArea ta1;
Panel p1;
TextField tf1;
Button b1, b2, b3;
FileDialog fd;
File file1 = null; public static void main(String[] args) {
(new Mytest_35()).display();
} public void display() {
f = new Frame("EditFile");
f.setSize(680, 400);
f.setLocation(200, 140);
f.setBackground(Color.lightGray);
f.addWindowListener(this);
tf1 = new TextField();
tf1.setEnabled(false);
// 设置文本行的初始字体
tf1.setFont(new Font("Dialog", 0, 20));
ta1 = new TextArea();
f.add(ta1);
// 注册文本区的事件监听程序
ta1.addTextListener(this);
p1 = new Panel();// 面板与布局
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
b1 = new Button("Open");
b2 = new Button("Save");
b3 = new Button("Save As");
p1.add(b1);
p1.add(b2);
p1.add(b3);
b2.setEnabled(false);
b3.setEnabled(false);
// 注册按钮的事件监听程序
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
f.add(p1, "South");// 面板贴上窗体
f.setVisible(true);
} // display()方法结束
// 实现TestListener接口的方法。
// 当对多行对文本区的内容编辑时触发
public void testValueChanged(TextEvent e) {
// "保存"和"另存为"两个按钮点亮
b2.setEnabled(true);
b3.setEnabled(true);
} // 响应单击按钮的高级事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
fd = new FileDialog(f, "Open", FileDialog.LOAD);
fd.setVisible(true);
if ((fd.getDirectory() != null) && (fd.getFile() != null)) { tf1.setText(fd.getDirectory() + fd.getFile());
try {
file1 = new File(fd.getDirectory(), fd.getFile());
// 文本读入通道连向文件对象
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);// 定义文件缓冲区
String aline;// 按行读取文本,每行附加在多行文本区后
while ((aline = br.readLine()) != null)
ta1.append(aline + "\r\n");
fr.close();
br.close();// 关闭文件缓冲区
}// 输入输出异常捕获
catch (IOException ioe) {
System.out.println(ioe);
}
} // 结束if文件不为空 }
// 单击[Save]或[SaveAs]按钮时
if (e.getSource() == b2 || (e.getSource() == b3)) {
// 单击[SaveAs]按钮时,
// 或单击[Save]按钮且文件对象为空时
if ((e.getSource() == b3) || (e.getSource() == b2)
&& (file1 == null)) {
// 文件对话框对象,保存方式
fd = new FileDialog(f, "Save", FileDialog.SAVE);
if (file1 == null)
fd.setFile("Edit1.txt");// 缺省文件名
else
fd.setFile(file1.getName());
fd.setVisible(true);
// 创建并显示保存文件对话框
if ((fd.getDirectory() != null) && (fd.getFile() != null)) {
// 单行文本框中显示文件路径和名称
tf1.setText(fd.getDirectory() + fd.getFile());
// 文件对象的赋值
file1 = new File(fd.getDirectory(), fd.getFile());
save(file1);
}
} else
// 文件对象不为空时
save(file1);
}
} // 自定义的save方法,参数为文件对象
public void save(File file1) {
try {
FileWriter fw = new FileWriter(file1);
fw.write(ta1.getText());// 写入多行文本框的内容
fw.close();// 关闭通道
b2.setEnabled(false);// 点灭保存按钮
b3.setEnabled(false);
} catch (IOException ioe) {
System.out.println(ioe);
}
} // 响应关闭窗口事件
public void windowClosing(WindowEvent e) {
System.exit(0);
} @Override
public void textValueChanged(TextEvent arg0) {
b2.setEnabled(true);
b3.setEnabled(true);
}
}