Java程序留言代码.docx

上传人:b****6 文档编号:6325775 上传时间:2023-01-05 格式:DOCX 页数:13 大小:17.66KB
下载 相关 举报
Java程序留言代码.docx_第1页
第1页 / 共13页
Java程序留言代码.docx_第2页
第2页 / 共13页
Java程序留言代码.docx_第3页
第3页 / 共13页
Java程序留言代码.docx_第4页
第4页 / 共13页
Java程序留言代码.docx_第5页
第5页 / 共13页
点击查看更多>>
下载资源
资源描述

Java程序留言代码.docx

《Java程序留言代码.docx》由会员分享,可在线阅读,更多相关《Java程序留言代码.docx(13页珍藏版)》请在冰豆网上搜索。

Java程序留言代码.docx

Java程序留言代码

/*

实验七,留言板和闹钟的设置

2、定义其中submit与display按钮的事件处理。

当submit按钮被点击后,弹出提示Dialog对象要求用户确认提交的信息;

当display按钮被点击后,在新建的Frame窗口中逐条显示留言记录。

Dialog窗口与Frame窗口可实现关闭功能。

*/

importjava.io.*;importjava.util.*;

importjava.applet.*;importjava.awt.*;importjava.awt.event.*;

classLMessage

{

privateStringauthor,title,content;

//构造方法

publicLMessage(Stringauthor1,Stringtitle1,Stringcontent1)

{

author=author1;

title=title1;

content=content1;

}

publicLMessage()

{

author="";

title="";

content="";

}

publicvoidsetContent(Stringc)

{

content=c;

}

publicStringgetContent()

{

returncontent;

}

publicvoidsetTitle(Stringt)

{

title=t;

}

publicStringgetTitle()

{

returntitle;

}

publicvoidsetAuthor(Stringa)

{

author=a;

}

publicStringgetAuthor()

{

returnauthor;

}

当我们创建一个类的时候,如果我们没有创建一个构造方法(哪怕是无参数的)。

JVM会帮助我们创建一个默认的构造方法,也称为无参构造方法。

但是如果我们创建一个构造方法时,JVM就不会再为我们生成一个默认的构造方法。

有时有必要的显示的写出这个默认构造函数,也就是无参构造函数。

所以当这个类被继承时,子类就会默认的调用这个无参构造方法。

如果你想显示的调用父类的构造方法,可以使用super()在子类的构造方法中的第一句。

(构造方法中只能调用构造方法一次)。

如果你在父类中没有写无参构造方法,那么在子类的构造方法中必须显示的调用父类中的有参数的构造方法(写在第一句)。

//进行留言

publicvoidsetMessage(Filef)throwsIOException

{

Stringtemp;

RandomAccessFilerAF=newRandomAccessFile(f,"rw");

rAF.seek(rAF.length());

rAF.write(author.getBytes());

rAF.write(title.getBytes());

rAF.write(content.getBytes());

}

//获取留言的内容

publicString[]getMessage(Filef)throwsIOException

{

Stringtemp="";

Stringatc[]=newString[6];

RandomAccessFilerAF=newRandomAccessFile(f,"r");

rAF.seek(0);

intp=0;

while((temp=rAF.readLine())!

=null)

{

//可以解决(中文字符)乱码的问题

temp=newString(temp.getBytes("ISO_8859_1"),"gb2312");

atc[p]=temp;

p++;

}

returnatc;

}

}

//创建dialog对话框

classMyFrameextendsFrame

{

MyFrame(Stringtitle)

{

super(title);

setLayout(newFlowLayout());

addWindowListener(newMyWindowAdapter(this));

}

//内嵌类可以直接存取其所在类中的私用成员.(这个类只为Myframe所使用,所以将其定义为内嵌类)

classMyWindowAdapterextendsWindowAdapter

{

MyFramemFrame;

publicMyWindowAdapter(MyFramef)

{

mFrame=f;

}

//Frame对象的setVisible(false)方法或dispose()方法来将窗口从屏幕中移除。

publicvoidwindowClosing(WindowEventwe)

{

mFrame.setVisible(false);

mFrame.dispose();

}

}

}

