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

📄 notebook.java

📁 一个记事本程序
💻 JAVA
字号:
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.awt.*;

public class NoteBook extends JFrame implements ActionListener
{
	public JTextArea textArea;
	private JScrollPane scrollBar;
	private JMenuBar menuBar;
	private JMenu menu;
	private JMenuItem menuItem;
	
	private File file=new File("1.txt");
	private JFileChooser fileChooser=new JFileChooser();
	private String fileName=null;
	private boolean isChanged=false;

	private MyFontChooser fontChooser=new MyFontChooser();
	
	private Timer myTimer;
	
	NoteBook()
	{
		super("NoteBook");
		setBounds(250,200,550,400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		textArea=new JTextArea();
		textArea.setLineWrap(true);//允许自动换行

		scrollBar=new JScrollPane(textArea);
		getContentPane().add(scrollBar);
		scrollBar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		menuBar=new JMenuBar();	
		setJMenuBar(menuBar);	//将菜单条放在窗口顶端
		menu=new JMenu("文件");	
		menuBar.add(menu);		//将文件菜单放进菜单条
		menuItem=new JMenuItem("新建");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("new");
		menu.add(menuItem);		//将子菜单放进文件父菜
		menuItem=new JMenuItem("打开");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("open");
		menu.add(menuItem);
		menuItem=new JMenuItem("保存");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("save");
		menu.add(menuItem);
		menu.addSeparator();
		menuItem=new JMenuItem("退出");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("exit");
		menu.add(menuItem);
		
		menu=new JMenu("编辑");
		menuBar.add(menu);
		menuItem=new JMenuItem("剪切");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("cut");
		menu.add(menuItem);
		menuItem=new JMenuItem("复制");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("copy");
		menu.add(menuItem);
		menuItem=new JMenuItem("粘贴");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("stick");
		menu.add(menuItem);
		menuItem=new JMenuItem("全选");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("selectall");
		menu.add(menuItem);
		menu.addSeparator();
		menuItem=new JMenuItem("清除");
		menu.add(menuItem);
		menuItem.addActionListener(this);
		menuItem.setActionCommand("clear");
		
		menu=new JMenu("设置");
		menuBar.add(menu);
		menuItem=new JMenuItem("字体颜色");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("fontColor");
		menu.add(menuItem);
		menuItem=new JMenuItem("字体样式");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("fontStyle");
		menu.add(menuItem);
		
				
		menu=new JMenu("帮助");
		menuBar.add(menu);
		menuItem=new JMenuItem("关于");
		menuItem.addActionListener(this);
		menuItem.setActionCommand("about");
		menu.add(menuItem);
		
		menu.addActionListener(this);
		textArea.addKeyListener(new KeyListener(){
			public void keyTyped(KeyEvent e){}
			public void keyPressed(KeyEvent e){isChanged=true;}
			public void keyReleased(KeyEvent e){}
		});
		
		myTimer=new Timer(500,new ActionListener(){
			public void actionPerformed(ActionEvent ev){
				String str=fontChooser.getStrFont();
				int st=fontChooser.getFontStyle();
				int si=fontChooser.getFontSize();
				textArea.setFont(new Font(str,st,si));
			}
		});
		myTimer.start();
		setTitle("NoteBook -"+file.getName());
		setVisible(true);
		
	}
	//=======================================================================
	public void openFile()
	{
		int flag=fileChooser.showOpenDialog(this);
		if(flag!=fileChooser.CANCEL_OPTION)
		{
			file=fileChooser.getSelectedFile().getAbsoluteFile();
			try{
				FileReader getFile=new FileReader(file);
				int len=(int)file.length();
				char[] buffer=new char[len];
				getFile.read(buffer,0,len);
				getFile.close();
				textArea.setText(new String(buffer));
			}
			catch(IOException ex){}
			setTitle("NoteBook -"+file.getName());
		}
	}
	//=======================================================================
	public void saveFile()
	{
		if(file.exists())
		{
			try
			{
				FileWriter putFile=new FileWriter(file);
				putFile.write(textArea.getText());
				putFile.close();
			}
			catch(IOException ex){}
		}
		else
		{
			int flag=fileChooser.showSaveDialog(this);
			if(flag!=fileChooser.CANCEL_OPTION)
			{
				fileName=fileChooser.getSelectedFile().getAbsolutePath();
				if(fileName.endsWith(".txt"))
				{
					try{
						FileWriter putFile=new FileWriter(fileName);
						putFile.write(textArea.getText());
						putFile.close();
					}
					catch(IOException ex){}
				}
				else if(fileName!="")
				{
					try{
						FileWriter putFile=new FileWriter(fileName+".txt");
						putFile.write(textArea.getText());
						putFile.close();
					}catch(IOException ex){}
				}
			}
		}
	}
	//=======================================================================
	public void actionPerformed(ActionEvent e)
	{
		String command=e.getActionCommand();
		if(command=="new")
		{
			if(isChanged)
			{
				int flag=JOptionPane.showConfirmDialog(this,
											  "你已经改变了文本内容,是否保存?",
											  "请确认",
											  JOptionPane.YES_NO_CANCEL_OPTION);
				if(flag==0)
					saveFile();
				else if(flag==1)
					textArea.setText("");
			}
			else
			{
				file=new File("1.txt");
				setTitle("NoteBook -1.txt");
				textArea.setText("");
			}
		}
		else if(command=="open")
		{
			openFile();
		}
		else if(command=="save")
		{
			saveFile();
			isChanged=false;
		}
		else if(command=="exit")
		{
			System.exit(0);
		}
		//-------------------------------------------------
		else if(command=="cut")
		{
			textArea.cut();
		}
		else if(command=="copy")
		{
			textArea.copy();
		}
		else if(command=="stick")
		{
			textArea.paste();
		}
		else if(command=="selectall")
		{
			textArea.selectAll();
		}
		else if(command=="clear")
		{
			textArea.setText("");
		}
		//********************************************************
		else if(command=="fontColor")
		{
			JColorChooser colorChooser=new JColorChooser();
			textArea.setForeground(colorChooser.showDialog(this,"字体颜色设置",Color.black));
		}
		else if(command=="fontStyle")
		{
			fontChooser.showWindow(true);		
		}
		//********************************************************
		else
		{
			JOptionPane.showMessageDialog(this,
										  "作者:※乱舞靑春※\n"+
										  "时间:2007-4-20\n"+
										  "QQ:287209159\n"+
										  "email: wu2yi2bing@163.com ",
										  "关于作者",
										  JOptionPane.OK_OPTION,
										  new ImageIcon("image\\icon.gif"));
										 
		}
	}
}
//**************************************************************************
class MyFontChooser extends JFrame implements ActionListener
{
	private int style;
	private int size;
	private String strFont;
	
