import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.*;
public class Notebook extends JFrame {
Container contianer;
JFileChooser fc=new JFileChooser();
File file=null;
JTextArea jta;
private MyFindDialog fd = new MyFindDialog(this, jta);
public Notebook()
{
this.setTitle("记事本");
contianer=this.getContentPane();
contianer.setLayout(new BorderLayout());
jta=new JTextArea();
contianer.add(jta);
JMenuBar mb=new JMenuBar();
buildMainMenu(mb);
this.setJMenuBar(mb);
this.setVisible(true);
this.setSize(650, 450);
}
void buildMainMenu(JMenuBar mb) {
JMenu fileMenu=new JMenu("文件(F)");
fileMenu.setMnemonic(KeyEvent.VK_F);
JMenuItem newMenuItem=new JMenuItem("新建");
fileMenu.add(newMenuItem);
JMenuItem openMenuItem=new JMenuItem("打开");
openMenuItem.setMnemonic(KeyEvent.VK_O);
openMenuItem.addActionListener(new OpenActionListener());
fileMenu.add(openMenuItem);
JMenuItem saveMenuItem=new JMenuItem("保存");
saveMenuItem.setMnemonic(KeyEvent.VK_S);
saveMenuItem.addActionListener(new SaveActionListener());
fileMenu.add(saveMenuItem);
JMenuItem saveasMenuItem=new JMenuItem("另存为");
saveasMenuItem.setMnemonic(KeyEvent.VK_A);
saveasMenuItem.addActionListener(new SaveActionListener());
fileMenu.add(saveasMenuItem);
JMenuItem exitMenuItem=new JMenuItem("退出");
exitMenuItem.setMnemonic(KeyEvent.VK_E);
exitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E ,ActionEvent.CTRL_MASK));
exitMenuItem.addActionListener(new ExitActionListener());
fileMenu.add(exitMenuItem);
mb.add(fileMenu);
JMenu editMenu=new JMenu("编辑(E)");
editMenu.setMnemonic(KeyEvent.VK_E);
JMenuItem cutMenuItem=new JMenuItem("剪切");
cutMenuItem.setMnemonic(KeyEvent.VK_T);
cutMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
cutMenuItem.addActionListener(new CutActionListener());
editMenu.add(cutMenuItem);
JMenuItem copyMenuItem=new JMenuItem("复制");
copyMenuItem.setMnemonic(KeyEvent.VK_C);
copyMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));
copyMenuItem.addActionListener(new CopyActionListener());
editMenu.add(copyMenuItem);
JMenuItem pasteMenuItem=new JMenuItem("粘贴");
pasteMenuItem.setMnemonic(KeyEvent.VK_P);
pasteMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
pasteMenuItem.addActionListener(new PasteActionListener());
editMenu.add(pasteMenuItem);
JMenuItem sallMenuItem=new JMenuItem("全选");
sallMenuItem.setMnemonic(KeyEvent.VK_A);
sallMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
sallMenuItem.addActionListener(new SallActionListener());
editMenu.add(sallMenuItem);
JMenuItem searchMenuItem=new JMenuItem("查找");
searchMenuItem.setMnemonic(KeyEvent.VK_S);
searchMenuItem.addActionListener(new SearchActionListener());
editMenu.add(searchMenuItem);
JMenuItem chanMenuItem=new JMenuItem("替换");
chanMenuItem.setMnemonic(KeyEvent.VK_G);
chanMenuItem.addActionListener(new ChanActionListener());
editMenu.add(chanMenuItem);
mb.add(editMenu);
JMenu helpMenu=new JMenu("帮助(H)");
helpMenu.setMnemonic(KeyEvent.VK_H);
JMenuItem abouthMenuItem=new JMenuItem("关于记事本");
abouthMenuItem.setMnemonic(KeyEvent.VK_A);
abouthMenuItem.addActionListener(new AbouthActionListener());
helpMenu.add(abouthMenuItem);
mb.add(helpMenu);
}
class MyFindDialog extends JDialog implements ActionListener {
private Label l1 = new Label("查找字符串");
private Label l2 = new Label("替换字符串");
private TextField tf1 = new TextField(10);
private TextField tf2 = new TextField(10);
private Button b1 = new Button("向下查找");
private Button b2 = new Button("替换");
public MyFindDialog(Frame owner, JTextArea jta) {
super(owner, "查找", false);
setLayout(null);
l1.setBounds(10, 30, 80, 20);
l2.setBounds(10, 70, 80, 20);
tf1.setBounds(90, 30, 90, 20);
tf2.setBounds(90, 70, 80, 20);
b1.setBounds(190, 30, 80, 20);
b2.setBounds(190, 70, 80, 20);
add(l1);
add(l2);
add(tf1);
add(tf2);
add(b1);
add(b2);
setResizable(false);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void showFind() {
setTitle("查找");
setSize(280, 300);
setVisible(true);
}
public void showReplace() {
setTitle("查找替换");
setSize(280,300);
setVisible(true);
}
private void find() {
String content = jta.getText();
String str = tf1.getText();
int pp=jta.getCaretPosition();
int end=content.length();
String text=content.substring(pp,end);
if(str!=null&&!str.equals(""))
{
int start=text.indexOf(str);
if(start>=0)
{start=start+pp;
jta.setSelectionStart(start);
jta.setSelectionEnd(start+str.length());
}
else
{
javax.swing.JOptionPane.showMessageDialog(this,"成功完成向下查找!","查找完毕",JOptionPane.OK_OPTION | JOptionPane.INFORMATION_MESSAGE);
}
}
}
private void replace() {
String str2 = tf2.getText();
find();
jta.replaceRange(str2, jta.getSelectionStart(),jta.getSelectionEnd());
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1)
find();
else if (e.getSource() == b2)
replace();
}
}
class SaveActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(file!=null) fc.setSelectedFile(file);
int returnVal=fc.showSaveDialog(Notebook.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fc.getSelectedFile();
saving();}
}
void saving(){
try{
FileWriter Writef=new FileWriter(file);
Writef.write(jta.getText());
Writef.close();
}
catch(Exception e){e.printStackTrace();}
}
}
class OpenActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(file !=null)fc.setSelectedFile(file);
int returnVal=fc.showOpenDialog(Notebook.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fc.getSelectedFile();
unfold();}}
void unfold(){
try{
FileReader Readf=new FileReader(file);
int len=(int)file.length();
char []buffer=new char[len];
Readf.read(buffer,0,len);
Readf.close();
jta.setText(new String(buffer));
}catch(Exception e){e.printStackTrace();}
}
}
class ExitActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
setVisible(false);
dispose();
System.exit(0);
}
}
class CutActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
jta.cut();
}
}
class CopyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
jta.copy();
}
}
class PasteActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
jta.paste();
}
}
class SallActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e) {
jta.setSelectionStart(0);
jta.setSelectionEnd(jta.getText().length());
}
}
class SearchActionListen
打小狠呆
- 粉丝: 2
- 资源: 7
最新资源
- 5G SRM815模组原理框图.jpg
- T型3电平逆变器,lcl滤波器滤波器参数计算,半导体损耗计算,逆变电感参数设计损耗计算 mathcad格式输出,方便修改 同时支持plecs损耗仿真,基于plecs的闭环仿真,电压外环,电流内环
- 毒舌(解锁版).apk
- 显示HEX、S19、Bin、VBF等其他汽车制造商特定的文件格式
- 操作系统实验 Ucore lab5
- 8bit逐次逼近型SAR ADC电路设计成品 入门时期的第三款sarADC,适合新手学习等 包括电路文件和详细设计文档 smic0.18工艺,单端结构,3.3V供电 整体采样率500k,可实现基
- 操作系统实验 ucorelab4内核线程管理
- 脉冲注入法,持续注入,启动低速运行过程中注入,电感法,ipd,力矩保持,无霍尔无感方案,媲美有霍尔效果 bldc控制器方案,无刷电机 提供源码,原理图
- Matlab Simulink#直驱永磁风电机组并网仿真模型 基于永磁直驱式风机并网仿真模型 采用背靠背双PWM变流器,先整流,再逆变 不仅实现电机侧的有功、无功功率的解耦控制和转速调节,而且能实
- 157389节奏盒子地狱模式第三阶段7.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