简单记事本

package xu01;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import java.io.*;
public class Nodepad extends JFrame
{
private JTextArea editor;
private Container c;


private JMenuBar mb;
private JMenu fileMenu;
private JMenu editMenu;
private JMenu formatMenu;
private JMenuItem fileMenuOpen,fileMenuSave,fileMenuExit;
private JMenuItem editMenuCopy,editMenuCut,editMenuPaste;
private JMenu formatSet;
private JMenuItem lineWrap,cancleLineWrap,wrapStyleWord,cancleWrapStyleWord;
private JMenu fontMenu;
private JMenu color;
private JMenu style;
private JMenuItem bold,italic,hong,lan,lv,huang,hei;
int b=Font.PLAIN;
int i=Font.PLAIN;

String[] zh={"8","10","12","14","16","18","20","24","28","32","40","56",};
private JMenu fontlabel;
private JComboBox c1,c2;
String ss="宋体";
int s=12;

private String [] mz={"宋体","华文行楷","黑体","华文新魏","方正舒体","方正桃体","隶书","楷体","幼圆","华文彩云",};

private JToolBar toolBar;
private JButton b1,b2,b3,b4,b5;

private JPopupMenu pm;
private JMenuItem item1,item2,item3,item4,item5;

public Nodepad()
{
super("简单记事本");
setBounds(200,100,500,400);
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e){ System.err.println("不能被设置外观的原因:"+e);}
c=getContentPane();
editor = new JTextArea();
c.add(new JScrollPane(editor));


mb = new JMenuBar();
fileMenu = new JMenu("文件(F)");
fileMenuOpen = new JMenuItem("打开(O)...Ctrl+O");
fileMenuSave = new JMenuItem("保存(S)...Ctrl+S");
fileMenuExit = new JMenuItem("退出");
JMHandler JM=new JMHandler();
fileMenuOpen.addActionListener(JM);
fileMenuSave.addActionListener(JM);
fileMenuExit.addActionListener(JM);
fileMenu.add(fileMenuOpen);
fileMenu.add(fileMenuSave);
fileMenu.addSeparator();
fileMenu.add(fileMenuExit);



editMenu = new JMenu("编辑(E)");
editMenuCopy = new JMenuItem("复制(C) Ctrl+C");
editMenuCut = new JMenuItem("剪切(T) Ctrl+X");
editMenuPaste = new JMenuItem("粘贴(P) Ctrl+v");
EMHandler EM=new EMHandler();
editMenuCopy.addActionListener(EM);
editMenuCut.addActionListener(EM);
editMenuPaste.addActionListener(EM);
editMenu.add(editMenuCopy);
editMenu.add(editMenuCut);
editMenu.add(editMenuPaste);



formatMenu = new JMenu("格式(O)");
formatSet = new JMenu("格式设置");
lineWrap = new JMenuItem("自动换行");
cancleLineWrap = new JMenuItem("取消自动换行");
wrapStyleWord = new JMenuItem("断行不断字");
cancleWrapStyleWord = new JMenuItem("取消断行不断字");
FMHandler FM=new FMHandler();
lineWrap.addActionListener(FM);
cancleLineWrap.addActionListener(FM);
wrapStyleWord.addActionListener(FM);
cancleWrapStyleWord.addActionListener(FM);
formatSet.add(lineWrap);
formatSet.add(cancleLineWrap);
formatSet.addSeparator();
formatSet.add(wrapStyleWord);
formatSet.add(cancleWrapStyleWord);
formatMenu.add(formatSet);


fontMenu=new JMenu("字体(Z)");
color=new JMenu("颜色");
hong=new JMenuItem("红色");
color.add(hong);
lan=new JMenuItem("蓝色");
color.add(lan);
lv=new JMenuItem("绿色");
color.add(lv);
huang=new JMenuItem("黄色");
color.add(huang);
hei=new JMenuItem("黑色");
color.add(hei);

bold=new JCheckBoxMenuItem("粗体");

italic=new JCheckBoxMenuItem("斜体");

HMHandler HM=new HMHandler();
hong.addActionListener(HM);
lan.addActionListener(HM);
lv.addActionListener(HM);
huang.addActionListener(HM);
bold.addActionListener(HM);
italic.addActionListener(HM);
color.addActionListener(HM);

fontMenu.add(color);
fontMenu.add(bold);
fontMenu.add(italic);



fontlabel=new JMenu("字号(H)");

c1=new JComboBox(zh);
c1.setBackground(Color.green);
c1.setFont(new Font("宋体",Font.PLAIN,12));
c1.setMaximumRowCount(5);
CMHandler CM=new CMHandler();
c1.addActionListener(CM);
fontlabel.add(c1);

style=new JMenu("宇体样式(S)");
c2=new JComboBox(mz);
c2.setBackground(Color.green);
c2.setFont(new Font("宋体",Font.PLAIN,12));
c2.setMaximumRowCount(5);
SMHandler SM=new SMHandler();
c2.addItemListener(SM);
style.add(c2);

mb.add(fileMenu);
mb.add(editMenu);
mb.add(formatMenu);
mb.add(fontMenu);
mb.add(fontlabel);
mb.add(style);





toolBar =new JToolBar();
b1= new JButton(new ImageIcon("d:/img/open.gif"));
b2= new JButton(new ImageIcon("d:/img/save.gif"));
b3= new JButton(new ImageIcon("d:/img/copy.gif"));
b4= new JButton(new ImageIcon("d:/img/cut.gif"));
b5= new JButton(new ImageIcon("d:/img/paste.gif"));
TBHandler TB=new TBHandler();
b1.addActionListener(TB); b2.addActionListener(TB);
b3.addActionListener(TB); b4.addActionListener(TB);
b5.addActionListener(TB);

toolBar.add(b1); toolBar.add(b2);
toolBar.add(b3); toolBar.add(b4);
toolBar.add(b5);



pm =new JPopupMenu();
item1 = new JMenuItem("打开");
item2 = new JMenuItem("保存");
item3 = new JMenuItem("复制");
item4 = new JMenuItem("剪切");
item5 = new JMenuItem("粘贴");
JPHandler JP=new JPHandler();
item1.addActionListener(JP);
item2.addActionListener(JP);
item3.addActionListener(JP);
item4.addActionListener(JP);
item5.addActionListener(JP);
editor.addMouseListener(JP);
pm.add(item1); pm.add(item2);
pm.add(item3); pm.add(item4);
pm.add(item5);







setJMenuBar(mb);
c.add(toolBar,BorderLayout.NORTH);


setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}


