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

📄 notepad.java

📁 基本实现了记事本的所有功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 {
	  //响应菜单项"新建"的事件
	  if(e.getSource()==f_new)
	  {
	   if(isCurrentFileSaved())
	   {
	    textArea.setText("");
	    stateBar.setText("新文档");
	   }
	   else
	   {
	    int i=JOptionPane.showConfirmDialog(null,"文件未保存,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
	    if(i ==JOptionPane.YES_OPTION)
	    saveFileDialog();
	    else if(i==JOptionPane.NO_OPTION)
	    {
	     textArea.setText("");
	     stateBar.setText("新文档");
	    }
	    else
	    return;
	   }
	  }

	  //响应菜单项"打开"的事件
	  if(e.getSource()==f_open)
	  {
	   if(isCurrentFileSaved())
	   {
	    openFileDialog();
	   }
	   else
	   {
	    int i=JOptionPane.showConfirmDialog(null,"文件未保存,是否要保存?","提示",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
	    if(i ==JOptionPane.YES_OPTION)
	    saveFileDialog();
	    else if(i==JOptionPane.NO_OPTION)
	    {
	     openFileDialog();
	    }
	    else
	    return;
	   }
	  }

	  //响应菜单项"保存"和"按钮"保存"的事件
	  if((e.getSource()==b_save)||(e.getSource()==f_save))
	  {
	   if(isCurrentFileExists)
	   {
	    try{
	     String str=textArea.getText();
	     FileWriter fw = new FileWriter(tempFile);
	     fw.write(str);
	     fw.close();

	     isCurrentFileExists=true;
	     tempFile=tempFile;

	     stateBar.setText("已保存");
	    }
	    catch(Exception ex)
	    {
	     JOptionPane.showMessageDialog(null, "出错:"+ ex.getMessage());
	     return ;
	    }
	   }
	   else
	    saveFileDialog();
	  }

	  //响应菜单"另存为"的事件
	  if(e.getSource()==f_saveas)
	  {
	   int option=jfc.showDialog(this,"另存为");

	   String fname = null;
	   if(option == JFileChooser.APPROVE_OPTION)
	   {
	    File f = jfc.getSelectedFile();//如果没有选取文件,下面的jfc.getName(f)将会返回输入的文件名
	    fname = jfc.getName(f);
	    if(fname != null && fname.trim().length()>0)
	    {
	     if(fname.endsWith(".txt"))
	     ;
	     else
	     {
	      fname = fname.concat(".txt");
	     }
	    }
	    if(f.isFile())
	    fname = f.getName();
	    f = jfc.getCurrentDirectory();
	    f = new File(f.getPath().concat(File.separator).concat(fname));

	    if(f.exists())
	    {
	     int i=JOptionPane.showConfirmDialog(null,"该文件已经存在,确定要覆盖吗?");
	     if(i ==JOptionPane.YES_OPTION)
	     ;
	     else
	     return;
	    }
	    try{
	     f.createNewFile();
	     String str=textArea.getText();
	     FileWriter fw = new FileWriter(f);
	     fw.write(str);
	     fw.close();
	     stateBar.setText("已保存");
	    }
	    catch(Exception ex)
	    {
	     JOptionPane.showMessageDialog(null, "出错:"+ ex.getMessage());
	     return ;
	    }
	   }
	   else
	   {
	    JOptionPane.showMessageDialog(null,"文件没有保存!");
	   }
	  }

	  //响应"退出"菜单和"关闭"按钮的事件
	  if((e.getSource()==f_close)||(e.getSource()==b_close))
	  {
	   whenExit();
	  }
	  if(e.getSource()==e_copy||e.getSource()==je_copy)
	  {
	   copy();
	  }
	  if(e.getSource()==e_paste||e.getSource()==je_paste)
	  {
	   paste();
	  }
	  if(e.getSource()==e_cut||e.getSource()==je_cut)
	  {
	   cut();
	  }
	  if(e.getSource()==e_clear||e.getSource()==je_clear)
	  {
	   clear();
	  }
	  if(e.getSource()==e_selectAll||e.getSource()==je_selectAll)
	  {
	   selectAll();
	  }
	  if(e.getSource()==s_font)                  //创建字体选择对话框
	  {
	   Font font;
	   font =new Font("新宋体",Font.PLAIN,12);
	   font = FontChooser.showDialog(this,null,font);
	   if(font!=null)    //判断是否选择了"确定"按钮
	   {
	    textArea.setFont(font);
	   }
	   else
	   {
	    textArea.setFont(textArea.getFont());
	   }   
	  }
	  if(e.getSource()==s_color)                 //创建颜色选择对话框
	  {
	   Color c=JColorChooser.showDialog(this,"请选择文字颜色",Color.cyan);
	   if(c!=null)
	   {
	    textArea.setForeground(c);
	   }
	   else
	    textArea.setForeground(textArea.getForeground());
	  }
	  if(e.getSource()==h_editor)
	  {
	   JOptionPane.showMessageDialog(this,"计科06级5班,郑世杰,200631500155\n              superle@vip.qq.com ","关于记事本",JOptionPane.INFORMATION_MESSAGE);
	  }

	 }

	 public static void main (String[] args)     //创建一个NotePad的匿名对象
	 {
	  new NotePad();
	 }
	}

	 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*           此字体对话框的使用方法
*   Font font = null;
*   font = FontChooser.showDialog(this,null,font);
*   textArea.setFont(font);
*/
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
class FontChooser extends JDialog
{
 String[] styleList = new String[]
 {"常规","粗体","斜体" ,"粗斜体"};
 String[] sizeList = new String[]
 {"3","4","5","6","7","8","9","10","11","12","13","14","15","16","17",
 "18","19","20","22","24","26","28","30","34","39","45","50","60","72"};
 MyList  StyleList;
 MyList  FontList ;
 MyList  SizeList ;
 static JLabel Sample = new JLabel();
 public static boolean isSelected = false;

 private FontChooser(Frame parent,boolean modal,Font font)
 {
  super (parent,modal);
  initAll();
  setTitle("字体");
  if (font == null) font = Sample.getFont();
  FontList.setSelectedItem(font.getName());
  SizeList.setSelectedItem(font.getSize()+"");
  StyleList.setSelectedItem(styleList[font.getStyle()]);

 }
 public static Font showDialog(Frame parent,String s,Font font)
 {
  FontChooser fd = new FontChooser(parent,true,font);
  if (s != null) fd.setTitle(s);
  fd.setVisible(true);
  Font fo = null;
  if (fd.isSelected) fo = Sample.getFont();  //如果选择OK按钮,则获取字体
  fd.dispose();
  return(fo);
 }
 private void initAll()
 {
  getContentPane().setLayout(null);
  int W = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
  int H = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
  setBounds((W-425)/2,(H-400)/2,430,400);   //设置字体窗口居屏幕中间显示
  addLists();
  addButtons();
  Sample.setBounds(10,312,400,28);
  Sample.setForeground(Color.black);
  getContentPane().add(Sample);
  addWindowListener(new WindowAdapter()
  {public void windowClosing(java.awt.event.WindowEvent e)
   {
    setVisible (false);
    isSelected=false;
   }
  });
 }
 private void addLists()
 {
  FontList = new MyList(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames());
  StyleList = new MyList(styleList);
  SizeList = new MyList(sizeList);
  FontList.setBounds(10,10,260,295);
  StyleList.setBounds(280,10,80,295);
  SizeList.setBounds(370,10,40,295);
  getContentPane().add(FontList);
  getContentPane().add(StyleList);
  getContentPane().add(SizeList);
 }
 private void addButtons()
 {
 JButton ok = new JButton("确定");
 ok.setMargin(new Insets(0,0,0,0));
 JButton ca = new JButton("取消");
 ca.setMargin(new Insets(0,0,0,0));
 ok.setBounds(260,350,70,20);
 ok.setFont(new Font(" ",1,12));
 ca.setBounds(340,350,70,20);
 ca.setFont(new Font(" ",1,12));
 getContentPane().add(ok);
 getContentPane().add(ca);
 
 ok.addActionListener(new ActionListener()
 {public void actionPerformed(ActionEvent e)
  {
   setVisible(false);
      isSelected = true;
  }
 });
  
 ca.addActionListener(new ActionListener()
 {public void actionPerformed(ActionEvent e)
   {
    setVisible(false);
    isSelected = false;
   }
 });
 
 }
 private void showSample()
 {
  int g = 0;
  try {g = Integer.parseInt(SizeList.getSelectedValue());}
  catch(NumberFormatException nfe){}
  String st = StyleList.getSelectedValue();
  int s = Font.PLAIN;
  if (st.equalsIgnoreCase("粗体")) s = Font.BOLD;
  if (st.equalsIgnoreCase("斜体")) s = Font.ITALIC;
  if (st.equalsIgnoreCase("粗斜体")) s = Font.ITALIC | Font.BOLD;
  Sample.setFont(new Font(FontList.getSelectedValue(),s,g));
  Sample.setHorizontalAlignment(SwingConstants.LEFT);
  Sample.setBorder(BorderFactory.createEtchedBorder());
  Sample.setText("效果预览,abcdefg");
 }

 public class MyList extends JPanel
 {
  JList jl;
  JScrollPane sp;
  JLabel jt;
  String si = " ";

  public MyList(String[] values)
  {
   setLayout(null);
   jl = new JList(values);
   sp = new JScrollPane(jl);
   jt = new JLabel();
   jt.setBackground(Color.white);
   jt.setForeground(Color.black);
   jt.setOpaque(true);
   jt.setBorder(new JTextField().getBorder());
   jt.setFont(getFont());
   jl.setBounds(0,0,100,1000);
   jl.setBackground(Color.white);
   jl.addListSelectionListener(new ListSelectionListener()
   {public void valueChanged(ListSelectionEvent e)
    { 
     jt.setText((String)jl.getSelectedValue());
     si = (String)jl.getSelectedValue();
        showSample();
    }
   });
   add(sp);
   add(jt);
  }
  public String getSelectedValue()
  {
   return(si);
  }
  public void setSelectedItem(String s)
  {
   jl.setSelectedValue(s,true);
  }
  public void setBounds(int x, int y, int w ,int h)
  {
   super.setBounds(x,y,w,h);
   sp.setBounds(0,y+12,w,h-23);
   sp.revalidate();
   jt.setBounds(0,0,w,20);
  }

  }
}

⌨️ 快捷键说明

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