| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2507 人关注过本帖
标题:帮我看一下,我写的对吗
取消只看楼主 加入收藏
ssaann
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-6-15
结帖率:0
收藏
已结贴  问题点数:20 回复次数:1 
帮我看一下,我写的对吗
package zs;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java .awt.event.*;
import *;
public class lianxi extends JFrame
{
     private JTextArea editor;
     private Container c;
     //private Font f=new Font(“sanserif”, Font.PLAIN,12);
     //菜单栏成员属性声明
     private JMenuBar mb;          //菜单棒
     private JMenu fileMenu;       //文件菜单
     private JMenu editMenu;       //编辑菜单
     private JMenu formatMenu;    //格式菜单
     private JMenuItem fileMenuOpen,fileMenuSave,fileMenuExit;   //文件菜单编辑菜单
     private JMenuItem editMenuCopy,editMenuCut,editMenuPaste;   //编辑菜单编辑菜单
     private JMenu formatSet;      //格式菜单第一组菜单
     private JMenuItem lineWrap,cancleLineWrap,cancleWrapStyleWord;
     private JMenu fontMenu;       //字体菜单
     private JMenu color;
     private JMenu style;          //字体样式
     private JMenuItem bold,italic,hong,lan,lv,huang,hei;
     int b=Font.PLAIN;
     int i=Font.PLAIN;
     //字号
     String[] zh={" 8 "," 10 "," 12 "," 14 "," 16 "," 18 "," 20 "," 24 "," 28 "," 32"," 40 "," 56 ",};
     private JMenu fontlabe1;
     private JComboBox c1,c2;
     String ss="宋体";
     int s=12;
     //字体样式
     private String[] mz={" 宋体  "," 隶书  "," 楷体  "," 宋体  "," 华文新魏  "," 华文新魏  "," 华文行楷  "," 幼圆  "," 黑体"," 华文彩云 "," 方正姚体  ",};
     //工具栏成员属性声明
     private JToolBar toolBar;       //工具棒
     private JButton b1,b2,b3,b4,b5;   //声明5个按钮,分别为:" 打开 "," 保存 "," 复制 "," 剪切 "," 粘贴 "
     //弹出式菜单属性声明
     private JPopupMenu pm;
     private JMenuItem item1,item2,item3,item4,item5;

