《Java程序留言代码》由会员分享,可在线阅读,更多相关《Java程序留言代码(10页珍藏版)》请在金锄头文库上搜索。
1、/* 实验七,留言板和闹钟的设置2、定义其中 submit 与 display 按钮的事件处理。当 submit 按钮被点击后,弹出提示 Dialog 对象要求用户确认提交的信息;当 display 按钮被点击后,在新建的 Frame 窗口中逐条显示留言记录。Dialog 窗口与 Frame 窗口可实现关闭功能。*/import java.io.*; import java.util.*; import java.applet.*; import java.awt.*; import java.awt.event.*; class LMessage private String author,
2、 title, content;public LMessage()author=“; title=“; content=“;/构造方法构造方法 public LMessage(String author1,String title1,String content1) author=author1; title=title1; content=content1;public void setAuthor(String a)author=a;public String getAuthor()return author;public void setTitle(String t)title=t;pu
3、blic String getTitle()return title;public void setContent(String c)content=c;public String getContent()return content;当我们创建一个类的时候,如果我们没有创建一个当我们创建一个类的时候,如果我们没有创建一个构造方法构造方法(哪怕是无参数的)(哪怕是无参数的) 。JVMJVM 会帮会帮 助我们创建一个默认的助我们创建一个默认的构造方法构造方法,也称为无参,也称为无参构造方法构造方法。但是如果我们创建一个构造方法。但是如果我们创建一个构造方法 时,时,JVMJVM 就不会再为我们生
4、成一个默认的构造方法。有时有必要的显示的写出这个就不会再为我们生成一个默认的构造方法。有时有必要的显示的写出这个默认构默认构 造函数造函数,也就是无参,也就是无参构造函数构造函数。所以当这个类被继承时,子类就会默认的调用这个无参构。所以当这个类被继承时,子类就会默认的调用这个无参构 造方法。造方法。 如果你想显示的调用父类的构造方法,可以使用如果你想显示的调用父类的构造方法,可以使用 supersuper()在子类的构造方法中的第一句。()在子类的构造方法中的第一句。 (构造方法中只能调用构造方法一次)(构造方法中只能调用构造方法一次) 。如果你在父类中没有写无参构造方法,那么在子。如果你在父
5、类中没有写无参构造方法,那么在子 类的构造方法中必须显示的调用父类中的有参数的构造方法(写在第一句)类的构造方法中必须显示的调用父类中的有参数的构造方法(写在第一句) 。/进行留言进行留言public void setMessage(File f) throws IOExceptionString temp;RandomAccessFile rAF=new RandomAccessFile(f,“rw“);rAF.seek(rAF.length();rAF.write(author.getBytes();rAF.write(title.getBytes();rAF.write(content.
6、getBytes();/获取留言的内容获取留言的内容public String getMessage(File f) throws IOExceptionString temp=“;String atc=new String6;RandomAccessFile rAF=new RandomAccessFile(f,“r“);rAF.seek(0);int p=0;while(temp=rAF.readLine()!=null)/可以解决可以解决(中文字符中文字符)乱码的问题乱码的问题temp=new String(temp.getBytes(“ISO_8859_1“), “gb2312“);a
7、tcp=temp;p+;return atc; /创建 dialog 对话框 class MyFrame extends Frame MyFrame(String title)super(title);setLayout(new FlowLayout();addWindowListener(new MyWindowAdapter(this);/内嵌类可以直接存取其所在类中的私用成员.(这个类只为 Myframe 所使用,所以将其 定义为内嵌类)class MyWindowAdapter extends WindowAdapterMyFrame mFrame;public MyWindowAda
8、pter(MyFrame f)mFrame=f;/Frame 对象的 setVisible(false)方法或 dispose()方法来将窗口从屏幕中移除。public void windowClosing(WindowEvent we)mFrame.setVisible(false);mFrame.dispose();class MyDialog extends Dialog MyDialog(Frame f, String title, boolean b)super(f,title,b);setLayout(new FlowLayout();addWindowListener(new W
9、indowAdapter()public void windowClosing(WindowEvent e)setVisible(false);dispose(););public class Message_7 extends Applet implements ActionListener String atc1=new String6;/用来接收文件传回来的数据信息 String input=new String3;/存储用户信息 LMessage message,message0; File f=new File(“d:/实验七/message.txt“);private Label
10、L_head,L_author,L_title,L_content; private TextField T_author,T_title;/主窗口中的两个单行文本框 private TextArea A_content;/主窗口中的多行文本框 private Button submit,reset,display;/主窗口中的三个按钮 private Panel p12,p21,p22,p31,p32,p41,p42,p43;private MyFrame F_content; /子窗口对象 private TextArea A_content1;/子窗口中的多行文本框private But
11、ton DL_submit,DL_cancel; /对话框中的两个按钮 private MyDialog D_logon;/对话框对象Label l_logintime;Timer t;MyTask mt;Panel p51;String s;public void init() setLayout(new GridLayout(5,3);setBackground(Color.cyan);L_head=new Label(“留言板“);L_author=new Label(“作者:“);L_title=new Label(“标题:“);L_content=new Label(“内容:“);T
12、_author=new TextField(30);T_title=new TextField(30);A_content=new TextArea(4,10);submit=new Button(“submit“);reset=new Button(“reset“);display=new Button(“display“);p12=new Panel();p12.setFont(new Font(“宋体“,Font.BOLD,28);p12.setForeground(Color.red);p12.setLayout(new GridLayout(1,3);p12.add(new Labe
13、l(“);p12.add(L_head);p12.add(new Label(“);p21=new Panel();p21.setFont(new Font(“宋体“,Font.BOLD,15);p21.setLayout(new GridLayout(1,3);p21.add(new Label(“);p21.add(new Label(“);p21.add(L_author);p22=new Panel();p22.setFont(new Font(“宋体“,Font.BOLD,20);p22.setLayout(new GridLayout(3,3);p22.add(new Label(
14、“);p22.add(new Label(“);p22.add(new Label(“);p22.add(T_author);p22.add(new Label(“);p22.add(new Label(“);p22.add(new Label(“);p22.add(new Label(“);p22.add(new Label(“);p31=new Panel();p31.setFont(new Font(“宋体“,Font.BOLD,20);p31.setLayout(new GridLayout(1,3);p31.add(new Label(“);p31.add(new Label(“);
15、p31.add(L_title);p32=new Panel();p32.setFont(new Font(“宋体“,Font.BOLD,20);p32.setLayout(new GridLayout(3,3);p32.add(new Label(“);p32.add(new Label(“);p32.add(new Label(“);p32.add(T_title);p32.add(new Label(“);p32.add(new Label(“);p32.add(new Label(“);p32.add(new Label(“);p32.add(new Label(“);p41=new
16、Panel();p41.setFont(new Font(“宋体“,Font.BOLD,20);p41.setLayout(new GridLayout(3,3);p41.add(new Label(“);p41.add(new Label(“);p41.add(L_content);p41.add(new Label(“);p41.add(new Label(“);p41.add(new Label(“);p41.add(new Label(“);p41.add(new Label(“);p41.add(new Label(“);p42=new Panel();p42.setFont(new Fo