Java的记事本 实现了记录关闭时的尺寸大小

本文介绍了一个简单的记事本程序,实现了窗口尺寸记录功能。通过读写properties文件,该程序可以保存并恢复用户调整过的窗口大小,提供更好的用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一个类:
package notepad;

import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
* 用于存储记事本尺寸大小的类 只是利用了文件的读写操作 对properties文件的操作
* */
public class NotePadSize
{
private File file;

private Properties p;

String w, h;

public NotePadSize()
{
w = new String();
h = new String();

w = "400";
h = "400";

file = new File("G:\\3.6\\5.1\\src\\notepad\\size.properties");

p = new Properties();

}

/**
* 从本地文件中读取上次的记事本的长宽
*
* @throws IOException
* */
protected void ReadSize() throws IOException
{
FileInputStream reader;
reader = new FileInputStream(file);

p.load(reader);

// 读取长宽
this.w = p.getProperty("width");
this.h = p.getProperty("height");

reader.close();

}

/**
* 存储记事本的长宽
*
* @throws IOException
* */
protected void SaveSize() throws IOException
{
FileOutputStream save;
save = new FileOutputStream(file);
p.setProperty("width", w);
p.setProperty("height", h);
p.store(save, "");
save.close();
}

/**
* 获取长宽
*
* @throws IOException
* */
public Dimension getSize() throws IOException
{
ReadSize();

Dimension size = new Dimension(Integer.parseInt(this.w),
Integer.parseInt(this.h));
return size;
}

/**
* 设置长宽,并保存
*
* @throws IOException
* */
public void setSize(Dimension Size) throws IOException
{
this.w = String.valueOf(Size.width);
this.h = String.valueOf(Size.height);

SaveSize();

}

}


第二个类:
package notepad;

import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
* 记事本beta 1.1.0版本 最新功能:实现了记事本的尺寸记录功能
* */