private class CMHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==c1)
{
s=Integer.parseInt(zh[c1.getSelectedIndex()]);
editor.setFont(new Font(ss,b+i,s));
}
}
}


private class JMHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==fileMenuOpen) { loadFile(); }
else if(e.getSource()==fileMenuSave){ saveFile(); }
else{System.exit(0);}
}

}

public void loadFile()
{
JFileChooser fc=new JFileChooser();
int r=fc.showOpenDialog(this);
if(r==JFileChooser.APPROVE_OPTION)
{
File file=fc.getSelectedFile();
try{ editor.read(new FileReader(file),null);}
catch(IOException e){}
}
}

public void saveFile()
{
JFileChooser fc=new JFileChooser();
int r=fc.showSaveDialog(this);
if(r==JFileChooser.APPROVE_OPTION)
{
File file=fc.getSelectedFile();
try{ editor.write(new FileWriter(file));}
catch(IOException e){}
}
}



private class EMHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==editMenuCopy)
{
editor.copy();
editor.requestFocus();
}
else if(e.getSource()==editMenuCut)
{
editor.cut();
editor.requestFocus();
}
else
{
editor.paste();
editor.requestFocus();
}
}
}

private class FMHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==lineWrap){ editor.setLineWrap(true); }
else if(e.getSource()==cancleLineWrap) { editor.setLineWrap(false);}
else if(e.getSource()==wrapStyleWord) { editor.setWrapStyleWord(true);}
else{ editor.setWrapStyleWord(false); }
}
}



private class TBHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1){ loadFile(); }
else if(e.getSource()==b2) {saveFile();}
else if(e.getSource()==b3)
{
editor.copy();
editor.requestFocus();
}
else if(e.getSource()==b4)
{
editor.cut();
editor.requestFocus();
}
else
{
editor.paste();
editor.requestFocus();
}
}
}


private class JPHandler implements ActionListener,MouseListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==item1){ loadFile(); }
else if(e.getSource()==item2) {saveFile();}
else if(e.getSource()==item3)
{
editor.copy();
editor.requestFocus();
}
else if(e.getSource()==item4)
{
editor.cut();
editor.requestFocus();
}
else
{
editor.paste();
editor.requestFocus();
}
}

public void mouseReleased(MouseEvent e)
{
if(e.isPopupTrigger())
pm.show(editor,e.getX(),e.getY());
}

public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
}

public class HMHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String cmd=e.getActionCommand();
int b;
int i;
if(cmd.equals("红色"))
editor.setForeground(Color.red);
else if(cmd.equals("蓝色"))
editor.setForeground(Color.blue);
else if(cmd.equals("绿色"))
editor.setForeground(Color.green);
else if(cmd.equals("黄色"))
editor.setForeground(Color.yellow);
else if(cmd.equals("黑色"))
editor.setForeground(Color.black);
else if(cmd.equals("粗体"))
editor.setFont(new Font("宋体",Font.BOLD,s));
else if(cmd.equals("斜体"))
editor.setFont(new Font("宋体",Font.ITALIC,s));
}
}

public class SMHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange()==e.SELECTED)
{
ss=(String)c2.getSelectedItem();
editor.setFont(new Font(ss,b+i,s));
}
}
}

public static void main(String []args)
{
Nodepad N=new Nodepad();
}
}

 

posted @ 2023-05-26 09:41  薯条1600  阅读(23)  评论(0编辑  收藏  举报