     public lianxi ()
     {
     super(" 简单记事本 ");
     setBounds(200,100,500,400);
     try{
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
     }
     catch (Exception e){  System.err.println(" 不能被设置外观的原因: "+e);}
     c=getContentPane();          //创建一个内容面板
     editor = new JTextArea();    //创建一个文本区
     c.add(new JScrollPane(editor));   //设置滚动条,并添加到内容面板

     //菜单栏的实现
     //文件菜单的实现
     mb = new JMenuBar();     //创建菜单棒
     fileMenu = new JMenu(" 文件(F) ");    //创建菜单
     fileMenuOpen = new JMenuItem(" 打开(O)...Ctrl+O ");
     fileMenuSave = new JMenuItem(" 保存(S)...Ctrl+S ");
     fileMenuExit = new JMenuItem(" 退出 ");
     JMHandler JM=new JMHandler();       //创建监听器
     fileMenuOpen.addActionListener(JM);   //注册监听器
     fileMenuSave.addActionListener(JM);
     fileMenu.add(fileMenuSave);
     fileMenu.add(fileMenuOpen);
     fileMenu.add(fileMenuSave);
     fileMenu.addSeparator();        //添加分隔线
     fileMenu.add(fileMenuSave);
    //编辑菜单的实现
    editMenu = new JMenu("编辑(E)");
    editMenuCopy = new JMenuItem(" 复制(C) Ctrl+C ");
    editMenuCut = new JMenuItem(" 剪切(T) Ctrl+X ");
    editMenuPaste =new JMenuItem(" 粘贴(P) Ctrl+V ");
    EMHandler EM=new EMHandler();        //创建监听器
    editMenuCopy.addActionListener(EM);    //注册监听器
    editMenuPaste.addActionListener(EM);
    editMenu.add(editMenuCopy);
    editMenu.add(editMenuCut);
    editMenu.add(editMenuPaste);
    //格式菜单的实现
    formatMenu = new JMenu(" 格式(O) ");      //创建菜单  
    formatSet = new JMenu(" 格式设置 ");      //二级菜单
    lineWrap = new JMenuItem(" 自动换行 ");   //二级菜单项
    cancleLineWrap = new JMenuItem(" 取消自动换行 ");
    wrapStyleWord = new JMenuItem(" 断行不断字 ");
    cancleWrapStyleWord = new JMenuItem(" 取消断行不断字 ");
    FMhandler FM=new FMHandler();
    lineWrap.addActionListener(FM);
    cancleLineWrap.addActionListener(FM);
    wrapStyleWord.addActionListener(FM);
    cancleWrapStyleWord.addActionListener(FM);
    formatSet.add(lineWrap);
    formatSet.add(cancleLineWrap);
    formatSet.addSeparator();     //添加分隔线
    formatSet.add(wrapStyleWord);
    formatSet.add(cancleWrapStyleWord);
    formatSet.add(formatSet);
   
    //字体菜单的实现
    fontMenu=new JMenu(" 字体(Z) ");
    color=new JMenu(" 颜色 ");
    hong=new JMenuItem(" 红色 ");
    color.add(hong);
    lan=new JMenuItem(" 蓝色 ");
    color.add(lan);
    lv=new JMenuItem(" 绿色 ");
    color.add(lv);
    huang=new JMenuItem(" 黄色 ");
    color.add(huang);
    hei=new JMenuItem(" 黑色 ");
    color.add(hei);
    //style=new JMenu("样式");
    bold=new JCheckBoxMenuItem(" 粗体 ");
    //style.add(bold);
    italic=new JCheckBoxMenuItem(" 斜体 ");
    //style.add(italic);
    HMHandler HM=new HMHandler();   //创建监听器
    hong.addActionListener(HM);
    lan.addActionListener(HM);
    lv.addActionListener(HM);
    huang.addActionListener(HM);
    bold.addActionListener(HM);
    italic.addActionListener(HM);
    color.addActionListener(HM);
    //style.addActionListener(HM);
    fontMenu.add(color);
    fontMenu.add(bold);
    fontMenu.add(italic);
   
    //字母组合框
    fontlabe1=new JMenu(" 字号(H)");
    //组合框
    c1=new JComboBox(zh);
    c1.setBackground(Color.green);
    c1.setFont(new Font("宋体",Font.PLAIN,12));
    c1.setMaximumRowCount(5);
    CMHandler CM=new CMHandler();
    c1.addActionListener(CM);
    fontlabe1.add(c1);
    //字体样式
    style=new JMenu(" 字体样式(S) ");
    c2=new JComboBox(mz);
    c2.setBackground(Color.green);
    c2.setFont(new Font("宋体",Font.PLAIN,12));
    c2.setMaximumRowCount(5);
    SMHandler SM=new SMHandler();
    c2.addItemListener(SM);
    style.add(c2);
   
    //将菜单全部添加菜单棒里
    mb.add(fileMenu);
    mb.add(editMenu);
    mb.add(formatMenu);
    mb.add(fontMenu);
    mb.add(fontlabe1);
    mb.add(style);
   
    //工具栏的实现
    //按钮分别为:"打开","保存","复制","剪切","粘贴"
    toolBar =new JToolBar();
    b1= new JButton(new ImageIcon(" d:/img/open.gif"));
    b2= new JButton(new ImageIcon(" d:/img/save.gif"));
    b3= new JButton(new ImageIcon(" d:/img/copy.gif"));
    b4= new JButton(new ImageIcon(" d:/img/cyt.gif"));
    b5= new JButton(new ImageIcon(" d:/img/paste.gif"));
    TBHanlder TB=new TBHanlder();    //创建按钮监听器
    b1.addActionListener(TB);
    b2.addActionListener(TB);
    b3.addActionListener(TB);
    b4.addActionListener(TB);
    b5.addActionListener(TB);
    //把按钮全部添加到工具棒中
    toolBar.add(b1);
    toolBar.add(b2);
    toolBar.add(b3);
    toolBar.add(b4);
    toolBar.add(b5);
   
    //弹出菜单的实现
    pm =new JPopupMenu();  //创建弹出菜单
    item1 = new JMenuItem(" 打开 ");
    item2 = new JMenuItem(" 保存");
    item3 = new JMenuItem(" 复制 ");
    item4 = new JMenuItem(" 剪切");
    item5 = new JMenuItem(" 粘贴 ");
    JPHandler JP=new JPHandler();
    item1.addActionListener(JP);  //注册菜单项的鼠标事件监听器
    item2.addActionListener(JP);
    item3.addActionListener(JP);
    item4.addActionListener(JP);
    item5.addActionListener(JP);
    editor.addMouseListener(JP);   //注册文本区的鼠标事件监听器
    pm.add(item1);
    pm.add(item2);
    pm.add(item3);
    pm.add(item4);
    pm.add(item5);
    //把菜单栏,工具栏,弹出菜单添加到内容面板
    setJMenuBar(mb);   //显示菜单栏
    c.add(toolBar,BorderLayout.NORTH);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
     
     //字号菜单
     public class CMHandler implements ActionListener
     {
         public void actionPerformed(ActionEvent e)
         {
             if(e.getSource()==c1)
             {
                 s=Integer.parseInt(zh[c1.getSelectedIndex()]);
                 editor.setFont(new Font(ss,b+i,s));
             }
         }
     }
     //自定义类实现文件菜单项的事件处理
     private class JMHandler implements ActionListener
     {
         public void actionPerformed(ActionEvent e)
         {
             if(e.getSource()==fileMenuOpen){ loadFile(); }
             else if(e.getSource()==fileMenuSave){ saveFile(); }
             else{ System.exit(0); }
         }
  public void loadFile()  //打开文件方法
  {
      JFileChooser fc=new JFileChooser();
      int r=fc.showSaveDialog(this);
      if(r==JFileChooser.APPROVE_OPTION)
      {
          File file=fc.getSelectedFile();
          try{ editor.write(new FileWriter(file)); }
          catch(IOException e){}
      }
  }
  //编辑简单菜单项的事件处理
  private class EMHandler implements ActionListener
  {
      public void actionFerformed(ActionEvent e)
      {
          if(e.getSource()==editMenuCopy)  //实现复制功能
          {
              editor.copy();
              editor.requestFocus();
          }
          else if(e.getSource()==editMenuCut)  //实现剪切功能
          {
              editor.cut();
              editor.requestFocus();
          }
          else   //实现粘贴
          {
              editor.paste();
              editor.requestFocus();
          }
      }
  }
  //格式菜单二级菜单的菜单项的事件处理
  private class FMHandler implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
      {
          if(e.getSource()==lineWrap){ editor.setLineWrap(true);}
          else if(e.getSource()==cancleLineWrap){ editor.setLineWrap(false);}
          else if(e.getSource()==wrapStyleWord){ editor.setWrapStyleWord(true);}
          else{ editor.setWrapStyleWord(false);}
      }
  }
  //自定义实现工具栏的按钮事件处理
  private  class TBHanlder implements ActionListener
  {
      public void actionPerformed(ActionEvent e)
      {
          if(e.getSource()==b1){ loadFile(); }  //实现打开文件功能
          else if((e.getSource()==b2){ saveFile();}  //实现保存文件功能
          else if((e.getSource()==b3)  //文件复制
                  {
              editor.copy();
              editor.requestFocus();
                  }
          else if(e.getSource()==b4)  //文件剪切
          {
              editor.cut();
              editor.requestFocus();
          }
          else  //文件粘贴
          {
              editor.paste();
              editor.requestFocus();
          }
      }
  }
  //自定义类实现弹出式菜单的事件处理
  private  class JPHandler implements  ActionListener,MouseListener
  {
      public void actionPerformed(ActionEvent e)
      {
          if(e.getSource()==item1){ loadFile(); }   //实现打开文件功能
          else if((e.getSource()==item2){ saveFile();}  //实现保存文件功能
          else if((e.getSource()==item3)  //文件复制
                  {
              editor.copy();
              editor.requestFocus();
                  }
          else if(e.getSource()==item4)  //文件剪切
          {
              editor.cut();
              editor.requestFocus();
          }
          else  //文件粘贴
          {
              editor.paste();
              editor.requestFocus();
          }
      }
  }
  public void mouseReleased(MouseEvent e)
  {
      if(e.isPopupTrigger())  //判断是否按下鼠标右键
          pm.show(editor,e.getX(),e.getY());  //显示弹出式菜单
  }
  public void mouseCliked(MouseEvent e){}
  public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
  public void mousePressed(MouseEvent e){}
  }
     //字体菜单按钮事件处理
public class HMHandler implements ActionListener
{
public void mouseReleased(MouseEvent e)
{
String cmd=e.getActionCommand();
int b;
if(cmd.equals("红色"))
    editor.setForegfound(Color.red);
else if(cmd.equals("蓝色"))
    editor.setForegfound(Color.blue);
else if(cmd.equals("绿色"))
    editor.setForegfound(Color.green);
else if(cmd.equals("黄色"))
    editor.setForegfound(Color.yellow);
else if(cmd.equals("黑色"))
    editor.setForegfound(Color.black);
else if(cmd.equals("粗体"))
    editor.setFont(new Font("宋体",Font.BOLD,s));
else if(cmd.equals("斜体"))
    editor.setFont(new Font("宋体",Font.ITALIC,s));
}
}
//字体样式
public class SMHandler implements ItemListener
{
    public void itemStateChanged(ItemEvent e)
    {
        if(e.getStateChange()==e.SELECTED)
        {
            ss=(String)c2.getSelectedItem();
            editor.setFont(new Font(ss,b+i,s));
        }
    }
public static void main(String[] args)
{
    Nodepad N=new Nodepad();
}
}
}
搜索更多相关主题的帖子: private editor new 菜单 add 
2018-06-15 11:35
ssaann
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2018-6-15
收藏
得分:0 
有人能帮我吗
2018-06-15 11:39
快速回复:帮我看一下,我写的对吗
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032879 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved