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

📄 一个记事本.txt

📁 一个类似于WINDOWS记事本的功能
💻 TXT
字号:
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*;
import java.io.*; 
import java.lang.*; 
import java.text.*; 

/***主类***********************************************************************/
class jb implements ActionListener 
{ 
Dialog bb;
String strt;                            //定义各变量
int i;  
FileDialog fd; 
File file; 
public Frame f; 
public JTextArea p1;
public MenuBar menubar; 
public Menu menu1,menu2,menu3,menu4,color; 
public MenuItem item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,
item11,item12,item13,item14,item15,item16,item17,item18,item19,item20,font; 

////////////////////////////////////////////////////////////////////////////////
jb(String s) 
{ 
i=0;                                    //初始化各变量
f=new Frame(s); 
f.setBackground(Color.white); 
p1=new JTextArea(15,50); 
p1.setFont(new Font("宋体",Font.PLAIN,14));
f.addWindowListener(new WindowAdapter() //窗口对象的事件监听器
{ 
public void windowClosing(WindowEvent e) 
{
f.setVisible(false);System.exit(0);
}
}); 
                                         
menubar=new MenuBar();                  //生成菜单组件

menu1=new Menu("文件"); 
item1=new MenuItem("新建");  
item2=new MenuItem("打开"); 
item3=new MenuItem("保存"); 
item4=new MenuItem("另存为"); 
item5=new MenuItem("退出"); 

menu2=new Menu("编辑");
item6=new MenuItem("全选"); 
item7=new MenuItem("复制"); 
item8=new MenuItem("剪切"); 
item9=new MenuItem("粘贴");
 
menu3=new Menu("格式");
color=new Menu("颜色");
font=new MenuItem("字体...");
item10=new MenuItem("black");
item11=new MenuItem("yellow");
item12=new MenuItem("gray");
item13=new MenuItem("green");
item14=new MenuItem("pink");
item15=new MenuItem("orange");
item16=new MenuItem("cyan");
item17=new MenuItem("red");
item18=new MenuItem("blue");
item19=new MenuItem("magenta");

menu4=new Menu("帮助"); 
item20=new MenuItem("关于");
 
menu1.add(item1); 
menu1.add(item2); 
menu1.add(item3); 
menu1.add(item4); 
menu1.add(item5);
 
menu2.add(item6); 
menu2.add(item7); 
menu2.add(item8); 
menu2.add(item9);   

menu3.add(color);
menu3.add(font);

color.add(item10);
color.add(item11);
color.add(item12);
color.add(item13);
color.add(item14);
color.add(item15); 
color.add(item16);
color.add(item17);
color.add(item18);
color.add(item19); 

menu4.add(item20);

menubar.add(menu1); 
menubar.add(menu2); 
menubar.add(menu3);
menubar.add(menu4);

f.setMenuBar(menubar); 
item1.addActionListener(this);           // 注册菜单项的事件监听器
item2.addActionListener(this); 
item3.addActionListener(this); 
item4.addActionListener(this); 
item5.addActionListener(this); 
item6.addActionListener(this); 
item7.addActionListener(this); 
item8.addActionListener(this); 
item9.addActionListener(this); 
item10.addActionListener(this);   
item11.addActionListener(this);
item12.addActionListener(this);
item13.addActionListener(this);
item14.addActionListener(this);
item15.addActionListener(this);
item16.addActionListener(this);
item17.addActionListener(this);
item18.addActionListener(this);
item19.addActionListener(this);
item20.addActionListener(this); 
font.addActionListener(this);
  
f.setLayout(new GridLayout(1,1)); 
f.add(p1); 
f.pack();
f.setVisible(true); 
} 

/***行为事件*******************************************************************/ 
public void actionPerformed(ActionEvent e) 
{ 
String ss; 
ss=p1.getText().trim(); 

////退出////////////////////////////////////////////////////////////////////////
if (e.getSource()==item5)                
{ 
if (i==0&&(ss.length()!=0))             //如果没有保存并且文档中有文字
{                                        //的话就出现保存对话框然后退出 
bc(); 
System.exit(0);
} 
else                                     //其他情况直接退出
{ 
System.exit(0); 
} 
} 

////新建//////////////////////////////////////////////////////////////////////// 
if (e.getSource()==item1)                
{ 
bc();p1.setText("");i=0;
}
 
////打开//////////////////////////////////////////////////////////////////////// 
if (e.getSource()==item2)                
{ 
fd=new FileDialog(f,"打开文件",0);       //创建一个打开文件的对话框 
fd.setVisible(true); 
try{ 
file=new File(fd.getDirectory(),fd.getFile()); 
f.setTitle(fd.getFile()+"--记事本"); 
FileReader fr=new FileReader(file);      //建立一个来自文件的输入流 
BufferedReader br=new BufferedReader(fr); 
String line = null; 
String view = ""; 
while((line=br.readLine())!=null) 
{ 
view += line+"\n"; 
} 
p1.setText(view); 
br.close();                              //关闭流
fr.close(); 
i=1;
} 
catch(IOException expIn) 
{} 
} 

////保存//////////////////////////////////////////////////////////////////////// 
if (e.getSource()==item3)                
{ 
if (i==0)                                
{                                        //如果之前从未新建则弹出保存对话框保存
bc(); 
} 
else                                     //其他情况则直接保存
{ 
try{ 
file=new File(fd.getDirectory(),fd.getFile()); 
f.setTitle(fd.getFile()+"--记事本"); 
FileWriter fw=new FileWriter(file);      //建立一个通向文件的输入流 
BufferedWriter bw=new BufferedWriter(fw); 
String s =p1.getText(); 
s = s.replaceAll("\n","\r\n"); 
bw.write(s); 
bw.flush(); 
bw.close();                              //关闭流
fw.close(); 
i=1; 
} 
catch(IOException expOut){i=0;} 
} 
} 

////另存为//////////////////////////////////////////////////////////////////////
if (e.getSource()==item4)                
{ 
bc(); 
} 

////帮助:关于作者////////////////////////////////////////////////////////////// 
if (e.getSource()==item20)               
{ 
bb=new Dialog(f,"关于"); 
Label l1=new Label("计算机0409班 丁小芩 于2006年6月制作"); 
bb.add(l1); 
bb.setSize(250,150); 
bb.setBackground(Color.white); 
bb.show(); 
bb.addWindowListener(new WindowAdapter() 
{ 
public void windowClosing(WindowEvent e) 
{bb.setVisible(false);bb.dispose();} 
}); 
} 

////全选//////////////////////////////////////////////////////////////////////// 
if (e.getSource()==item6)                
{ 
p1.setSelectionStart(0); 
p1.setSelectionEnd(p1.getText().length()); 
} 

////复制//////////////////////////////////////////////////////////////////////// 
if (e.getSource()==item7)               
{ 
try{ 
String str=p1.getSelectedText(); 
if(str.length()!=0) 
{ 
strt=str; 
} 
}catch(Exception ex) 
{} 
} 

////剪切////////////////////////////////////////////////////////////////////////
if (e.getSource()==item8)                
{ 
try{ 
String str=p1.getSelectedText(); 
if(str.length()!=0) 
{ strt=str;
p1.replaceRange("",p1.getSelectionStart(),p1.getSelectionEnd()); 
} 
} 
catch(Exception ex) 
{} 
} 

////粘贴////////////////////////////////////////////////////////////////////////
if (e.getSource()==item9)                
{ 
if(strt.length()>0) 
{ 
p1.insert(strt,p1.getCaretPosition()); 
} 
} 

////颜色设置////////////////////////////////////////////////////////////////////
if (e.getSource()==item10)              //颜色设置成黑色
{
p1.setForeground(Color.black);
}

if (e.getSource()==item11)              //颜色设置成黄色
{
p1.setForeground(Color.yellow);
}

if (e.getSource()==item12)              //颜色设置成灰色
{
p1.setForeground(Color.gray);
}

if (e.getSource()==item13)              //颜色设置成绿色
{
p1.setForeground(Color.green);
}

if (e.getSource()==item14)              //颜色设置成粉色
{
p1.setForeground(Color.pink);
}

if (e.getSource()==item15)              //颜色设置成橙色
{
p1.setForeground(Color.orange);
}

if (e.getSource()==item16)              //颜色设置成青色
{
p1.setForeground(Color.cyan);
}

if (e.getSource()==item17)              //颜色设置成红色
{
p1.setForeground(Color.red);
}

if (e.getSource()==item18)              //颜色设置成蓝色
{
p1.setForeground(Color.blue);
}

if (e.getSource()==item19)              //颜色设置成洋红色
{
p1.setForeground(Color.magenta);
}

////字体设置////////////////////////////////////////////////////////////////////       ////
if (e.getSource()==font)                
{
new Newfont(p1);
}
} 

////保存函数////////////////////////////////////////////////////////////////////
public void bc()                        
{ 
fd=new FileDialog(f,"保存文件",1);      //创建一个保存文件的对话框 
fd.setVisible(true); 
try{ 
file=new File(fd.getDirectory(),fd.getFile()); 
f.setTitle(fd.getFile()+"--记事本"); 
FileWriter fw=new FileWriter(file);     //建立一个通向文件的输入流 
BufferedWriter bw=new BufferedWriter(fw);//封装输入流以提高效率 
String s =p1.getText(); 
s = s.replaceAll("\n","\r\n"); 
bw.write(s); 
bw.flush(); 
bw.close();                             //关闭流 
fw.close(); 
i=1; 
} 
catch(IOException expOut){i=0;} 
} 

////字体类//////////////////////////////////////////////////////////////////////
public class Newfont extends JDialog 
{ 
JPanel contentPane;                     //字体对话框各菜单项内容
JLabel jLabel1 = new JLabel(); 
String[] data1 = {"Arial","Verdana", 
"Serif","Impact","宋体",
"黑体","仿宋_GB2312","楷体_GB2312"}; 
JList jList1 = new JList(data1); 

JLabel jLabel2 = new JLabel(); 
String[] data2 = {"常规","斜体","粗体","粗斜体"}; 
JList jList2 = new JList(data2); 

JLabel jLabel3 = new JLabel(); 
String[] data3 = {"14","16","18","20","22","24","26"}; 
JList jList3 = new JList(data3); 

JTextField jTextField1 = new JTextField("Arial"); 
JTextField jTextField2 = new JTextField("常规"); 
JTextField jTextField3 = new JTextField("14"); 

JButton jButton1 = new JButton(); 
JButton jButton2 = new JButton(); 

int index;                             //定义变量
String modal; 
int style;
int size; 

////字体设置函数////////////////////////////////////////////////////////////////
public Newfont(JTextArea p1)              
{ 
enableEvents(AWTEvent.WINDOW_EVENT_MASK); 
try 
{ 
jbInit(); 
} 
catch(Exception e) 
{ 
e.printStackTrace(); 
} 
} 

////对话框设计函数//////////////////////////////////////////////////////////////
private void jbInit() throws Exception 
{ 
contentPane = (JPanel) this.getContentPane(); 
contentPane.setLayout(null); 
this.setFont(new Font("宋体", Font.PLAIN, 14));
this.getContentPane().setBackground(Color.white); 
this.setSize(new Dimension(450,320)); 
this.setTitle("字体"); 
this.setModal(true); 

jLabel1.setText("字体"); 
jLabel1.setBounds(new Rectangle(11, 10, 65, 23)); 
jTextField1.setBounds(new Rectangle(8, 40, 138, 20)); 
jList1.setSelectedIndex(0); 
jList1.setBounds(new Rectangle(8, 61, 138, 160)); 

jLabel2.setText("风格"); 
jLabel2.setBounds(new Rectangle(158, 10, 65, 23)); 
jTextField2.setBounds(new Rectangle(152, 40, 109, 20)); 
jList2.setSelectedIndex(0); 
jList2.setBounds(new Rectangle(152, 61, 109, 115)); 

jLabel3.setText("大小"); 
jLabel3.setBounds(new Rectangle(273, 10, 65, 23)); 
jTextField3.setBounds(new Rectangle(267, 40, 66, 20)); 
jList3.setSelectedIndex(0); 
jList3.setBounds(new Rectangle(267, 61, 66, 115)); 

jButton1.setBounds(new Rectangle(344, 38, 81, 26)); 
jButton1.setText("确定"); 

jButton2.setBounds(new Rectangle(344, 83, 81, 26)); 
jButton2.setText("取消"); 

contentPane.add(jLabel1, null); 
contentPane.add(jLabel2, null);   
contentPane.add(jLabel3, null); 
contentPane.add(jList1, null); 
contentPane.add(jList2, null); 
contentPane.add(jList3, null);  
contentPane.add(jTextField1, null); 
contentPane.add(jTextField2, null); 
contentPane.add(jTextField3, null); 
contentPane.add(jButton1, null); 
contentPane.add(jButton2, null);
 
Button1Listener ok=new Button1Listener();        //按钮事件监听器
jButton1.addActionListener(ok); 

Button2Listener cancel=new Button2Listener(); 
jButton2.addActionListener(cancel); 

MouseListener mouseListener1 = new MouseAdapter()//字体设置
{ 
public void mouseClicked(MouseEvent e)
{ 
if (e.getClickCount() == 1) 
{ 
index = jList1.locationToIndex(e.getPoint()); 
jList1.setSelectedIndex(index); 
jTextField1.setText(jList1.getSelectedValue().toString()); 
modal=jTextField1.getText();
} 
} 
};

MouseListener mouseListener2 = new MouseAdapter()//风格设置 
{ 
public void mouseClicked(MouseEvent e) 
{
if (e.getClickCount() == 1) 
{ 
index = jList2.locationToIndex(e.getPoint()); 
jList2.setSelectedIndex(index); 
String zip=jList2.getSelectedValue().toString(); 
jTextField2.setText(zip); 
if(zip.compareTo("常规")==0)
{ 
style=Font.PLAIN; 
} 
else if(zip.compareTo("斜体")==0)
{ 
style=Font.ITALIC; 
} 
else if(zip.compareTo("粗体")==0)
{ 
style=Font.BOLD; 
} 
else if(zip.compareTo("粗斜体")==0)
{ 
style=Font.BOLD+Font.ITALIC; 
}  
} 
} 
}; 

MouseListener mouseListener3 = new MouseAdapter()//大小设置 
{ 
public void mouseClicked(MouseEvent e) 
{ 
if (e.getClickCount() == 1) 
{ 
index = jList3.locationToIndex(e.getPoint()); 
jList3.setSelectedIndex(index); 
jTextField3.setText(jList3.getSelectedValue().toString()); 
size=Integer.parseInt(jTextField3.getText());   
} 
} 
}; 

jList1.addMouseListener(mouseListener1);         //鼠标事件监听器
jList2.addMouseListener(mouseListener2); 
jList3.addMouseListener(mouseListener3); 
show(); 
} 

void sfont()
{	
p1.setDragEnabled(true);  
p1.setFont(new Font(modal,style,size));  
} 
 
class Button1Listener implements ActionListener  //确定按钮设置
{ 
public void actionPerformed(ActionEvent e)
{ 
sfont(); 
hide(); 
} 
} 

class Button2Listener implements ActionListener  //取消按钮设置
{ 
public void actionPerformed(ActionEvent e)
{ 
hide(); 
} 
} 

public void processWindowEvent(WindowEvent e) 
{ 
super.processWindowEvent(e); 
if(e.getID() == WindowEvent.WINDOW_CLOSED) 
{ 
System.exit(0); 
} 
} 
}
} 

/***主函数*********************************************************************/
public class jsb 
{ public static void main(String args[]) 
{jb dd=new jb("我的记事本");} 
} 

⌨️ 快捷键说明

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