📄 diarybook.java
字号:
/*
* DiaryBook.java
*
* Created on 2008年3月20日, 下午3:41
*/
package pda;
import java.awt.FileDialog;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class DiaryBook extends javax.swing.JFrame {
FileDialog fsave=new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
FileDialog fopen=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
private Calendar t;
private String n;
private String path;
/** Creates new form DiaryBook */
public DiaryBook(String path) {
super("新建日志");
initComponents();
this.path=path;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane2 = new javax.swing.JScrollPane();
diaryArea = new javax.swing.JTextArea();
save = new javax.swing.JButton();
del = new javax.swing.JButton();
open = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
edit = new javax.swing.JMenu();
saveDiary = new javax.swing.JMenuItem();
delDiary = new javax.swing.JMenuItem();
openDiary = new javax.swing.JMenuItem();
diaryArea.setColumns(20);
diaryArea.setRows(5);
jScrollPane2.setViewportView(diaryArea);
save.setText("\u4fdd\u5b58\u65e5\u5fd7");
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
del.setText("\u5220\u9664\u5185\u5bb9");
del.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delActionPerformed(evt);
}
});
open.setText("\u6253\u5f00\u5176\u4ed6\u65e5\u5fd7");
open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openActionPerformed(evt);
}
});
edit.setText("\u7f16\u8f91");
saveDiary.setText("\u4fdd\u5b58\u65e5\u5fd7");
saveDiary.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveDiaryActionPerformed(evt);
}
});
edit.add(saveDiary);
delDiary.setText("\u5220\u9664\u5185\u5bb9");
delDiary.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delDiaryActionPerformed(evt);
}
});
edit.add(delDiary);
openDiary.setText("\u6253\u5f00\u5176\u4ed6\u65e5\u5fd7");
openDiary.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openDiaryActionPerformed(evt);
}
});
edit.add(openDiary);
jMenuBar1.add(edit);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(85, 85, 85)
.addComponent(save)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(del)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(open))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(save)
.addComponent(del)
.addComponent(open))
.addContainerGap())
);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-400)/2, (screenSize.height-318)/2, 400, 318);
}// </editor-fold>//GEN-END:initComponents
private void openDiaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openDiaryActionPerformed
// TODO 将在此处添加您的处理代码:
openFile();
}//GEN-LAST:event_openDiaryActionPerformed
private void delDiaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delDiaryActionPerformed
// TODO 将在此处添加您的处理代码:
diaryArea.setText(null);
}//GEN-LAST:event_delDiaryActionPerformed
private void saveDiaryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveDiaryActionPerformed
// TODO 将在此处添加您的处理代码:
saveFile(path);
}//GEN-LAST:event_saveDiaryActionPerformed
private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed
// TODO 将在此处添加您的处理代码:
openFile();
}//GEN-LAST:event_openActionPerformed
private void delActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delActionPerformed
// TODO 将在此处添加您的处理代码:
diaryArea.setText(null);
}//GEN-LAST:event_delActionPerformed
private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
// TODO 将在此处添加您的处理代码:
saveFile(path);
}//GEN-LAST:event_saveActionPerformed
/*public void time(){
t=new GregorianCalendar();
n=t.get(t.YEAR)+"年"+(t.get(t.MONTH)+1)+"月"+t.get(t.DAY_OF_MONTH)+"日:\n";
diaryArea.setText(n);
}*/
public void openFile(){
fopen.setVisible(true);
diaryArea.setText(null);
String s;
if(fopen.getFile()!=null)
{
try{
File file=new File(fopen.getDirectory(),fopen.getFile());
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
while((s=br.readLine())!=null)
diaryArea.append(s+'\n');
br.close();
fr.close();
}catch (Exception e){
}
}
}
/*public void saveFile(){
fsave.setVisible(true);
if(fsave.getFile()!=null)
{
try{
File file=new File(fsave.getDirectory(),fsave.getFile());
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(diaryArea.getText(),0,(diaryArea.getText()).length());
bw.flush();
bw.close();
fw.close();
}catch(Exception e){
}
}
}*/
public void saveFile(String path){
try{
File file=new File(path);
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
bw.write(diaryArea.getText(),0,(diaryArea.getText()).length());
bw.flush();
bw.close();
fw.close();
}catch(Exception e){
}
JOptionPane.showMessageDialog(null,"日志保存成功!");
}
/**
* @param args the command line arguments
*/
/*public static void main(String args[]) {
DiaryBook db=new DiaryBook("");
db.setVisible(true);
}*/
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JButton del;
private javax.swing.JMenuItem delDiary;
private javax.swing.JTextArea diaryArea;
private javax.swing.JMenu edit;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JButton open;
private javax.swing.JMenuItem openDiary;
private javax.swing.JButton save;
private javax.swing.JMenuItem saveDiary;
// 变量声明结束//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -