【无标题】

package lianxi;
import javax.swing.*;

import javax.swing.filechooser.FileFilter;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class stl extends JFrame implements ActionListener,MouseListener, Runnable{
private JMenuBar cdt;
private JMenu wj, bj, gs, bz, ck,time;
private JMenuItem xj,dk,bc,tc,copy,paste,shear,whole,typeface,ys,gyjsb,ztl;
private JTextArea dhwbk;

JPopupMenu pm =new JPopupMenu();
JMenuItem red=new JMenuItem("红色");
JMenuItem blue=new JMenuItem("蓝色");
JMenuItem green=new JMenuItem("绿色");
JMenuItem black=new JMenuItem("黑色");
JMenuItem zh=new JMenuItem("宋体");
JMenuItem fz=new JMenuItem("复制");
JMenuItem jq=new JMenuItem("剪切");
JMenuItem zt=new JMenuItem("粘贴");
JMenuItem qx=new JMenuItem("全选");
JMenuItem sc=new JMenuItem("删除");
private Color color;

public stl() {
 super("Java记事本");
       cdt = new JMenuBar();
          wj = new JMenu("文件"); 
          bj = new JMenu("编辑"); 
          gs = new JMenu("格式"); 
          bz = new JMenu("帮助"); 
          ck = new JMenu("查看");     
          time = new JMenu("时间");
          xj = new JMenuItem("新建");
          xj.addActionListener(this); 
          dk = new JMenuItem("打开");
          dk.addActionListener(this);
         bc = new JMenuItem("保存");
         bc.addActionListener(this);
         tc = new JMenuItem("退出");
         tc.addActionListener(this);
        
         copy = new JMenuItem("复制");
         copy.addActionListener(this);
         paste = new JMenuItem("粘贴");
         paste.addActionListener(this); 
         shear = new JMenuItem("剪切"); 
         shear.addActionListener(this);
         whole = new JMenuItem("全选");
         whole.addActionListener(this);
         typeface = new JMenuItem("字体"); 
         typeface.addActionListener(this); 
         ys = new JMenuItem("颜色");
         ys.addActionListener(this); 
         gyjsb = new JMenuItem("关于记事本");
         gyjsb.addActionListener(this); 
         ztl = new JMenuItem("状态栏");
         ztl.addActionListener(this);
         
         cdt.add(wj);
         cdt.add(bj); 
         cdt.add(gs); 
         cdt.add(bz);
         cdt.add(ck);
         cdt.add(time);
         
         wj.add(xj);
         wj.add(dk); 
         wj.add(bc);
         wj.add(tc); 
         
         bj.add(copy);
         bj.add(paste);
         bj.add(shear); 
         bj.add(whole);
         
         gs.add(typeface);
         gs.add(ys);
         
         bz.add(gyjsb);
         ck.add(ztl);
setJMenuBar(cdt);

dhwbk=new JTextArea(23,70); 
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(dhwbk);
this.getContentPane().add(new JScrollPane(dhwbk));
 setSize(800, 500);
 dhwbk.add(pm);
 pm.add(red);
 pm.add(blue);
 pm.add(green);
 pm.add(black);
 pm.add(zh);
 pm.add(fz);
 pm.add(jq);
 pm.add(zt);
 pm.add(qx);
 pm.add(sc);

 red.addActionListener(this);
 blue.addActionListener(this);
 green.addActionListener(this);
 black.addActionListener(this);
 zh.addActionListener(this);
 fz.addActionListener(this);
 jq.addActionListener(this);
 zt.addActionListener(this);
 qx.addActionListener(this);
 sc.addActionListener(this);
 
 dhwbk.addMouseListener(this);
 setVisible(true);
 setupTime();
 }
public void setupTime() {     
    Thread thread1 = new Thread(this);
    thread1.start();
    }
public void run() {
    while (true) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy 年 MM 月 dd 日          时间:HH : mm : ss");
        time.setText("当前日期:  " + sdf.format(new Date()));
        time.setForeground(Color.BLUE);
    }
    }
