通过对java的学习,初步完成了记事本的编写,希望java记事本代码能对需要的同学有所帮助!
/**
* @author andy
* @function:实现记事本的新建、打开、保存、另存为、剪切、复制、粘贴、删除和全选、查找和替换、字体和颜色、关于、退出
*/
package com.dream.jsq;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Rectangle;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import java.awt.Dimension;
import java.awt.Point;
public class Jsq extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JMenuBar MB = null;
private JMenu M1 = null;
private JMenu M2 = null;
private JMenu M3 = null;
private JMenu M4 = null;
private JMenu M5 = null;
private JMenuItem MI11 = null;
private JMenuItem MI12 = null;
private JMenuItem MI13 = null;
private JMenuItem MI14 = null;
private JMenuItem MI21 = null;
private JMenuItem MI22 = null;
private JMenuItem MI23 = null;
private JMenuItem MI24 = null;
private JMenuItem MI25 = null;
private JMenuItem MI26 = null;
private JMenuItem MI51 = null;
private JScrollPane SPane = null;
private JTextArea TA = null;
Toolkit toolKit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolKit.getSystemClipboard();
Component bar=null;
private JMenuItem MI31 = null;
private JMenuItem MI32 = null;
/**
*定义菜单栏,再上面添加菜单
*
*
*/
private JMenuBar getMB() {
if (MB == null) {
MB = new JMenuBar();
MB.add(getM1());
MB.add(getM2());
MB.add(getM3());
MB.add(getM4());
MB.add(getM5());
}
return MB;
}
/**
* 定义“文件”菜单,添加菜单项
*
* @return javax.swing.JMenu
*/
private JMenu getM1() {
if (M1 == null) {
M1 = new JMenu();
M1.setText("文件");
M1.add(getMI11());
M1.add(getMI12());
M1.add(getMI13());
M1.add(getMI14());
}
return M1;
}
/**
* 定义“编辑”菜单,添加菜单项
*
* @return javax.swing.JMenu
*/
private JMenu getM2() {
if (M2 == null) {
M2 = new JMenu();
M2.setText("编辑");
M2.add(getMI21());
M2.add(getMI22());
M2.add(getMI23());
M2.add(getMI24());
M2.add(getMI25());
M2.add(getMI26());
}
return M2;
}
/**
* 定义“格式”菜单,添加菜单项
*
* @return javax.swing.JMenu
*/
private JMenu getM3() {
if (M3 == null) {
M3 = new JMenu();
M3.setText("格式");
M3.add(getMI31());
M3.add(getMI32());
}
return M3;
}
/**
* 定义“查看”,添加菜单项
*
* @return javax.swing.JMenu
*/
private JMenu getM4() {
if (M4 == null) {
M4 = new JMenu();
M4.setText("查看");
}
return M4;
}
/**
* 定义“帮助”菜单,添加菜单项
*
* @return javax.swing.JMenu
*/
private JMenu getM5() {
if (M5 == null) {
M5 = new JMenu();
M5.setText("帮助");
M5.add(getMI51());
}
return M5;
}
/**
* This method initializes MI11
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI11() {
if (MI11 == null) {
MI11 = new JMenuItem();
MI11.setText("新建");
MI11.addActionListener(this);
}
return MI11;
}
/**
* 定义“打开”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI12() {
if (MI12 == null) {
MI12 = new JMenuItem();
MI12.setText("打开");
MI12.addActionListener(this);
}
return MI12;
}
/**
* 定义“保存”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI13() {
if (MI13 == null) {
MI13 = new JMenuItem();
MI13.setText("保存");
MI13.addActionListener(this);
}
return MI13;
}
/**
* 定义“退出”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI14() {
if (MI14 == null) {
MI14 = new JMenuItem();
MI14.setText("退出");
MI14.addActionListener(this);
}
return MI14;
}
/**
*定义“剪切”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI21() {
if (MI21 == null) {
MI21 = new JMenuItem();
MI21.setText("剪切");
MI21.addActionListener(this);
}
return MI21;
}
/**
* 定义“复制”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI22() {
if (MI22 == null) {
MI22 = new JMenuItem();
MI22.setText("复制");
MI22.addActionListener(this);
}
return MI22;
}
/**
* 定义“粘贴”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI23() {
if (MI23 == null) {
MI23 = new JMenuItem();
MI23.setText("粘贴");
MI23.addActionListener(this);
}
return MI23;
}
/**
* 定义“全选”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI24() {
if (MI24 == null) {
MI24 = new JMenuItem();
MI24.setText("全选");
MI24.addActionListener(this);
}
return MI24;
}
/**
* 定义“查看”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI25() {
if (MI25 == null) {
MI25 = new JMenuItem();
MI25.setText("查找");
MI25.addActionListener(this);
}
return MI25;
}
/**
* 定义“替换”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI26() {
if (MI26 == null) {
MI26 = new JMenuItem();
MI26.setText("替换");
MI26.addActionListener(this);
}
return MI26;
}
/**
* 定义“关于”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI51() {
if (MI51 == null) {
MI51 = new JMenuItem();
MI51.setText("关于");
MI51.addActionListener(this);
}
return MI51;
}
/**
* This method initializes SPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getSPane() {
if (SPane == null) {
SPane = new JScrollPane();
SPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
SPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
SPane.setViewportView(getTA());
}
return SPane;
}
/**
* 添加文本域
*
* @return javax.swing.JTextArea
*/
private JTextArea getTA() {
if (TA == null) {
TA = new JTextArea();
}
return TA;
}
/**
*定义“颜色”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI31() {
if (MI31 == null) {
MI31 = new JMenuItem();
MI31.setText("颜色");
MI31.addActionListener(this);
}
return MI31;
}
/**
* 定义“字体”菜单项
*
* @return javax.swing.JMenuItem
*/
private JMenuItem getMI32() {
if (MI32 == null) {
MI32 = new JMenuItem();
MI32.setText("字体");
MI32.addActionListener(this);
}
return MI32;
}
/**
* 主函数
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Jsq thisClass = new Jsq();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* 函数构造器
*/
public Jsq() {
super();
initialize();
}
/**
* 窗体属性
*
* @return void
*/
private void initialize() {
this.setContentPane(getJContentPane());
this.setJMenuBar(getMB());
this.setTitle("notepad");
this.setLocation(new Point(360, 200));
this.setSize(new Dimension(450, 360));
}
/**
* 初始BorderLayout布局
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getSPane(), BorderLayout.CENTER);
}
return jContentPane;
}
//查找与替换的方法
private void index(String str,String rep){
int i = -1, j = JOptionPane.YES_OPTION;
if (str != null) {
while (TA.getText().indexOf(str, i + 1) != -1
&& j == JOptionPane.YES_OPTION) {
i = TA.getText().indexOf(str, i + 1);
TA.setSelectionStart(i);
TA.setSelectionEnd(i + str.length());
if (!str.equals(rep)) {
TA.replaceSelection(rep);
}
j = JOptionPane.showConfirmDialog(bar, "查找下一个?");
}
if (j != JOptionPane.YES_OPTION || i == -1) {
return;
}
} else {
return;
}
}
//打开方法
private void open(){
JFileChooser fc=new JFileChooser();
fc.setDialogTitle("打开文件");
fc.showOpenDialog(null);
String filename=fc.getSelectedFile().getAbsolutePath();
FileReader fr=null;
BufferedReader br=null;
try{
fr=new FileReader(filename);
br=new BufferedReader(fr);
String st="";
String all="";
while((st=br.readLine())!=null){
all+=st+"\r\n";
}
TA.setText(all);
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
try{
br.close();
fr.close();
}
catch(Exception exe){
}
}
}
//保存方法
private void save(){
JFileChooser fc1=new JFileChooser();
fc1.setDialogTitle("另存为");
fc1.showOpenDialog(null);
String filename=fc1.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;
try{
fw=new FileWriter(filename);
bw=new BufferedWriter(fw);
bw.write(TA.getText());
}
catch(Exception ex){
ex.printStackTrace();
}
finally{
try {
bw.close();
fw.close();
} catch (IOException ex) {
// TODO 自动生成 catch 块
ex.printStackTrace();
}
}
}
//复制方法
private void copy(){
String s = TA.getSelectedText();
StringSelection st= new StringSelection(s);
clipboard.setContents(st,null);
}
//粘贴方法
private void paste(){
Transferable tf = clipboard.getContents(this);
if(tf==null)
return;
String s="";
try{
s = (String)tf.getTransferData(DataFlavor.stringFlavor);
}
catch(Exception ex){
ex.printStackTrace();
}
TA.replaceRange(s,TA.getSelectionStart(),TA.getSelectionEnd());
}
//剪切方法
private void cut(){
String text = TA.getSelectedText();
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection,null);
TA.replaceRange("",TA.getSelectionStart(), TA.getSelectionEnd());
}
//查找方法
private void found(){
String str = JOptionPane.showInputDialog(bar, "请输入查找字符:", "查找",JOptionPane.YES_NO_OPTION);
index(str, str);
}
//替换方法
private void replace(){
String str1 = JOptionPane.showInputDialog(bar, "请输入查找字符:", "替换",JOptionPane.YES_NO_OPTION);
String str2 = JOptionPane.showInputDialog(bar, "请输入替换字符:", "替换",JOptionPane.YES_NO_OPTION);
index(str1, str2);
}
//字体颜色方法
private void color(){
Color c = JColorChooser.showDialog(this, "请选择文字颜色", Color.cyan);
if (c != null)
{
TA.setForeground(c);
}
else
TA.setForeground(TA.getForeground());
}
//字体方法
private void font(){
Font f=FontReplace.showDialog(null, "请选择");
TA.setFont(f);
}
//对事件进行处理
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
String s=e.getActionCommand();
if(s.equals("新建"))
TA.setText("");
else if(s.equals("打开"))
open();
else if(s.equals("保存"))
save();
else if(s.equals("退出"))
dispose();
else if(s.equals("剪切"))
cut();
else if(s.equals("复制"))
copy();
else if(s.equals("粘贴"))
paste();
else if(s.equals("全选"))
TA.selectAll();
else if(s.equals("查找"))
found();
else if(s.equals("替换"))
replace();
else if(s.equals("颜色"))
color();
else if(s.equals("字体"))
font();
else if(s.equals("关于"))
JOptionPane.showMessageDialog(null,"");
}
}
//新建FontReplace面板实现字体切换
package com.dream.jsq;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import javax.swing.event.*;
public class FontReplace extends JPanel implements ActionListener,ListSelectionListener{
private JDialog jd;//用于显示模态的窗体
private JComboBox fonts;//用于选择字体的下拉框
private JList face,size;//用于选择字形和字号的列表
private JTextField sizeJT;//用于显示选中的字形和字号
private JButton ok,cancel;//表示选中和取消的按钮
private Font current;//表示当然选中的字体
private GraphicsEnvironment ge;//表示当前的图形环境
private JLabel demo;//表示预览的label
private String fontName= "宋体 ";
private int fontStyle=Font.PLAIN,fontSize=20;
private Frame owner;//表示父类的组件窗体
private Hashtable ht;//名字到大小的映射
/** Creates a new instance of JFontChooser */
private FontReplace() {
initOther();
}
private void initOther(){
current=new Font(fontName,fontStyle,fontSize);
ht=new Hashtable ();
sizeJT=new JTextField( "20 ");
sizeJT.setEditable(false);
sizeJT.setBounds(260,40,50,20);
ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] family=ge.getAvailableFontFamilyNames();
fonts=new JComboBox(family);
fonts.setEditable(false);
fonts.setMaximumRowCount(5);
demo=new JLabel( "Hello Java",JLabel.CENTER);
demo.setFont(current);
String[] faceString={ "正常 ", "粗体 ", "斜体 ", "粗体+斜体 "};
String[] sizeString={ "初号 ", "小初 ", "一号 ", "小一 ", "二号 ", "小二 ",
"三号 ", "小三 ", "四号 ", "小四 ", "五号 ", "小五 ", "六号 ", "小六 ", "七号 ",
"八号 ", "5 ", "8 ", "9 ", "10 ", "11 ", "12 ", "14 ", "16 ", "18 ", "20 ", "22 ", "24 ",
"26 ", "28 ", "36 ", "48 ", "72 "};
int[] sizeValue={42,36,26,24,22,18,16,15,14,12,11,9,7,6,5,4,5,8,9,10,11,12,14,16,18,20,
22,24,26,28,36,48,72};
for(int i=0;i <sizeString.length;i++){
ht.put(sizeString[i],new Integer(sizeValue[i]));
}
face=new JList(faceString);
size=new JList(sizeString);
face.setSelectedIndex(0);
size.setSelectedIndex(25);
fonts.setSelectedItem( "宋体 ");
face.setVisibleRowCount(4);
size.setVisibleRowCount(4);
ok=new JButton( "确定 ");
cancel=new JButton( "取消 ");
ok.addActionListener(this);
cancel.addActionListener(this);
fonts.addActionListener(this);
face.addListSelectionListener(this);
size.addListSelectionListener(this);
}
private void initWindow(String title){
this.setLayout(new BorderLayout());
JLabel fontLabel=new JLabel( "字体 ");
JLabel faceLabel=new JLabel( "字形 ");
JLabel sizeLabel=new JLabel( "字号 ");
fontLabel.setForeground(Color.RED);
faceLabel.setForeground(Color.RED);
sizeLabel.setForeground(Color.RED);
fontLabel.setBounds(20,20,100,20);
faceLabel.setBounds(180,20,80,20);
sizeLabel.setBounds(260,20,50,20);
fonts.setBounds(10,40,127,21);
JScrollPane faceScroll=new JScrollPane(face);
JScrollPane sizeScroll=new JScrollPane(size);
faceScroll.setBounds(180,40,65,100);
sizeScroll.setBounds(260,60,50,80);
JPanel up=new JPanel(null);
JPanel center=new JPanel(new BorderLayout());
JPanel bottom=new JPanel();
up.setPreferredSize(new Dimension(345,160));
up.add(fontLabel);
up.add(faceLabel);
up.add(sizeLabel);
up.add(fonts);
up.add(faceScroll);
up.add(sizeScroll);
up.add(sizeJT);
up.setBorder(BorderFactory.createTitledBorder( "选择区 "));
center.add(demo,BorderLayout.CENTER);
center.setBorder(BorderFactory.createTitledBorder( "预览区 "));
bottom.add(ok);
bottom.add(cancel);
this.add(up,BorderLayout.NORTH);
this.add(center,BorderLayout.CENTER);
this.add(bottom,BorderLayout.SOUTH);
jd=new JDialog(owner,title,true);
jd.getContentPane().add(this,BorderLayout.CENTER);
jd.setSize(360,360);
jd.setResizable(false);
jd.setLocationRelativeTo(owner);
jd.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
current=null;
jd.dispose();
}
});
jd.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==fonts){
fontName=(String)fonts.getSelectedItem();
current=new Font(fontName,fontStyle,fontSize);
demo.setFont(current);
this.repaint();
}else if(ae.getSource()==ok){
jd.dispose();
}else if(ae.getSource()==cancel){
current=null;
jd.dispose();
}
}
public void valueChanged(ListSelectionEvent le){
if(le.getSource()==face){
String value=(String)face.getSelectedValue();
if(value.equals( "正常 ")){
fontStyle=Font.PLAIN;
}else if(value.equals( "粗体 ")){
fontStyle=Font.BOLD;
}else if(value.equals( "斜体 ")){
fontStyle=Font.ITALIC;
}else if(value.equals( "粗体+斜体 ")){
fontStyle=Font.ITALIC|Font.BOLD;
}
current=new Font(fontName,fontStyle,fontSize);
demo.setFont(current);
this.repaint();
}else if(le.getSource()==size){
String sizeName=(String)size.getSelectedValue();
sizeJT.setText(sizeName);
fontSize=((Integer)ht.get(sizeName)).intValue();
current=new Font(fontName,fontStyle,fontSize);
demo.setFont(current);
this.repaint();
}
}
public static Font showDialog(Frame owner,String title){
FontReplace jf=new FontReplace();
jf.initWindow(title);
return jf.current;
}
public static void main(String[] args) {
System.out.println(FontReplace.showDialog(null, "请选择 "));
}
}