您现在的位置:计算机技术学习网 > 技术中心 > WEB编程 > JSP >

用按钮调用jsp代码怎么写?

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 15:21 点击:

   //导入JAVA类包
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

class EditFile
{
//默认构造函数
EditFile(){}
//保存文件函数
void SaveFile(String name,String str)
{
try
{
//声明文件输出对象
FileOutputStream fos = new FileOutputStream(name);
//输出保存文件
fos.write(str.getBytes());
}
catch (IOException e)
{
//发生错误打印错误信息
System.out.println(e.toString());
}
}
//打开文件函数
String OpenFile(String name)
{
//定义字符串str来保存文件内容
String str=null;
//定义计数器
int ch=0;
try
{
//声明文件输入对象
FileInputStream fis = new FileInputStream(name);
//遍历输入对象,将内存存储到str中
while ((ch=fis.read())!=-1)
{
str=str+(char)ch;
}
//关闭文件对象
fis.close();
}
catch (IOException e)
{
//发生错误打印错误信息
System.out.println(e.toString());
}
//返回str
return str;
}
}

class MyMainFrame extends JFrame implements ActionListener
{
//文件名
String filename=null;
//查找字符串
String findtxt=null;
//替换字符串
String replacetxt=null;
//查找开始位
int findtxton=-1;
//查找结束位
int findtxtoff=-1;
//中间面版
Container c=getContentPane();
//菜单
JMenuBar mainMb=new JMenuBar();
//菜单按钮
JMenu fileMenu=new JMenu("文件");
JMenu editMenu=new JMenu("编辑");
JMenu typeMenu=new JMenu("格式");
JMenuItem newItem=new JMenuItem("新建");
JMenuItem openItem=new JMenuItem("打开");
JMenuItem saveItem=new JMenuItem("保存");
JMenuItem saveasItem=new JMenuItem("另存为");
JMenuItem exitItem=new JMenuItem("退出");
JMenuItem findItem=new JMenuItem("查找");
JMenuItem findnextItem=new JMenuItem("查找下一个");
JMenuItem changeItem=new JMenuItem("替换");
JCheckBoxMenuItem changelineItem=new JCheckBoxMenuItem("自动换行",false);
//文本域
JTextArea txt=new JTextArea();
JTextArea txt2=new JTextArea();
//滚动条
JScrollPane jsp(SUN企业级应用的首选);
//文件操作对象
EditFile ef=new EditFile();
//构造函数
MyMainFrame()
{
//绘制界面
c.setLayout(new BorderLayout());
jsp(SUN企业级应用的首选)= new JScrollPane(txt);
jsp(SUN企业级应用的首选).setHorizontalScrollBarPolicy(jsp(SUN企业级应用的首选).HORIZONTAL_SCROLLBAR_AS_NEEDED);
c.add(jsp(SUN企业级应用的首选),BorderLayout.CENTER);
mainMb.add(fileMenu);
fileMenu.add(newItem);
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(saveasItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
mainMb.add(editMenu);
editMenu.add(findItem);
editMenu.add(findnextItem);
editMenu.add(changeItem);
mainMb.add(typeMenu);
typeMenu.add(changelineItem);
setJMenuBar(mainMb);
//为菜单添加事件监听
newItem.addActionListener(this);
openItem.addActionListener(this);
saveItem.addActionListener(this);
saveasItem.addActionListener(this);
exitItem.addActionListener(this);
findItem.addActionListener(this);
findnextItem.addActionListener(this);
changeItem.addActionListener(this);
changelineItem.addActionListener(this);
}
//事件处理函数
public void actionPerformed(ActionEvent e)
{
//建立一个文件操作对象
EditFile ef=new EditFile();
//如果点击按钮为新建
if(e.getActionCommand()=="新建")
{
//将文本域置为空
txt.setText(null);
//文件名为空
filename=null;
}
//如果点击按钮为打开
if(e.getActionCommand()=="打开")
{
//定义一个文件窗体
FileDialog openDialog=new FileDialog(this,"打开文件...",FileDialog.LOAD);
//显示窗体
openDialog.show();
//如果返回文件不为空
if(openDialog.getFi

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • JSP与Servlet
      • 自己动手写MiniBBS系列(基本篇)之用户登录
      • JSP取当前日期
      • JDBC 入门(一)
      • 打开一个jsp页面默认查询所有数据,调用action
      • 使用JSP标签库验证用户的输入(2)完
      • WIN98/2000下的jsp服务器
      • 自定义JSP标签(tag)浅议
      • Struts学习傻瓜式入门篇
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1