⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 notepad3.java

📁 用JAVA编的仿WINDOWS的记事本源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    step[i]=false;
   }
  }
  else if(e.getSource()==bt_left)     //"左对齐"操作
  {
   text.setAlignmentX(0.0f);
  }
  else if(e.getSource()==bt_center)   //"居中"操作
  {
   
  }
  else if(e.getSource()==bt_right)    //"右对齐"操作
  {
  
  }
  else if(e.getSource()==bt_font)   //"字体"设置
  {
   fontSize=Integer.parseInt(JOptionPane.showInputDialog(null,"请输入字体的大小(单位:象素):"));
  }
  else if(e.getSource()==bt_color)   //"文字"颜色设置
  {
   new SetColor();
   text.setCaretColor(Color.red);
  }
  ft=new Font("宋体",Font.PLAIN,fontSize); //为设置文本的字体
  text.setFont(ft);
 }


/******************************************************************************************/

//下面两个方法实现剪切版与此程序的数据交换
 protected static String getClipboardText(Clipboard clip) throws Exception
 { 
  // 获取剪切板中的内容 
  Transferable clipT = clip.getContents(null); 
  if (clipT != null) 
  { 
   // 检查内容是否是文本类型 
   if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor)) 
   {
    return (String)clipT.getTransferData(DataFlavor.stringFlavor); 
   } 
  } 
  return null;
 }
 protected static void setClipboardText(Clipboard clip, String writeMe) 
 { 
  Transferable tText = new StringSelection(writeMe); 
  clip.setContents(tText, null); 
 }

 //实现键盘事件
 public void keyTyped(KeyEvent key)
 {
  
 }
 public void keyPressed(KeyEvent key)
 {
  //int row=1,colum=1;
  if(key.getKeyCode()==KeyEvent.VK_ENTER)  //如果回车则行值加1
  {
   row++;
   colum=1;
   //lab_statusBar2.setText("字节数: "+(text.getText().length()-1)+"个");
  }
  else
  {
   colum++;
   //lab_statusBar2.setText("行 "+row+" 列 "+colum+"  字节数: "+text.getText().length());
  }
 }
 public void keyReleased(KeyEvent key)
 {
 
 }
 
 //下面为鼠标事件,主要是鼠标在菜单上的动作事件
 public void mouseClicked(MouseEvent mouse)
 {
  
 }
 public void mousePressed(MouseEvent mouse)
 {
  if(mouse.getSource()==text && mouse.getButton()==MouseEvent.BUTTON3 )
  {
    PopuMenu popu=new PopuMenu();
    popu.show((Component)text, mouse.getX(), mouse.getY());
  }
 }
 public void mouseReleased(MouseEvent mouse)
 {
 
 }
 public void mouseEntered(MouseEvent mouse)
 {
  //以下为鼠标移到下列组件上进产生的事件
  if(mouse.getSource()==menu1)
  {
   menu1.setBorder(BorderFactory.createLineBorder(Color.blue));
  }
  else if(mouse.getSource()==menu2)
  {
   menu2.setBorder(BorderFactory.createLineBorder(Color.blue));
  }
  else if(mouse.getSource()==menu3)
  {
   menu3.setBorder(BorderFactory.createLineBorder(Color.blue));
  }
  else if(mouse.getSource()==menu4)
  {
   menu4.setBorder(BorderFactory.createLineBorder(Color.blue));
  }
  else if(mouse.getSource()==menu5)
  {
   menu5.setBorder(BorderFactory.createLineBorder(Color.blue));
  }
 }
 public void mouseExited(MouseEvent mouse)
 {
  //鼠标移出组件时恢复先前设置
  menu1.setBorder(BorderFactory.createLineBorder(Color.white));
  menu2.setBorder(BorderFactory.createLineBorder(Color.white));
  menu3.setBorder(BorderFactory.createLineBorder(Color.white));
  menu4.setBorder(BorderFactory.createLineBorder(Color.white));
  menu5.setBorder(BorderFactory.createLineBorder(Color.white));
 }
 
 //下面是主方法
 public static void main(String [] args)
 {
  new NotePad3();
  new AboutNote();
 }
 
 
 //程序中的公有变量如下:
 private static int row=1,colum=1;
 private static int r=255,g=255,b=0;
 private static String path;   //path为在"打开","保存","另存为"文件时的路径变量
 private static String context;  //context为编辑时文本框内的内容
 private static boolean []step={false,false,false,false,false,false,false,false,false,false};
 private static int fontSize=16;
 private Font ft=new Font("楷体",Font.BOLD,fontSize); //为设置文本的字体
 
 private Clipboard cb=getToolkit().getSystemClipboard();  
 
 private JPanel panStatusBar;  //设置状态栏的容器
    //cb.setContents();  
 private JToolBar toolBar;
 private JFileChooser chooser;
 private JTextPane text;
 private JScrollPane scr;
 private JScrollBar scr1,scr2,scr3;
 private JPanel pColor;
 private Color c;
 
 private JMenuBar  menubar=new JMenuBar();
 private JMenu menu1=new JMenu("文件(F)");
 private JMenu menu2=new JMenu("编辑(E)");
 private JMenu menu3=new JMenu("查看(V)");
 private JMenu menu4=new JMenu("设置(S)");
 private JMenu menu5=new JMenu("关于(?)");
 
 private JMenuItem menuitem11=new JMenuItem("新建(N)",'N');
 private JMenuItem menuitem12=new JMenuItem("打开(O)",'O');
 private JMenuItem menuitem13=new JMenuItem("保存(S)",'S');
 private JMenuItem menuitem14=new JMenuItem("另存为(A)",'A');
 private JMenuItem menuitem15=new JMenuItem("只读模式(R)",'R');
 private JMenuItem menuitem16=new JMenuItem("退出(X)",'X');

 
 private JMenuItem menuitem21=new JMenuItem("撤销(U)",'U');
 private JMenuItem menuitem22=new JMenuItem("重复(R)",'R');
 private JMenuItem menuitem23=new JMenuItem("剪切(T)",'T');
 private JMenuItem menuitem24=new JMenuItem("复制(C)",'C');
 private JMenuItem menuitem25=new JMenuItem("全部复制(A)",'A');
 private JMenuItem menuitem26=new JMenuItem("粘贴(P)",'P');
 private JMenuItem menuitem27=new JMenuItem("删除");
 private JMenuItem menuitem28=new JMenuItem("全部删除(L)",'L');
 private JMenuItem menuitem29=new JMenuItem("全选(A)",'A');
 
 private JMenuItem menuitem31=new JMenuItem("放大页面(I)",'I');
 private JMenuItem menuitem32=new JMenuItem("缩小页面(O)",'O');
 private JMenuItem menuitem33=new JMenuItem("重置页面大小(Z)",'Z');
 
 private JMenuItem menuitem41=new JMenuItem("隐藏工具栏(T)",'T');
 private JMenuItem menuitem42=new JMenuItem("自定义工具栏(C)",'C');
 private JMenuItem menuitem43=new JMenuItem("隐藏状态栏(S)",'S');
 private JMenuItem menuitem44=new JMenuItem("C/C++编程");
 private JMenuItem menuitem45=new JMenuItem("java编程");
 private JMenuItem menuitem46=new JMenuItem("字体大小",'F');

 private JMenuItem menuitem51=new JMenuItem("关于(O)");
 
 //下面是为工具栏设置的按扭
 
 private JButton bt_new=new JButton(new ImageIcon("pic/new.gif"));
 private JButton bt_open=new JButton(new ImageIcon("pic/open.gif"));
 private JButton bt_save=new JButton(new ImageIcon("pic/save.gif"));
 private JButton bt_expler=new JButton(new ImageIcon("pic/expler.gif"));
 private JLabel bt_split1=new JLabel(new ImageIcon("pic/split.gif"));
 private JButton bt_back=new JButton(new ImageIcon("pic/back.gif"));
 private JButton bt_foward=new JButton(new ImageIcon("pic/foward.gif"));
 private JLabel bt_split2=new JLabel(new ImageIcon("pic/split.gif"));
 private JButton bt_cut=new JButton(new ImageIcon("pic/cut.gif"));
 private JButton bt_copy=new JButton(new ImageIcon("pic/copy.gif"));
 private JButton bt_paste=new JButton(new ImageIcon("pic/paste.gif"));
 private JLabel bt_split3=new JLabel(new ImageIcon("pic/split.gif"));
 private JButton bt_left=new JButton(new ImageIcon("pic/left.gif"));
 private JButton bt_center=new JButton(new ImageIcon("pic/center.gif"));
 private JButton bt_right=new JButton(new ImageIcon("pic/right.gif"));
 private JButton bt_font=new JButton(new ImageIcon("pic/font.gif"));
 private JButton bt_color=new JButton(new ImageIcon("pic/color.gif"));
 private JLabel bt_split4=new JLabel(new ImageIcon("pic/split.gif"));
 
 //下面为状态栏上显示的标签
 private JLabel lab_statusBar1=new JLabel("编辑",JLabel.LEFT);
 private JLabel lab_statusBar2;
 private JLabel lab_statusBar3=new JLabel("",JLabel.LEFT);
 private JLabel lab_statusBar4=new JLabel("Default Text",JLabel.LEFT);
 class PopuMenu extends JPopupMenu //implements ActionListener
 {
  public PopuMenu()
  {
   this.add(menuitem41);
   this.add(menuitem42);
   this.add(menuitem43);
  }
    }
    
 class SetColor extends JFrame implements AdjustmentListener,ItemListener
 {
  //实现两接口分别监听流动条事件和组合框事件
  JComboBox box=new JComboBox();
  JLabel lab=new JLabel("请选择设置对像");
  SetColor()
  {
   super("设置文本颜色");
   setResizable(false);
   setLocation(200,150);
   setSize(240,150);
   
   Image image1=java.awt.Toolkit.getDefaultToolkit().getImage("pic/icon.gif");
   pColor=new JPanel();
   pColor.setLayout(null);  

   c=new Color(r,g,b);
   pColor.setBounds(0,0,400,300);  

   scr1=new JScrollBar(JScrollBar.HORIZONTAL,0,10,0,255);
   scr2=new JScrollBar(JScrollBar.HORIZONTAL,0,10,0,255);
   scr3=new JScrollBar(JScrollBar.HORIZONTAL,0,10,0,255);
   
   scr1.setValue(255);
   scr2.setValue(255);
   scr3.setValue(0);

   scr1.setBounds(10,45,200,20);
   scr2.setBounds(10,70,200,20); 
   scr3.setBounds(10,95,200,20); 
   lab.setBounds(10,15,120,20);
   box.setBounds(130,15,80,20);
   
   box.addItem("文本");
   box.addItem("背景");
   
   scr1.addAdjustmentListener(this);  
   scr2.addAdjustmentListener(this);  
   scr3.addAdjustmentListener(this);  

   pColor.add(scr1);
   pColor.add(scr2);
   pColor.add(scr3);
   
   pColor.add(lab);
   pColor.add(box);
   pColor.setBackground(c);
   pColor.setVisible(true);
   
   box.addItemListener(this);
   
   this.setIconImage(image1);
   this.add(pColor);
   this.setVisible(true);
  }
  public void adjustmentValueChanged(AdjustmentEvent e)  //监听流动条事件
  {
   if(e.getAdjustable()==scr1)
   {
    if(box.getSelectedIndex()==0)    
    {
     r=scr1.getValue();
     g=scr2.getValue();
     b=scr3.getValue();
     c=new Color(r,g,b);
     pColor.setBackground(c);
     text.setForeground(c);
     text.setFont(ft);
    }
    else if(box.getSelectedIndex()==1)
    {
     r=scr1.getValue();
     g=scr2.getValue();
     b=scr3.getValue();
     c=new Color(r,g,b);
     pColor.setBackground(c);
     text.setBackground(c);
     text.setFont(ft);
    }
   }
   else if(e.getAdjustable()==scr2)
   {
    if(box.getSelectedIndex()==0)
    {
     r=scr1.getValue();
     g=scr2.getValue();
     b=scr3.getValue();
     c=new Color(r,g,b);
     pColor.setBackground(c);
     text.setForeground(c);
     text.setFont(ft);
    }
    else if(box.getSelectedIndex()==1)
    {
     r=scr1.getValue();
     g=scr2.getValue();
     b=scr3.getValue();
     c=new Color(r,g,b);
     pColor.setBackground(c);
     text.setBackground(c);
     text.setFont(ft);
    }
   }
   else if(e.getAdjustable()==scr3)
   {
    if(box.getSelectedIndex()==0)
    {
     r=scr1.getValue();
     g=scr2.getValue();
     b=scr3.getValue();
     c=new Color(r,g,b);
     pColor.setBackground(c);
     text.setForeground(c);
     text.setFont(ft);
    }
    else if(box.getSelectedIndex()==1)
    {
     r=scr1.getValue();
     g=scr2.getValue();
     b=scr3.getValue();
     c=new Color(r,g,b);
     pColor.setBackground(c);
     text.setBackground(c);
     text.setFont(ft);
    }
   }
  } 
  public void itemStateChanged(ItemEvent item)   //监听组合框事件
  {
   if(ItemEvent.SELECTED==0)
   {
    this.setTitle("设置文本颜色");
   }
   else if(ItemEvent.SELECTED==1)
   {
    this.setTitle("设置背景颜色");
   }
   else
   {
    this.setTitle("设置文本颜色");
   }
  } 
 }
 }
 