private void setDirty(boolean isDirty) {
    
}

public void actionPerformed(ActionEvent e) {
     JMenuItem item = (JMenuItem)e.getSource();
     if(item.equals(xj)){
     clearEditorArea();
     setDirty(false);
}else if(item.equals(dk)){
      openFile();
}else if(item.equals(bc)){
      saveFile();
}else if(item.equals(tc)){
      dispose();
 }else if(item.equals(copy)){
     dhwbk.copy();
 }else if(item.equals(shear)){
     dhwbk.cut();
 }else if(item.equals(paste)){
     dhwbk.paste();
 }else if(item.equals(whole)){
     dhwbk.selectAll();
 }
 else if(item.equals(typeface)){
  FontDialog font = new FontDialog(this, dhwbk.getFont());
  dhwbk.setFont(font.getSelectedFont());
 }else if(item.equals(gyjsb)){
}if (e.getSource()==red){
    dhwbk.setForeground(Color.red);
}if (e.getSource()==blue){
    dhwbk.setForeground(Color.blue);
}if (e.getSource()==green){
    dhwbk.setForeground(Color.green);
}if (e.getSource()==black){
    dhwbk.setForeground(Color.black); 
}if (e.getSource()==zh){
    dhwbk.setFont(new Font("宋体",Font.BOLD,20));
}if (e.getSource()==fz){
    dhwbk.copy();
}if (e.getSource()==jq){
    dhwbk.cut();
}if (e.getSource()==zt){
    dhwbk.paste();
}if (e.getSource()==qx){
    dhwbk.selectAll();
}if(e.getSource()==sc){
     clearEditorArea();
     setDirty(false);
}else if(e.getSource()==ys){
     color = JColorChooser.showDialog(cdt,"字体颜色设计",color);
     dhwbk.setForeground(color);
     }else if (e.getSource() == gyjsb)
       JOptionPane.showMessageDialog(null, "学院:信息技术学院"
               + "\n班级:信息技术"
               + "\n辅导老师:张三老师"
               + "\n姓名:李四同学"
               + "\n学号:123456789学号\n  ",
          "关于记事本", JOptionPane.INFORMATION_MESSAGE);
}
private void clearEditorArea(){
    dhwbk.selectAll();
    dhwbk.replaceSelection("");
}
//打开文件
private void openFile(){
 JFileChooser openDialog = new JFileChooser();
 openDialog.setFileFilter(new TxtFileFilter());
 if(openDialog.showOpenDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
  File file = openDialog.getSelectedFile();
  BufferedReader br = null;
  try {
   br = new BufferedReader(new FileReader(file));
   String buff = br.readLine();
 
   while(buff != null){
       dhwbk.append(buff);
       dhwbk.append("\n");
    buff = br.readLine();}
  } catch (FileNotFoundException e1) {
   e1.printStackTrace();
  } catch (IOException ioe) {
   ioe.printStackTrace();
  } finally{
   try{
    if(br != null)
     br.close();
   } catch (IOException ioe){
    ioe.printStackTrace();
    }
   }
  }
 }
//保存文件
private void saveFile(){
 JFileChooser saveDialog = new JFileChooser();
 saveDialog.setFileFilter(new TxtFileFilter());
 if(saveDialog.showSaveDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
  File file = saveDialog.getSelectedFile();
  BufferedWriter bw = null;
  try {
   bw = new BufferedWriter(new FileWriter(file));
   String buff = dhwbk.getText();
   bw.write(buff);
  } catch (IOException ioe) {
   ioe.printStackTrace();
  } finally{
  try{
    if(bw != null)
     bw.close();
   } catch (IOException ioe){
    ioe.printStackTrace();
    }
   }
  }
 }