	private int tempStyle;
	private int tempSize;
	private String tempStrFont;
	
	private JRadioButton radioBtnST;
	private JRadioButton radioBtnYY;
	private JRadioButton radioBtnKT;
	private JRadioButton radioBtnHT;
	private JRadioButton radioBtnLS;
	private ButtonGroup  group1;
	
	private JRadioButton radioBtnBold;
	private JRadioButton radioBtnItalic;
	private JRadioButton radioBtnBI;
	private JRadioButton radioBtnPlain;
	private ButtonGroup  group2;
	
	private JComboBox    comboBox;
	private JButton      btnOK;
	private JButton      btnCancle;
	
	public MyFontChooser()
	{
		super("Font Setting");
		setBounds(300,250,300,150);
		style=Font.PLAIN;
		size=15;
		strFont="宋体";
		tempStyle=Font.PLAIN;
		tempSize=15;
		tempStrFont="宋体";
		
		group1=new ButtonGroup();
		radioBtnST=new JRadioButton("宋体",true);
		radioBtnST.setBackground(Color.cyan);
		radioBtnST.addActionListener(this);
		radioBtnYY=new JRadioButton("幼圆",false);
		radioBtnYY.setBackground(Color.cyan);
		radioBtnYY.addActionListener(this);
		radioBtnKT=new JRadioButton("楷体",false);
		radioBtnKT.setBackground(Color.cyan);
		radioBtnKT.addActionListener(this);
		radioBtnHT=new JRadioButton("黑体",false);
		radioBtnHT.setBackground(Color.cyan);
		radioBtnHT.addActionListener(this);
		radioBtnLS=new JRadioButton("隶书",false);
		radioBtnLS.setBackground(Color.cyan);
		radioBtnLS.addActionListener(this);
		group1.add(radioBtnST);
		group1.add(radioBtnYY);
		group1.add(radioBtnKT);
		group1.add(radioBtnHT);
		group1.add(radioBtnLS);
		
		group2=new ButtonGroup();
		radioBtnPlain=new JRadioButton("一般",true);
		radioBtnPlain.setBackground(Color.pink);
		radioBtnPlain.addActionListener(this);
		radioBtnBold=new JRadioButton("粗体",false);
		radioBtnBold.setBackground(Color.pink);
		radioBtnBold.addActionListener(this);
		radioBtnItalic=new JRadioButton("斜体",false);
		radioBtnItalic.setBackground(Color.pink);
		radioBtnItalic.addActionListener(this);
		radioBtnBI=new JRadioButton("粗斜体",false);
		radioBtnBI.setBackground(Color.pink);
		radioBtnBI.addActionListener(this);
		group2.add(radioBtnPlain);
		group2.add(radioBtnBold);
		group2.add(radioBtnItalic);
		group2.add(radioBtnBI);
		
		String[] strItem={"字体大小8","字体大小9","字体大小10","字体大小11",
						  "字体大小12","字体大小13","字体大小14","字体大小15"};
		comboBox=new JComboBox(strItem);
		comboBox.addActionListener(this);
		comboBox.setSelectedIndex(1);
		
		btnOK=new JButton("确定");
		btnOK.addActionListener(this);
		btnCancle=new JButton("取消");
		btnCancle.addActionListener(this);
		
		Container cp;
		cp=getContentPane();
		
		GridLayout layout=new GridLayout(3,4,5,5);
		cp.setLayout(layout);
		
		cp.add(radioBtnST);
		cp.add(radioBtnYY);
		cp.add(radioBtnKT);
		cp.add(radioBtnHT);
		cp.add(radioBtnLS);
		cp.add(radioBtnPlain);
		cp.add(radioBtnBold);
		cp.add(radioBtnItalic);
		cp.add(radioBtnBI);
		cp.add(comboBox);
		cp.add(btnOK);
		cp.add(btnCancle);
	}
	public void showWindow(boolean b)
	{
		if(b)
		  setVisible(true);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==radioBtnST)
		{
			tempStrFont="宋体";
		}
		else if(e.getSource()==radioBtnYY)
		{
			tempStrFont="幼圆";
		}
		else if(e.getSource()==radioBtnKT)
		{
			tempStrFont="楷体";
		}
		else if(e.getSource()==radioBtnHT)
		{
			tempStrFont="黑体";
		}
		else if(e.getSource()==radioBtnLS)
		{
			tempStrFont="隶书";
		}
		else if(e.getSource()==radioBtnBold)
		{
			tempStyle=Font.BOLD;
		}
		else if(e.getSource()==radioBtnBI)
		{
			tempStyle=Font.BOLD+Font.ITALIC;
		}
		else if(e.getSource()==radioBtnItalic)
		{
			tempStyle=Font.ITALIC;
		}
		else if(e.getSource()==radioBtnPlain)
		{
			tempStyle=Font.PLAIN;
		}
		else if(e.getSource()==comboBox)
		{
			tempSize=15+comboBox.getSelectedIndex();
		}
		else if(e.getSource()==btnOK)
		{
			strFont=tempStrFont;
			style=tempStyle;
			size=tempSize;
		}
		else if(e.getSource()==btnCancle)
		{
			setVisible(false);
		}
	}
	
	public int getFontStyle()
	{
		return style;
	}
	
	public int getFontSize()
	{
		return size;
	}
	
	public String getStrFont()
	{
		return strFont;
	}
}
//**************************************************************************
class MyNote
{
	public static void main(String args[])
	{
		NoteBook noteBook=new NoteBook();
	}
	
}

⌨️ 快捷键说明

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