Java 记事本代码
Java 记事本代码
源文件、文档说明、可运行的jar文件下载地址
免费下载地址在 http://linux.bkjia.com/
用户名与密码都是www.bkjia.com
具体下载目录在 /pub/2011/11/05/Java 记事本代码/
- /**
- * NoteBook
- *
- * @versionliujunguang
- * @version 1.00 09/10/25
- */
- public class NoteBook {
- public static void main(String[] args) {
- // Create application frame.
- NoteBookFrame frame = new NoteBookFrame();
- // Show frame
- frame.setVisible(true);
- }
- }
- import java.awt.*;
- /**
- * NoteBook-OpenClass.
- *
- * @liujunguang
- * @version 1.00 09/10/28
- */
- class SeeClass extends Frame
- {
- NoteBookFrame notebookframe = null;
- SeeClass(NoteBookFrame p)
- {
- notebookframe = p;
- }
- public void Modo()
- {
- System.out.println("状态栏");
- }
- }
- //编辑类中的查找选项的实现
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.lang.*;//String 类
- import javax.swing.JOptionPane;
- class SearchFrame extends JFrame implements ActionListener,ItemListener//单选按钮事件接口
- {
- JLabel label1,label2,label3;
- JButton but1,but2,but3,but4;
- JTextField textfield,text;
- JRadioButton radiobutton1,radiobutton2;
- ButtonGroup group1;
- JCheckBox box1;
- NoteBookFrame notebookframe = null;
- String sub1="",sub2="";
- int n=-1;
- boolean isup = false;//判断是否是最后一个找到的内容时用到
- boolean direction = true ;//判断查找方向 向下为 true
- SearchFrame(NoteBookFrame p)
- {
- notebookframe = p;
- label1 = new JLabel("查找内容(N):");
- label2 = new JLabel("方向");
- label3 = new JLabel("替换为(P)");
- but1 = new JButton("查找下一个(F)");
- but2 = new JButton(" 取 消 ");
- but3 = new JButton(" 替 换 ");
- but4 = new JButton(" 转 到 ");
- textfield = new JTextField(15);
- text = new JTextField(15);
- radiobutton1 = new JRadioButton("向下(U)",true);
- radiobutton2 = new JRadioButton("向上(D)");
- group1 = new ButtonGroup();
- //创建组件
- box1 = new JCheckBox("区分大小写(C)");
- Container con = getContentPane();
- con.setLayout(null);//设置布局为空
- group1.add(radiobutton1);
- group1.add(radiobutton2);//将单选按钮加到按钮组中
- con.add(label1);//查找
- label1.setBounds(10,10,85,25);
- con.add(label3);//替换为
- label3.setBounds(10,45,85,25);
- con.add(textfield);//文本
- textfield.setBounds(90,10,180,25);
- con.add(text);//文本
- text.setBounds(90,45,180,25);
- con.add(but1);//查找下一个
- but1.setBounds(280,10,120,25);
- con.add(label2);//方向
- label2.setBounds(135,80,50,25);
- con.add(box1);
- box1.setBounds(10,115,120,25);
- con.add(radiobutton1);//向下
- radiobutton1.setBounds(130,115,70,25);
- con.add(radiobutton2);//向上
- radiobutton2.setBounds(200,115,70,25);
- con.add(but2);//取消
- but2.setBounds(280,115,120,25);
- con.add(but3);//替换
- but3.setBounds(280,45,120,25);
- con.add(but4);//转到
- but4.setBounds(280,80,120,25);
- but1.addActionListener(this);
- but2.addActionListener(this);
- but3.addActionListener(this);
- but4.addActionListener(this);
- box1.addItemListener(this);
- textfield.addActionListener(this);
- text.addActionListener(this);
- radiobutton1.addItemListener(this);
- radiobutton2.addItemListener(this);
- //注册监听
- setBounds(500,300,420,180);
- setVisible(false);
- setTitle("查找");
- validate();
- }
- public void actionPerformed(ActionEvent e)
- {
- sub1 = notebookframe.textarea.getText();//得到文本区中的文本
- sub2 = textfield.getText();//得到文本框中的文本
- if(!isup) //如果不区分大小写(默认不区分大小写)
- {
- sub1 = sub1.toLowerCase();//将sub1转换成小写
- sub2 = sub2.toLowerCase();//将sub2转换成小写
- }
- if(!direction) n = sub1.lastIndexOf(sub2);//在文本区中查找文本框中的内容
- else n = sub1.indexOf(sub2);//在文本区中查找文本框中的内容
- if(e.getSource() == but1||e.getSource() == textfield)//查找下一个
- {
- if(n!=-1)
- {
- notebookframe.toFront();//如果此窗口是可见的,则将此窗口置于前端,并可以将其设为焦点 Window
- notebookframe.textarea.select(n,n+sub2.length());//选中查找的内容
- this.setVisible(false);
- }
- else
- {
- JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);
- }
- }
- if(e.getSource() == but2)//取消
- {
- this.setVisible(false);
- }
- if(e.getSource() == but4)//转到
- {
- int i=0,j=1;
- char ch[];
- try{
- int raw = Integer.parseInt(textfield.getText());//得到文本框中的文本
- String s = notebookframe.textarea.getText();
- ch = new char[s.length()];
- s.getChars(0,s.length()-1,ch,0);
- while(j<raw)
- {
- i++;
- if(ch[i] == '/n')
- {j++;}
- if(i == s.length())break;
- }
- if(raw == 1) //转到第一行
- notebookframe.textarea.setCaretPosition(0);
- else
- notebookframe.textarea.setCaretPosition(i+1);//转到指定行
- this.setVisible(false);
- }
- catch(Exception a){
- JOptionPane.showMessageDialog(this,"你输入的位置不对无法到达!","记事本",JOptionPane.WARNING_MESSAGE);
- }
- }
- if(e.getSource() == but3)//替换
- {
- if(n != -1)
- {
- String sub3 = text.getText();
- notebookframe.textarea.select(n,n+sub2.length());//选中查找的内容
- notebookframe.textarea.replaceRange(sub3,n,n+sub2.length());//替换选中位置的文本
- sub1 = notebookframe.textarea.getText();//得到文本区中的文本
- sub2 = textfield.getText();//得到文本框中的文本
- if(!isup) //如果不区分大小写(默认不区分大小写)
- {
- sub1 = sub1.toLowerCase();//将sub1转换成小写
- sub2 = sub2.toLowerCase();//将sub2转换成小写
- }
- if(!direction) n = sub1.lastIndexOf(sub2);//在文本区中查找最后出现的文本框中的内容
- else n = sub1.indexOf(sub2);//在文本区中查找最先出现的文本框中的内容
- notebookframe.toFront();//如果此窗口是可见的,则将此窗口置于前端,并可以将其设为焦点 Window
- if(n!=-1 )
- {
- notebookframe.textarea.select(n,n+sub2.length());//选中查找内容
- }
- else
- JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);
- }
- else
- {
- JOptionPane.showMessageDialog(this,"所指定的文本没有找到无法替换!","记事本",JOptionPane.WARNING_MESSAGE);
- }
- }
- }
- public void nextShear()//查找下一个 菜单项对应的
- {
- if(n!=-1)
- {
- sub1 = notebookframe.textarea.getText().substring(n+sub2.length());//得到文本区的子串
- if(!isup) //如果不区分大小写(默认不区分大小写)
- {
- sub1 = sub1.toLowerCase();//将sub1转换成小写
- sub2 = sub2.toLowerCase();//将sub2转换成小写
- }
- if(sub1.indexOf(sub2)!=-1)
- {
- n = n+sub2.length()+sub1.indexOf(sub2);//得到查找内容在文本区中的位置
- notebookframe.textarea.select(n,n+sub2.length());//选中查找内容
- }
- else
- JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);
- }
- else
- JOptionPane.showMessageDialog(this,"所指定的文本没有找到!","记事本",JOptionPane.WARNING_MESSAGE);
- }
- public void itemStateChanged(ItemEvent ee)
- {
- if(ee.getSource() == box1)
- {
- if(box1.isSelected())
- {
- isup = true ;
- }
- else
- isup = false ;
- }
- if(ee.getSource() == radiobutton1)
- {
- if(radiobutton1.isSelected())
- {
- direction = true ;
- }
- }
- if(ee.getSource() == radiobutton2)
- {
- if(radiobutton2.isSelected())
- {
- direction = false;
- }
- }
- }
- }
|
评论暂时关闭