classMyDialogextendsDialog

{

MyDialog(Framef,Stringtitle,booleanb)

{

super(f,title,b);

setLayout(newFlowLayout());

addWindowListener(newWindowAdapter()

{

publicvoidwindowClosing(WindowEvente)

{

setVisible(false);

dispose();

}

}

);

}

}

publicclassMessage_7extendsAppletimplementsActionListener

{

Stringatc1[]=newString[6];//用来接收文件传回来的数据信息

Stringinput[]=newString[3];//存储用户信息

LMessagemessage,message0;

Filef=newFile("d:

/实验七/message.txt");

privateLabelL_head,L_author,L_title,L_content;

privateTextFieldT_author,T_title;//主窗口中的两个单行文本框

privateTextAreaA_content;//主窗口中的多行文本框

privateButtonsubmit,reset,display;//主窗口中的三个按钮

privatePanelp12,p21,p22,p31,p32,p41,p42,p43;

privateMyFrameF_content;//子窗口对象

privateTextAreaA_content1;//子窗口中的多行文本框

privateButtonDL_submit,DL_cancel;//对话框中的两个按钮

privateMyDialogD_logon;//对话框对象

Labell_logintime;

Timert;

MyTaskmt;

Panelp51;

Strings;

publicvoidinit()

{

setLayout(newGridLayout(5,3));

setBackground(Color.cyan);

L_head=newLabel("留言板");

L_author=newLabel("作者:

");

L_title=newLabel("标题:

");

L_content=newLabel("内容:

");

T_author=newTextField(30);

T_title=newTextField(30);

A_content=newTextArea(4,10);

submit=newButton("submit");

reset=newButton("reset");

display=newButton("display");

p12=newPanel();

p12.setFont(newFont("宋体",Font.BOLD,28));

p12.setForeground(Color.red);

p12.setLayout(newGridLayout(1,3));

p12.add(newLabel(""));

p12.add(L_head);

p12.add(newLabel(""));

p21=newPanel();

p21.setFont(newFont("宋体",Font.BOLD,15));

p21.setLayout(newGridLayout(1,3));

p21.add(newLabel(""));

p21.add(newLabel(""));

p21.add(L_author);

p22=newPanel();

p22.setFont(newFont("宋体",Font.BOLD,20));

p22.setLayout(newGridLayout(3,3));

p22.add(newLabel(""));

p22.add(newLabel(""));

p22.add(newLabel(""));

p22.add(T_author);

p22.add(newLabel(""));

p22.add(newLabel(""));

p22.add(newLabel(""));

p22.add(newLabel(""));

p22.add(newLabel(""));

p31=newPanel();

p31.setFont(newFont("宋体",Font.BOLD,20));

p31.setLayout(newGridLayout(1,3));

p31.add(newLabel(""));

p31.add(newLabel(""));

p31.add(L_title);

p32=newPanel();

p32.setFont(newFont("宋体",Font.BOLD,20));

p32.setLayout(newGridLayout(3,3));

p32.add(newLabel(""));

p32.add(newLabel(""));

p32.add(newLabel(""));

p32.add(T_title);

p32.add(newLabel(""));

p32.add(newLabel(""));

p32.add(newLabel(""));

p32.add(newLabel(""));

p32.add(newLabel(""));

p41=newPanel();

p41.setFont(newFont("宋体",Font.BOLD,20));

p41.setLayout(newGridLayout(3,3));

p41.add(newLabel(""));

p41.add(newLabel(""));

p41.add(L_content);

p41.add(newLabel(""));

p41.add(newLabel(""));

p41.add(newLabel(""));

p41.add(newLabel(""));

p41.add(newLabel(""));

p41.add(newLabel(""));

p42=newPanel();

p42.setFont(newFont("宋体",Font.BOLD,20));

p42.setLayout(newGridLayout(1,2));

p42.add(A_content);

p42.add(newLabel(""));

p43=newPanel();

p43.setFont(newFont("宋体",Font.BOLD,20));

p43.setLayout(newGridLayout(3,3));

p43.add(submit);

p43.add(newLabel(""));

p43.add(newLabel(""));

p43.add(reset);

p43.add(newLabel(""));

p43.add(newLabel(""));

p43.add(display);

p43.add(newLabel(""));

p43.add(newLabel(""));

add(newLabel(""));

add(p12);

add(newLabel(""));

add(p21);

add(p22);

add(newLabel(""));

add(p31);

add(p32);

add(newLabel(""));

add(p41);

add(p42);

add(p43);

DL_submit=newButton("确定");

DL_cancel=newButton("取消");

l_logintime=newLabel("您已使用系统0天0时0分0秒");

t=newTimer();

mt=newMyTask();

t.schedule(mt,1000,1000);

p51=newPanel();

p51.setFont(newFont("宋体",Font.BOLD,20));

p51.setForeground(Color.red);

p51.setLayout(newGridLayout(1,3));

add(p51);

p51.add(l_logintime);

submit.addActionListener(this);

reset.addActionListener(this);

display.addActionListener(this);

DL_submit.addActionListener(this);

DL_cancel.addActionListener(this);

}

/*

TimerTask类是一个抽象类,该类实现了runnable接口

它的run()方法表示定时器需要定时完成的任务

*/

//内嵌类

classMyTaskextendsTimerTask

{

intt_day=0,t_hour=0,t_min=0,t_sec=0;

publicvoidrun()

{

t_sec++;

getTime();

s="您已使用本系统"+t_day+"天"+t_hour+"时"+t_min+"分"+t_sec+"秒";

l_logintime.setText(s);

}

publicvoidgetTime()

{

if(t_hour>24)

{

t_hour=0;

t_day++;

}

if(t_min>60)

{

t_min=0;

t_hour++;

}

if(t_sec>60)

{

t_sec=0;

t_min++;

}

}

}

publicvoidactionPerformed(ActionEvente)

{

try

{

if(!

f.exists())

{f.createNewFile();}

/*点击reset按钮的时候,重置文本框中的内容*/

if(e.getSource()==reset)

{

System.out.println("reset");

T_author.setText("");

T_title.setText("");

A_content.setText("");

}

/*点击submit按钮的时候出现dialog对话框*/

if(e.getSource()==submit)

{

System.out.println("submit11");

D_logon=newMyDialog(newFrame(),"确认",true);

D_logon.add(newLabel("确定提交留言内容吗?

"));

D_logon.add(DL_submit);

D_logon.add(DL_cancel);

D_logon.setSize(200,100);

D_logon.setVisible(true);

}

/*对话框中的取消按钮*/

if(e.getSource()==DL_submit)

{

intr=0;

System.out.println("DL_submit");

input[0]="&"+T_author.getText();

input[1]="&"+T_title.getText();

input[2]="&"+A_content.getText();

message=newLMessage(input[0],input[1],input[2]);

message.setMessage(f);

D_logon.setVisible(false);

D_logon.dispose();

}

/*点击DL_cancel按钮时,不提交内容,对话框消失*/

if(e.getSource()==DL_cancel)

{

System.out.println("DL_cancel");

D_logon.setVisible(false);

D_logon.dispose();

}

/*子窗口显示文件中的用户留言*/

if(e.getSource()==display)

{

System.out.println("display");

F_content=newMyFrame("留言板");

F_content.setTitle("111");

F_content.setBackground(Color.cyan);

A_content1=newTextArea(10,30);

F_content.add(newLabel("用户留言:

"));

F_content.add(A_content1);

F_content.setSize(800,600);

F_content.setVisible(true);

message0=newLMessage();

message0.setMessage(f);

atc1=message0.getMessage(f);

for(intn=0;n

{

if(atc1[n]!

=null)

{

StringTokenizerst=newStringTokenizer(atc1[n],"&");

while(st.hasMoreTokens())

{

A_content1.setText(A_content1.getText()+st.nextToken()+"\n");

}

}

continue;

}

}

}

catch(Exceptionie)

{

System.out.print(ie);

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 表格模板 > 合同协议

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1