/*
class AboutNote extends JFrame implements ActionListener  //关于此记事本
{
 Color c=new Color(236,233,216);
 JTextArea about=new JTextArea(11,20);
 JPanel pane=new JPanel();
 JButton submit=new JButton("确定");
 JLabel lab;
 AboutNote()
 {
  super("关于记事本");
  setSize(250,250);
  setResizable(false);
  setLocation(250,200);
  
  pane.setLayout(null);
  pane.setBounds(0,0,250,250);
  pane.setBackground(c);
  
  lab=new JLabel(new ImageIcon("pic/newyear.gif"));
  lab.setBounds(5,0,80,150);
  
  submit.setBounds(95,190,60,22);
  
  about.setEditable(false);
  about.setBounds(60,5,150,180);
  about.setBackground(c);
  about.setText("Notepad3 1.0.20"+"\n"+
       "(J)Heary Storm 2007"+"\n"+
       "-------------------------------------"+"\n"+
       "发送意见到:"+"\n"+
       "likui800@sina.com"+"\n"+
       "更多版本信息:"+"\n"+
       "QQ : 286262640"+"\n"+
       "-------------------------------------"+"\n"+
       "制作人:李奎(Heary Storm)"+"\n"+
       "[E-mail:likui800@sina.com]"+"\n"+
       "\n");
                        
        pane.add(lab);
  pane.add(about);
  pane.add(submit);
  
  submit.addActionListener(this);
  
  this.add(pane);
  
  Image image1=java.awt.Toolkit.getDefaultToolkit().getImage("pic/icon.gif");
  this.setIconImage(image1);
  setVisible(true);
 }
 public void actionPerformed(ActionEvent e)
 {
  this.setVisible(false);
 }  
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -