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

📄 w1.java

📁 实现记事本基本功能!包括字体大小
💻 JAVA
字号:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.applet.Applet;
import java.lang.*;


class W1 extends JFrame implements ActionListener ,MouseListener{

FileDialog fileDlg;
String str,fileName;
byte byteBuf[]=new byte[10000];
JDialog a= new JDialog(this); 
 
Color c;

  

JTextArea ta=new JTextArea();

MenuBar mb=new MenuBar();//生成菜单组件对象

Menu m1=new Menu("文件");
MenuItem open=new MenuItem("打开",new MenuShortcut('O'));//快捷方式
MenuItem close=new MenuItem("关闭",new MenuShortcut('X'));
MenuItem save=new MenuItem("保存",new MenuShortcut('S'));
MenuItem exit=new MenuItem("退出",new MenuShortcut('Q'));

Menu m2=new Menu("编辑");
MenuItem copy=new MenuItem("复制",new MenuShortcut('C'));
MenuItem cut=new MenuItem("剪切",new MenuShortcut('T'));
MenuItem paste=new MenuItem("粘贴",new MenuShortcut('P'));
MenuItem selectall=new MenuItem("全选",new MenuShortcut('A'));

Menu m3=new Menu("格式");
MenuItem changergrow=new MenuItem("自动换行");
MenuItem font=new MenuItem("字体",new MenuShortcut('F'));
MenuItem color=new MenuItem("颜色",new MenuShortcut('R'));

Menu m4=new Menu("帮助");
MenuItem about=new MenuItem("关于");
MenuItem time=new MenuItem("时间",new MenuShortcut('M'));


PopupMenu m6=new PopupMenu();
MenuItem copy1=new MenuItem("复制",new MenuShortcut('C'));
MenuItem cut1=new MenuItem("剪切",new MenuShortcut('T'));
MenuItem paste1=new MenuItem("粘贴",new MenuShortcut('P'));
MenuItem selectall1=new MenuItem("全选",new MenuShortcut('A'));
MenuItem font1=new MenuItem("字体",new MenuShortcut('F'));
MenuItem color1=new MenuItem("颜色",new MenuShortcut('R'));


W1(){
    super("guojing");//生成窗口必须的(名,宽,高,窗口可见,注册事件监听器)
    setSize(500,600);
    add("Center",ta);
   
    addWindowListener(new Win());//继承子类Win;
    
    m1.add(open);//将菜单项加入到菜单中
    m1.add(close);
    m1.add(save);
    m1.addSeparator();//将分隔条加入到菜单中
    m1.add(exit);
    open.addActionListener(this);
    close.addActionListener(this);
    save.addActionListener(this);
    exit.addActionListener(this);//注册菜单项的事件监听器
    mb.add(m1);//将菜单加入到菜单条中

    m2.add(copy);
    m2.add(cut);
    m2.add(paste);
    m2.add(selectall);
    m2.add(time);
    copy.addActionListener(this);
    cut.addActionListener(this);
    paste.addActionListener(this);
    selectall.addActionListener(this);
    time.addActionListener(this);
    mb.add(m2);
    

    m3.add(font);
    m3.add(color);
    m3.add(changergrow);
    font.addActionListener(this);
    color.addActionListener(this);
    changergrow.addActionListener(this);
    mb.add(m3);
    
    m4.add(about);
    about.addActionListener(this);
    mb.add(m4);
  
    
    
    
    m6.add(copy1);
    m6.add(cut1);
    m6.add(paste1);
    m6.add(selectall1);
    m6.add(font1);
    m6.add(color1);
    copy1.addActionListener(this);
    cut1.addActionListener(this);
    paste1.addActionListener(this);
    selectall1.addActionListener(this);
    font1.addActionListener(this);
    color1.addActionListener(this);
    ta.addMouseListener(this);//注册文本区的鼠标事件监听器
    add(m6);//将弹出式菜单加到文本编辑器
    setMenuBar(mb);//显示菜单条
    
    
      
    setVisible(true);//或show()方法必须位于添加组件语句之后
     
  }


/************************************************
public void windowOpened(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
*************************************************/



public void actionPerformed(ActionEvent e){
  if(e.getSource()==exit)
    System.exit(0);
  else if(e.getSource()==close)//关闭文件
    ta.setText(null);         //设文本区为空
  else if(e.getSource()==open){//打开文件
    fileDlg=new FileDialog(this,"打开文件");//生成文件对话框
    fileDlg.show();                        //显示文件对话框
    fileName=fileDlg.getFile();            //获取文件名
    try{
     FileInputStream in=new FileInputStream(fileName);//建立文件输入流
     in.read(byteBuf);                                //将文件内容读到字节数组  
     in.close();                                      //关闭文件输入流
     str=new String(byteBuf);                         //将字节数组转换为字符串
     ta.setText(str);
     setTitle("guojing"+fileName);
     }
     catch(IOException ioe){}
     }
    else if(e.getSource()==save){            //保存文件
      fileDlg=new FileDialog(this,"保存文件",FileDialog.SAVE);//生成文件对话框
       fileDlg.show();
       fileName=fileDlg.getFile();
       str=ta.getText();                     //将文本区内容读入字符串
       byteBuf=str.getBytes();        //将字符串转换成字节数组
       try{
          FileOutputStream out=new FileOutputStream(fileName);//建立文件输出流
          out.write(byteBuf);                                 //将字节数组写入文件输出流
          out.close();                                        //关闭文件输出流
         }
        catch(IOException ioe){}
     }
     else if(e.getSource()==about){//关于
      a.setTitle("关于...");
      a.getContentPane().setBackground(Color.cyan);
      a.getContentPane().add(new Label("欢迎使用小小记事本! \t作者:郭晶 \t单位:南京邮电大学\t"));
      a.setModal(true);
      a.setSize(300,100);
      a.show();
     }
     else if(e.getSource()==copy||e.getSource()==copy1)//复制
      {ta.copy();}
     else if(e.getSource()==cut||e.getSource()==cut1)//剪切
      {ta.cut();}
     else if(e.getSource()==paste||e.getSource()==paste1)//粘贴
      {ta.paste();}
     else if(e.getSource()==selectall||e.getSource()==selectall1)//全选
      {ta.selectAll();}
     else if(e.getSource()==time)//时间
      {Date d=new Date();
      ta.setText(d.toString());
      }
     else if(e.getSource()==color||e.getSource()==color1)//颜色
      {
        c=JColorChooser.showDialog(W1.this,"",c);
       ta.setForeground(c);
      }
     else if(e.getSource()==changergrow){            //响应自动换行
     }   
     else if(e.getSource()==font||e.getSource()==font1)                //响应字体
     {new ListDemo();}

}
//字体文本框
//--------------------------------------------------------------
public class ListDemo extends JFrame implements ItemListener,ActionListener{

	
	Button button1=new Button("确定");
    Button button2=new Button("取消");
	Label label1=new Label("字体");
	Label label2=new Label("字形");
	Label label3=new Label("大小");
	Label label4=new Label("示例",Label.CENTER);
	
