📄 showdiary.java
字号:
/*
* ShowDiary.java
*
* Created on 2008年4月27日, 下午2:47
*/
package pda;
import java.awt.FileDialog;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JOptionPane;
/**
*
* @author Administrator
*/
public class ShowDiary extends javax.swing.JFrame {
FileDialog fsave=new FileDialog(this,"保存文件对话框",FileDialog.SAVE);
FileDialog fopen=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);
private String path;
/** Creates new form ShowDiary */
public ShowDiary(String path) {
super("显示日志");
initComponents();
this.path=path;
openFile(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() {
jScrollPane1 = new javax.swing.JScrollPane();
conTextArea = new javax.swing.JTextArea();
openButton = new javax.swing.JButton();
delButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
Menu = new javax.swing.JMenu();
saveMenuItem = new javax.swing.JMenuItem();
delMenuItem = new javax.swing.JMenuItem();
openMenuItem = new javax.swing.JMenuItem();
conTextArea.setColumns(20);
conTextArea.setRows(5);
jScrollPane1.setViewportView(conTextArea);
openButton.setText("\u6253\u5f00\u5176\u4ed6\u65e5\u5fd7");
openButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openButtonActionPerformed(evt);
}
});
delButton.setText("\u5220\u9664\u5185\u5bb9");
delButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delButtonActionPerformed(evt);
}
});
saveButton.setText("\u4fdd\u5b58\u65e5\u5fd7");
saveButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveButtonActionPerformed(evt);
}
});
Menu.setText("\u7f16\u8f91");
saveMenuItem.setText("\u4fdd\u5b58\u65e5\u5fd7");
saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveMenuItemActionPerformed(evt);
}
});
Menu.add(saveMenuItem);
delMenuItem.setText("\u5220\u9664\u5185\u5bb9");
delMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delMenuItemActionPerformed(evt);
}
});
Menu.add(delMenuItem);
openMenuItem.setText("\u6253\u5f00\u5176\u4ed6\u65e5\u5fd7");
openMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openMenuItemActionPerformed(evt);
}
});
Menu.add(openMenuItem);
jMenuBar1.add(Menu);
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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(saveButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(delButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(openButton)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(openButton)
.addComponent(delButton)
.addComponent(saveButton))
.addContainerGap(40, Short.MAX_VALUE))
);
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 openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
openFile();
}//GEN-LAST:event_openMenuItemActionPerformed
private void delMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
conTextArea.setText(null);
}//GEN-LAST:event_delMenuItemActionPerformed
private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
// TODO 将在此处添加您的处理代码:
saveFile(path);
}//GEN-LAST:event_saveMenuItemActionPerformed
private void delButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButtonActionPerformed
// TODO 将在此处添加您的处理代码:
conTextArea.setText(null);
}//GEN-LAST:event_delButtonActionPerformed
private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
// TODO 将在此处添加您的处理代码:
saveFile(path);
}//GEN-LAST:event_saveButtonActionPerformed
private void openButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openButtonActionPerformed
// TODO 将在此处添加您的处理代码:
openFile();
}//GEN-LAST:event_openButtonActionPerformed
/**
* @param args the command line arguments
*/
/*public static void main(String args[]) {
ShowDiary sd=new ShowDiary("2008年4月15日.txt");
sd.setVisible(true);
}*/
public void openFile(String path){
try{
File file=new File(path);
FileReader fr=new FileReader(file);
BufferedReader br=new BufferedReader(fr);
while((s=br.readLine())!=null)
conTextArea.append(s+'\n');
br.close();
fr.close();
}catch (Exception e){}
}
public void openFile(){
fopen.setVisible(true);
conTextArea.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)
conTextArea.append(s+'\n');
br.close();
fr.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(conTextArea.getText(),0,(conTextArea.getText()).length());
bw.flush();
bw.close();
fw.close();
}catch(Exception e){
}
JOptionPane.showMessageDialog(null,"日志保存成功!");
}
// 变量声明 - 不进行修改//GEN-BEGIN:variables
private javax.swing.JMenu Menu;
private javax.swing.JTextArea conTextArea;
private javax.swing.JButton delButton;
private javax.swing.JMenuItem delMenuItem;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton openButton;
private javax.swing.JMenuItem openMenuItem;
private javax.swing.JButton saveButton;
private javax.swing.JMenuItem saveMenuItem;
// 变量声明结束//GEN-END:variables
private String s;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -