java l留言板的制作_留言板制作步骤
java l留言板的制作由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“留言板制作步骤”。
package com.cfxy.wwl.java.shixun;
import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.text.SimpleDateFormat;import java.util.Date;
import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.ScrollPaneConstants;/* * Swing:其实大部分组建派生与awt * * 组建的对应规则: * awt Swing * Frame JFrame * Button JButton */
public cla liuyanban {
//建立组件的引用
private JFrame f;private JButton b1;private JButton b2;private JButton b3;private JTextArea ta;
//构造方法
public liuyanban(){
init();}
//初始化显示界面
public void init(){
JFrame f=new JFrame(“我的留言板”);
f.setBounds(300, 200, 400, 400);
//设置布局方式
f.setLayout(new BorderLayout());
//三个按钮
b1=new JButton(“查看/刷新留言”);
b2=new JButton(“我要留言”);
b3=new JButton(“清除历史留言”);
//Panel
JPanel p=new JPanel();
p.setLayout(new GridLayout(1,3));
p.add(b1);
p.add(b2);
p.add(b3);
//创建带有滚动条的面板,其构造方法参数在接口ScrollPaneConstants中
JScrollPane jp=new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
ta=new JTextArea(“”);
ta.setWrapStyleWord(true);
jp.setViewportView(ta);
f.add(p,“North”);
f.add(jp,“Center”);
myEvent();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);} //加载各种监听器
public void myEvent(){
b2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
String label=b2.getText();
if(“我要留言”.equals(label)){
b2.setText(“提交留言”);
ta.setText(“留言信息:”);
}else{
//将留言信息存入文本文件
String s=ta.getText();
try {
PrintWriter FileWriter(“record.txt”,true));
Date d=new Date();
//SimpleDateFormat位于java.text包,这里使用其format(date)方法
SimpleDateFormat SimpleDateFormat(“yyyy/MM/dd hh:mm:”);
pw.println(sdf.format(d));
pw.println(s);
pw.println(“-------------”);
sdf=new
pw=new
PrintWriter(new
pw.close();
} catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
b2.setText(“我要留言”);
ta.setText(“留言已经提交.......”);
}
}
});
b1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
try {
ta.setText(“”);
BufferedReader FileReader(“record.txt”));
String line=“”;
while((line=br.readLine())!=null){
ta.append(line);
ta.append(“rn”);
br=new
BufferedReader(new
}
} catch(IOException e){
ta.setText(“文件不存在!”);
}
}
});
b3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0){
try {
PrintWriter FileWriter(“record.txt”));
pw.println(“留言记录:rn”);
pw.close();
ta.setText(“留言已经清除!”);
} catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
pw=new
PrintWriter(new
});}
public static void main(String[] args){
new liuyanban();} }