java电子日记本代码及运行结果Word文档下载推荐.docx
《java电子日记本代码及运行结果Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《java电子日记本代码及运行结果Word文档下载推荐.docx(15页珍藏版)》请在冰豆网上搜索。
),
b_fontCommen=newJButton("
还原"
b_fontBig=newJButton("
大字"
b_fontRed=newJButton("
红色"
b_fontBlue=newJButton("
蓝色"
b_bg=newJButton("
背景"
);
Remember()
Containercon=getContentPane();
DefaultMutableTreeNoderoot=newDefaultMutableTreeNode("
日记本"
//设置以月份为节点的树
for(i=1;
i<
=12;
i++)
month[i]=newDefaultMutableTreeNode("
"
+i+"
月"
root、add(month[i]);
}
//设置不同月份的节点
//处理有31天的月份
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
for(intj=1;
j<
=31;
j++){
Strings1=month[i]、toString();
Strings2=j+"
日、txt"
;
new);
//如果这一天写了日记则相应节点后面添加一个“*”来标志
if(()==true)month[i]、add(newDefaultMutableTreeNode(j+"
日*"
));
elsemonth[i]、add(newDefaultMutableTreeNode(j+"
日"
//处理有30天的月份
elseif(i==4||i==6||i==9||i==11)
=30;
//处理有28天的月份
else
{
=28;
j++){
//设置监听
b_save、addActionListener(this);
b_fontRed、addActionListener(this);
b_fontCommen、addActionListener(this);
b_fontBig、addActionListener(this);
b_fontBlue、addActionListener(this);
b_bg、addActionListener(this);
//创建树及其面板
tree=newJTree(root);
JPanelp=newJPanel();
//设置边界布局
p、setLayout(newBorderLayout());
JScrollPanescrollpane_1=newJScrollPane(text);
//带滚动条的面板放置到中间,可以占据最大空间位置
p、add(scrollpane_1,BorderLayout、CENTER);
//设置面板,放置"
与"
删除"
按钮等按钮,面板放在上部
JPanelp_1=newJPanel();
p_1、add(b_save);
p_1、add(b_fontCommen);
p_1、add(b_fontBig);
p_1、add(b_fontRed);
p_1、add(b_fontBlue);
p_1、add(b_bg);
//设置布局
p、add(p_1,BorderLayout、NORTH);
JScrollPanescrollpane_2=newJScrollPane(tree);
JSplitPanesplitpane=newJSplitPane(JSplitPane、HORIZONTAL_SPLIT,true,scrollpane_2,p);
tree、addTreeSelectionListener(this);
con、add(splitpane);
//设置窗体适配器,关闭功能
addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEvente){
System、exit(0);
}});
setVisible(true);
setBounds(200,150,500,600);
//处理树节点事件
publicvoidvalueChanged(TreeSelectionEvente)
//清空文本区中的内容
text、setText("
if(e、getSource()==tree)
//定义被选中的节点
DefaultMutableTreeNodenode=
(DefaultMutableTreeNode)tree、getLastSelectedPathComponent();
//如果就是叶节点,则可以对文件进行操作
if(node、isLeaf())
//日记以“-月-日、txt”的形式存储,所以要消除“*”号的影响
Stringstr=node、toString();
str=str、substring(0,str、length()-1);
for(inti=0;
if(node、getParent()==month[i])
try{
Stringtemp=null;
//打开以节点(“*”号影响已经消除)所定义的文本为文件名的文件
new()、toString()+str+"
、txt"
//建立文本文件输入流,读取文件
(f);
BufferedReaderin=newBufferedReader(file);
//将读到的文件内容添加到文本区中,按行读入
while((temp=in、readLine())!
=null)
text、append(temp+'
\n'
();
in、close();
catch(e1){}
catch(IOExceptione1){}
//处理按钮事件
publicvoidactionPerformed(ActionEvente)
//处理"
按钮的事件
if(e、getSource()==b_save)
//保存文件时去除“*”号的影响
str=str、replace("
*"
"
//保存文件
try{
//建立以节点所定义的文本为文件名的文件
//建立文本文件输出流
to(f);
BufferedWriterout=newBufferedWriter(tofile);
//将文本区中的内容写到文件缓冲区
out、write(text、getText(),0,(text、getText())、length());
//将缓冲区中的内容写到文件
out、flush();
to();
out、close();
//处理"
按钮事件
elseif(e、getSource()==b_fontBig)
text、setFont(newFont("
Serif"
1,18));
普通"
elseif(e、getSource()==b_fontCommen)
0,12));
text、setForeground(Color、black);
elseif(e、getSource()==b_fontRed)
text、setForeground(Color、red);
elseif(e、getSource()==b_fontBlue)
text、setForeground(Color、blue);
//处理"
elseif(e、getSource()==b_bg)
//随机变化背景颜色
//随机+-操作,当op>
0、5时做减法负责做加法,
doubleop=Math、random();
doublec=Math、random()*10;
if(op>
0、5)
//使r,g,b三个标志RGB的变量减小一个随机的大小小在0-50之间的数
if(c<
4)r-=Math、random()*50;
if(c>
4&
&
c<
7)g-=Math、random()*50;
if(c>
7)b-=Math、random()*50;
if(op<
=0、5)
{
//使r,g,b三个标志RGB的变量增加一个随机的大小小在0-50之间的数
if(c<
}
text、setBackground(newColor(r,g,b));
//删除文件
voiddel()
//调用外部命令执行批处理程序
Runtimece=Runtime、getRuntime();
ce、exec("
gxy、bat"
}catch(IOExceptione1){}
//登陆窗口类
classLoginWindowextendsJDialogimplementsActionListener
JTextFieldtxtUserName=newJTextField(15);
//用户名文本框
JTextFieldtxtPwd=newJPasswordField(15);
//密码框
JButtonok=newJButton("
确定"
JButtoncancel=newJButton("
取消"
publicLoginWindow()
JPanel[]p=newJPanel[4];
4;
i++)
p[i]=newJPanel();
JPanelp1=newJPanel();
//定义并建立面板
setModal(true);
//设置模态
setBackground(Color、LIGHT_GRAY);
//设置背景色
ContainercontentPane=this、getContentPane();
//取出内容面板
contentPane、setLayout(newGridLayout(5,1));
//设置布局为5行1列
p[1]、add(newJLabel("
用户名:
p[1]、add(txtUserName);
//将组件添加到中间容器
p[2]、add(newJLabel("
密码:
p[2]、add(txtPwd);
p[3]、add(ok);
p[3]、add(cancel);
ok、addActionListener(this);
cancel、addActionListener(this);
txtUserName、addActionListener(this);
txtPwd、addActionListener(this);
contentPane、add(p[0]);
//将面板添加到内容面板
contentPane、add(p[1]);
contentPane、add(p[2]);
contentPane、add(p[3]);
setDefaultCloseOperation(JFrame、DISPOSE_ON_CLOSE);
//设置自动关闭窗口
setSize(300,220);
//Dimension类封装单个对象中组件的宽度与高度
Dimensionscreen=Toolkit、getDefaultToolkit()、getScreenSize();
setLocation((screen、width-300)/2,(screen、height-220)/2);
setTitle("
登录窗口"
setResizable(false);
//设置窗口大小固定
//添加事件监听器
//处理”确定”按钮事件
if(e、getSource()==ok||e、getSource()==txtPwd)
if(txtUserName、getText()、trim()、equals("
c"
)&
txtPwd、getText()、equals("
123"
))//如果用户名为c密码为123则正确
dispose();
//关闭登录窗口
JOptionPane、showMessageDialog(null,"
用户名或密码错误!
txtUserName、requestFocus();
//设置焦点
txtUserName、setSelectionStart(0);
//设置选中文本开始位置
txtUserName、setSelectionEnd(txtUserName、getText()、length());
//处理”取消”按钮事件
elseif(e、getSource()==cancel)
dispose();
System、exit(0);
//退出程序
elseif(e、getSource()==txtUserName)//在用户名文本框按回车移动焦点到密码框
txtPwd、requestFocus();
运行,出现登陆界面
若输入的用户名与密码有误
输入正确的用户名:
c
正确的密码:
123
登陆成功,进入电子日记版面
输入内容,对字体进行加粗,变色与添加背景
点击还原
凡就是在当天有过记录的日期都有*的标志