class TxtFileFilter extends FileFilter{
 public boolean accept(File f) {
  return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
 }
 @Override
 public String getDescription() {
  return "*.txt(文本文件)";
 }
}
//设置字体
class FontDialog extends JDialog{
 private JComboBox cb_FontSize;
 private JComboBox cb_FontStyle;
 private JComboBox cb_FontNm;
 private Font font;
Hashtable<Integer, String> style = new Hashtable<Integer, String>();
 public FontDialog(){
  this(null, null);}
 public FontDialog(Frame owner, Font font){
  super(owner);
  this.font = font == null?getFont():font;
  setTitle("字体选择框");
  setModal(true);
  setResizable(false);
  setSize(326, 164);
  getContentPane().setLayout(null);
  final JLabel lb_FontNm = new JLabel();
  lb_FontNm.setText("字体名称");
  lb_FontNm.setBounds(10, 10, 66, 16);
  getContentPane().add(lb_FontNm);
  cb_FontNm = new JComboBox();
  cb_FontNm.setBounds(10, 28, 133, 25);
  getContentPane().add(cb_FontNm);
  cb_FontStyle = new JComboBox();
  cb_FontStyle.setBounds(169, 28, 66, 25);
  getContentPane().add(cb_FontStyle);
  cb_FontSize = new JComboBox();
  cb_FontSize.setBounds(258, 28, 53, 25);
  getContentPane().add(cb_FontSize);
 final JButton btn_OK = new JButton();
  btn_OK.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    int styleCode = 0;
    for(Enumeration<Integer> i = style.keys();i.hasMoreElements();
        ){
     styleCode = i.nextElement();
     if(style.get(styleCode).equals(cb_FontStyle.getSelectedItem()))
      break;
     }
    Font font = new Font(cb_FontNm.getSelectedItem().toString(), styleCode, ((Integer)cb_FontSize.getSelectedItem()).intValue());
    setSelectedFont(font);
    dispose();   
    } 
   });
  btn_OK.setText("确定");
  btn_OK.setBounds(58, 83, 76, 26);
  getContentPane().add(btn_OK);
  final JButton btn_Cancel = new JButton();
  btn_Cancel.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    dispose();
    }
   });
  btn_Cancel.setText("取消");
  btn_Cancel.setBounds(169, 83, 76, 26);
  getContentPane().add(btn_Cancel);
  final JLabel lb_FontStyle = new JLabel();
  lb_FontStyle.setText("字体样式");
  lb_FontStyle.setBounds(169, 10, 66, 16);
  getContentPane().add(lb_FontStyle);

 final JLabel lb_FontSize = new JLabel();
  lb_FontSize.setText("字体大小");
  lb_FontSize.setBounds(258, 10, 66, 16);
  getContentPane().add(lb_FontSize);
  init();
  setVisible(true);
  }
 private void init(){
  GraphicsEnvironment gg=GraphicsEnvironment.getLocalGraphicsEnvironment();
  String ss[]=gg.getAvailableFontFamilyNames();
  for(String s : ss)
   cb_FontNm.addItem(s);
  if(font != null)
   cb_FontNm.setSelectedItem(font.getFamily());
  style.put(Font.PLAIN, "标准");
  style.put(Font.BOLD, "粗体");
  style.put(Font.ITALIC, "斜体");
  style.put(Font.BOLD+Font.ITALIC, "粗体&斜体");
  cb_FontStyle.addItem(style.get(Font.PLAIN));
  cb_FontStyle.addItem(style.get(Font.BOLD));
  cb_FontStyle.addItem(style.get(Font.ITALIC));
  cb_FontStyle.addItem(style.get(Font.BOLD+Font.ITALIC));

  if(font != null)
   cb_FontStyle.setSelectedItem(style.get(font.getStyle()));
  for(int i=8;i<23;i++)
   cb_FontSize.addItem(i);
  if(font != null)
   cb_FontSize.setSelectedItem(font.getSize());
  }
public Font getSelectedFont() {
  return font;}
 public void setSelectedFont(Font font) {
  this.font = font; 
  }
}
public void mouseReleased (MouseEvent e){
    if(e.isPopupTrigger())
        pm.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[]) {
     stl tt= new stl();
     }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值