@SuppressWarnings("serial")
public class NotePad extends JFrame implements WindowListener
{
private JScrollPane txtJs;

private JTextArea txtArea;

private JMenuBar jm;

private JMenu file, edit, format, help;

private JMenuItem file_new, file_open, file_save, exit;

private JMenuItem edit_cut, edit_copy, edit_paste, edit_find,
edit_selectAll;

private JCheckBoxMenuItem format_wrap;

private JMenuItem help_issue;

private JPopupMenu jpm;
private JMenuItem jp_cut, jp_copy, jp_paste, jp_selectAll;

private FileDialog openFileDialog, saveFileDialog;

private NotePadSize size;

private Dimension NotePadDimension;

public NotePad() throws IOException
{

super("NotePad");

jm = new JMenuBar();

file = new JMenu("文件(F)");
edit = new JMenu("编辑(E)");
format = new JMenu("格式(O)");
help = new JMenu("帮助(H)");

// 文件菜单的实例化
file_new = new JMenuItem("新建(N)");
file_open = new JMenuItem("打开(O)");
file_save = new JMenuItem("保存(S)");
exit = new JMenuItem("退出(X)");
file.add(file_new);
file.add(file_open);
file.add(file_save);
file.addSeparator();
file.add(exit);

// 编辑菜单的实例化
edit_cut = new JMenuItem("剪切");
edit_copy = new JMenuItem("复制");
edit_paste = new JMenuItem("粘贴");
edit_find = new JMenuItem("查找");
edit_selectAll = new JMenuItem("全选");
edit.add(edit_cut);
edit.add(edit_copy);
edit.add(edit_paste);
edit.addSeparator();
edit.add(edit_find);
edit.addSeparator();
edit.add(edit_selectAll);

// 格式菜单的实例化
format_wrap = new JCheckBoxMenuItem("自动换行");
format.add(format_wrap);

// 帮助菜单的实例化
help_issue = new JMenuItem("关于记事本");
help.add(help_issue);

// 添加添加右键弹出式菜单
jpm = new JPopupMenu();
jp_cut = new JMenuItem("剪切");
jp_copy = new JMenuItem("复制");
jp_paste = new JMenuItem("粘贴");
jp_selectAll = new JMenuItem("全选");
jpm.add(jp_cut);
jpm.add(jp_copy);
jpm.add(jp_paste);
jpm.addSeparator();
jpm.add(jp_selectAll);

// 注册组件监听
file_new.addActionListener(new NotePadActionListener());
file_open.addActionListener(new NotePadActionListener());
file_save.addActionListener(new NotePadActionListener());
exit.addActionListener(new NotePadActionListener());
edit_cut.addActionListener(new NotePadActionListener());
edit_copy.addActionListener(new NotePadActionListener());
edit_paste.addActionListener(new NotePadActionListener());
edit_find.addActionListener(new NotePadActionListener());
edit_selectAll.addActionListener(new NotePadActionListener());
format_wrap.addActionListener(new NotePadActionListener());
help_issue.addActionListener(new NotePadActionListener());
jp_cut.addActionListener(new NotePadActionListener());
jp_copy.addActionListener(new NotePadActionListener());
jp_paste.addActionListener(new NotePadActionListener());
jp_selectAll.addActionListener(new NotePadActionListener());

// 实例化对话框
openFileDialog = new FileDialog(this, "打开文件", FileDialog.LOAD);
saveFileDialog = new FileDialog(this, "保存文件", FileDialog.SAVE);

jm.add(file);
jm.add(edit);
jm.add(format);
jm.add(help);

setJMenuBar(jm);
txtArea = new JTextArea();
// 使用匿名内部内,为textArea添加鼠标监听
txtArea.addMouseListener(new MouseAdapter()
{

public void mouseClicked(MouseEvent e)
{
if (e.getButton() == MouseEvent.BUTTON3)
{
jpm.show(e.getComponent(), e.getX(), e.getY());
}
}
});
txtJs = new JScrollPane(txtArea);
add(txtJs);

size = new NotePadSize();

// 添加窗口事件,记录窗口的大小尺寸
addWindowListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
NotePadDimension = new Dimension(size.getSize());
setSize(NotePadDimension);
// 让窗体居中显示
setLocation(getToolkit().getScreenSize().width / 2
- NotePadDimension.width / 2,
getToolkit().getScreenSize().height / 2
- NotePadDimension.height / 2);

setVisible(true);
}

// ///Main method
public static void main(String[] args) throws IOException
{
new NotePad();
}

// /内部类,用于处理事件//
class NotePadActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if (o == file_new)
{ // 新建文件
txtArea.setText("");
}
if (o == file_open)
{// 打开文件
openFileDialog.setVisible(true);
String name = openFileDialog.getDirectory()
+ openFileDialog.getFile();
OpenFile(name);
}

if (o == file_save)
{// 保存文件
saveFileDialog.setVisible(true);
String name = saveFileDialog.getDirectory()
+ saveFileDialog.getFile();

SaveFile(name);
}
if (o == exit)
{// 退出
System.exit(0);
}
if (o == help_issue)
{// 帮助
JOptionPane.showMessageDialog(txtArea,
"关于记事本:\n仅供学习参考 NotePad Beta 1.1");
}

// //编辑事件/
if (o == edit_cut || o == jp_cut)
{
txtArea.cut();
}
if (o == edit_copy || o == jp_copy)
{
txtArea.copy();
}
if (o == edit_paste || o == jp_paste)
{
txtArea.paste();
}
if (o == edit_selectAll || o == jp_selectAll)
{
txtArea.selectAll();
}
// ///end edit event
if (o == edit_find)
{
// 从输入对话框中获取需要查询的内容
String findStr = JOptionPane.showInputDialog(txtArea,
"输入查询的内容", "查找", JOptionPane.QUESTION_MESSAGE);
Find(findStr);

}
if (o == format_wrap)
{// 自动换行
if (format_wrap.isSelected())
{
txtArea.setLineWrap(true);
}
else
txtArea.setLineWrap(false);
}
}

public void Find(String s)
{
int x;
String txt = txtArea.getText();
x = txt.indexOf(s);
if (x != -1)
txtArea.select(x, x + s.length());
else
JOptionPane.showMessageDialog(txtArea, "查找失败", "没有找到",
JOptionPane.WARNING_MESSAGE);
}

public void OpenFile(String fileName)
{// 读取文件 使用了reader方法
File file = new File(fileName);
try
{
FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
String s = new String();
try
{
while ((s = br.readLine()) != null)
{
txtArea.append(s + "\n");
}
br.close();
reader.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}

// 保存文件
public void SaveFile(String fileName)
{
File file = new File(fileName);
try
{
FileWriter save = new FileWriter(file);
save.write(txtArea.getText());
save.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

/**
* 实现WindowListenner接口,当关闭窗口的时候获取窗口大小,
* 然后调用NotePadSize类的setSize方法用properties文件保存最后尺寸
* */
public void windowClosing(WindowEvent e)
{
NotePadDimension = this.getSize();

try
{
size.setSize(NotePadDimension);
}
catch (IOException e1)
{
e1.printStackTrace();
}
}

public void windowOpened(WindowEvent e)
{
}

public void windowClosed(WindowEvent e)
{
}

public void windowIconified(WindowEvent e)
{
}

public void windowDeiconified(WindowEvent e)
{
}

public void windowActivated(WindowEvent e)
{
}

public void windowDeactivated(WindowEvent e)
{
}

}


当然还有个properties文件

#
#Tue May 03 21:01:46 CST 2011
height=311
width=317
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值