	List list1=new List();
	//List list2=new List(3,false);
	List list2=new List();
	List list3=new List();
	TextField text1=new TextField();
	TextField text2=new TextField();
	TextField text3=new TextField();
	TextField text4=new TextField();
	
	
	Panel panel=new Panel();
	
	String[] buf1,buf2,buf3;
    //String str=new String("宋体");
    String str=new String();
    String str1=new String();
    String str2=new String();
    int st,si;
    Font fonts=new Font(str,st,si);
public ListDemo(){
       super("字体");
       setSize(450,300);
       init();
		}
	
 public void init(){
		setLayout(null);
		add(label1);//字体
		label1.setBounds(10,0,100,20);
		add( text1);	
		text1.setBounds(10,20,100,25);
	    add(label2);//字形
	    label2.setBounds(120,0,100,20);
	    add( text2);
	    text2.setBounds(120,20,100,25);
		add(label3);//大小
		label3.setBounds(230,0,100,20);
		add( text3);
		text3.setBounds(230,20,100,25);
		
		add(label4);//示例
		label4.setBounds(10,170,120,20);
		add( text4);
		text4.setBounds(10,190,150,35);
		text4.setEditable(false);
		text4.setText("Hello The World");
		text4.setFont(fonts);
		
		
		
		add(button1);//确定
		button1.setBounds(360,60,60,20);
		add(button2);//取消
		button2.setBounds(360,90,60,20);
		

	
	
	    button1.addActionListener(this);
	    button2.addActionListener(this);
	
	
		
		
		add(list1);
		list1.setBounds(10,50,100,100);
		list1.addItemListener(this);
		buf1=new String[]{"宋体","黑体","楷体","仿宋","楷体","a","s","d","t","c"};
		for(int i=0;i<10;i++)list1.add(buf1[i]);

		 
		
		
		add(list2);
		list2.addItemListener(this);
		list2.setBounds(120,50,100,100);
                buf2=new String[]{"常规","斜体","粗体","粗斜体"};
		for(int i=0;i<4;i++)list2.add(buf2[i]);

		
		add(list3);
		list3.addItemListener(this);
		list3.setBounds(230,50,100,100);
		buf3=new String[]{"8","9","10","11","12","16","18","20","22"};
        for(int i=0;i<9;i++)list3.add(buf3[i]);
        
        
        show();
		}
		
		
    public void itemStateChanged(ItemEvent e){

    if(e.getSource()==list1)
      { 
      str=list1.getSelectedItem();
      text1.setText(str);
         
      
      
      
      }
    else if(e.getSource()==list2)  
      { str1=list2.getSelectedItem();
        text2.setText(str1);
        if(str1==buf2[0]){st=Font.PLAIN;}
        else if(str1==buf2[1]){st=Font.ITALIC;}
        else if(str1==buf2[2]){st=Font.BOLD;}
        else if(str1==buf2[3]){st=Font.ITALIC+Font.BOLD;}
      }
    else if(e.getSource()==list3)  
      { str2=list3.getSelectedItem();
      text3.setText(str2);
      si=Integer.parseInt(str2);
     }
        
       
        fonts =new Font(str,st,si);
        text4.setFont(fonts);
  
        
        
    	}
    	
    	public void actionPerformed(ActionEvent e){
    		
    	if(e.getSource()==button1){ta.setFont(fonts);dispose();}
        else if(e.getSource()==button2){dispose();}
    		
    		
    	}
    	

    	}
    //---------------------------------------------------

 public void mouseReleased(MouseEvent e){//判断是否单击鼠标右键
 if(e.isPopupTrigger())   
  m6.show(this,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 static void main(String args[]){
new W1();
}
}

class Win extends WindowAdapter{   //窗口事件适配器类;Win设为其子类;
public void windowClosing(WindowEvent e){
   System.exit(0);
}
}

⌨️ 快捷键说明

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