我是初学者,编了一个记事本后,老师要求在里面加入幻灯片功能。要求如下:
记事本中添加一个新菜单:名称为图片显示。
用户可以选择放在磁盘目录中的图片文件,并显示。
用户可以选择存放图片的目录,后记事本可以自动播放目录中的图片。
以下是我记事本的一些代码,求助各位的帮助,万分感谢!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Date;
import java.util.Locale;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import javax.swing.KeyStroke;
import java.awt.event.InputEvent;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class myword extends JFrame{
 final UndoManager undo = new UndoManager();   
 public final static int CORRECT = 0;
 public final static int MISSED_ONE_LETTER = 1;
 public final static int ADDED_ONE_LETTER = 2;
 public final static int EXCHANGED_ADJACEMENT_LETTERS = 3;
 public final static int UNDEFINED_ERROR = 4;
 private int width,height,status,save_status;
 private Container container;
 private JTextArea textarea;     // 文本区 
 private JMenuBar menubar;        //菜单栏
 private JToolBar toolbar;        //工具栏
 private JFrame frame;
 private int fangxiangStatus=0;
 private String fileDictionary,myfileName;
 private JScrollPane scroll;
 private JPopupMenu popup;
 private JMenu wenjian,bianji,geshi,bangzhu,tupian;
 private String copy,searchWord;
 private static int start=0;
 private static int huanhang_count=0;
 private JMenuItem wenjian_item[],bianji_item[],geshi_item,bangzhu_item[],tupian_item[],popup_item[],huanhang_item;
 private JButton  buttons[];       //工具栏上的按键
 public myword()
 {
  super("CC's Notepad");
  container=getContentPane();  
  container.setLayout(new BorderLayout());
  actionEventHandler actionHandler=new actionEventHandler();
  frame=new JFrame();
  width=750;
  height=500;
  status=0;
  save_status=0;
  menubar=new JMenuBar();            //菜单
  popup=new JPopupMenu();              //快捷
  toolbar=new JToolBar();            //工具栏
  container.add(menubar,BorderLayout.NORTH);   
  add(toolbar,BorderLayout.SOUTH);                 //添加工具栏
  addWindowListener(
    new WindowAdapter(){
     public void windowClosing(WindowEvent event)
     {
      if(save_status==0)
      {
       int check=JOptionPane.showConfirmDialog(null,"文件的文字已经改变。想保存文件吗?","警告",JOptionPane.YES_NO_CANCEL_OPTION);
       if(check==0)
       {
        saveFile(status);
       }
       if(check==1)
       {
        System.exit(0);
       }
       if(check==2)
       {
        dispose();
       }
      }    
     }    
    });  buttons=new JButton[]{
  new JButton("", new ImageIcon("D:/new.gif")),
  new JButton("", new ImageIcon("D:/open.gif")),
          new JButton("", new ImageIcon("D:/save.gif")),
          new JButton("", new ImageIcon("D:/cut.gif")),
          new JButton("", new ImageIcon("D:/copy.gif")),                
          new JButton("", new ImageIcon("D:/paste.gif")),
          new JButton("", new ImageIcon("D:/replace.png")),
  };
  for (int i=0;i<buttons.length;i++){
  toolbar.add(buttons[i]);
  buttons[i].addActionListener(actionHandler);
  }
  
    bianji=new JMenu("编辑(E)");                //编辑菜单栏 
  bianji.setMnemonic('E');
  bianji.setFont(new Font("Monospaced",Font.PLAIN,12));
  char bianjis[]={'U','T','C','P','L','F','N','R','G','A','D'};
  char accelerator[]={'Z','X','C','V'};
  String bianjiOperation[]={"撤销(U)","剪切(T)","复制(C)","粘贴(P)","删除(L)","查找(F)","查找下一个(N)","替换(R)...","全选(A)","时间/日期(D)"};
  bianji_item=new JMenuItem[bianjiOperation.length];
  for(int count=0;count<bianjiOperation.length;count++)
  {
   bianji_item[count]=new JMenuItem(bianjiOperation[count]);
   bianji_item[count].setBackground(Color.white);
   bianji_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   bianji_item[count].setMnemonic(bianjis[count]); 
    bianji.add(bianji_item[count]);
   if(count<accelerator.length)
    bianji_item[count].setAccelerator(KeyStroke.getKeyStroke(accelerator[count],InputEvent.CTRL_DOWN_MASK));
   bianji_item[count].addActionListener(actionHandler);
   if(count==0||count==4||count==8)
   bianji.addSeparator();
  }
  menubar.add(bianji);
  bianji_item[4].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
  bianji_item[5].setAccelerator(KeyStroke.getKeyStroke('F',InputEvent.CTRL_DOWN_MASK));
  bianji_item[6].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3,0));
  bianji_item[7].setAccelerator(KeyStroke.getKeyStroke('H',InputEvent.CTRL_DOWN_MASK));
  bianji_item[8].setAccelerator(KeyStroke.getKeyStroke('A',InputEvent.CTRL_DOWN_MASK));
  bianji_item[9].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5,0));
  
  geshi=new JMenu("格式(O)");                //格式菜单栏
  geshi.setMnemonic('O');
  geshi.setFont(new Font("Monospaced",Font.PLAIN,12)); 
  huanhang_item=new JMenuItem("  自动换行(W)");
  huanhang_item.setMnemonic('W');
  huanhang_item.setBackground(Color.white);
  huanhang_item.addActionListener(actionHandler);
  huanhang_item.setFont(new Font("Monospaced",Font.PLAIN,12));    
  geshi_item=new JMenuItem("  字体(F)...");
  geshi_item.setBackground(Color.white);
  geshi_item.setFont(new Font("Monospaced",Font.PLAIN,12));
  geshi_item.setMnemonic('F');
  geshi_item.addActionListener(actionHandler);  
  geshi.add(huanhang_item);
  geshi.add(geshi_item); 
  menubar.add(geshi); 
   
  bangzhu=new JMenu("帮助(H)");                 //帮助菜单栏  
  bangzhu.setMnemonic('H');
  bangzhu.setFont(new Font("Monospaced",Font.PLAIN,12));
  String bangzhuOperation[]={"帮助主题(H)","关于记事本(A)"};
  char bangzhus[]={'H','A'};
  bangzhu_item=new JMenuItem[bangzhuOperation.length]; 
  for(int count=0;count<bangzhuOperation.length;count++)
  {
   bangzhu_item[count]=new JMenuItem(bangzhuOperation[count]);
   bangzhu_item[count].setBackground(Color.white);
   bangzhu_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   bangzhu_item[count].setMnemonic(bangzhus[count]);
   bangzhu.add(bangzhu_item[count]);
   bangzhu_item[count].addActionListener(actionHandler);
   if(count==0)
   bangzhu.addSeparator();
  }  
  menubar.add(bangzhu);
  
  tupian=new JMenu("显示图片(P)");                 //帮助菜单栏  
  tupian.setMnemonic('P');
  tupian.setFont(new Font("Monospaced",Font.PLAIN,12));
  String tupianOperation[]={"图片显示(X)"};
  char tupians[]={'X'};
  tupian_item=new JMenuItem[tupianOperation.length]; 
  for(int count=0;count<tupianOperation.length;count++)
  {
   tupian_item[count]=new JMenuItem(tupianOperation[count]);
   tupian_item[count].setBackground(Color.white);
   tupian_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   tupian_item[count].setMnemonic(tupians[count]);
   tupian.add(tupian_item[count]);
   tupian_item[count].addActionListener(actionHandler);
  }  
  menubar.add(tupian);
  String popupOperation[]={"撤销(U)","剪贴(T)","复制(C)","粘贴(P)","删除(D)","全选(A)"};
  char popupMnemonic[]={'U','T','C','P','D','A'};
  popup_item=new JMenuItem[popupOperation.length];
  for(int count=0;count<popupOperation.length;count++)
  {
   popup_item[count]=new JMenuItem(popupOperation[count]);
   popup_item[count].setBackground(Color.white);
   popup_item[count].setFont(new Font("Monospaced",Font.PLAIN,12));
   popup_item[count].setMnemonic(popupMnemonic[count]);
   popup_item[count].addActionListener(actionHandler);
   if(count==1||count==5)
    popup.addSeparator();
   popup.add(popup_item[count]);
  }
  textarea=new JTextArea();
  textarea.setWrapStyleWord(true);
  textarea.setFont(new Font("Monospaced",Font.PLAIN,14));
  textarea.addMouseListener(
    new MouseAdapter(){
     public   void   mousePressed(MouseEvent   event)
     { 
      if(event.getButton()==event.BUTTON3)
       checkForTriggerEvent(event);             
     }            
     public   void   mouseReleased(MouseEvent   event)
     {       
      if(event.getButton()==event.BUTTON3)
       checkForTriggerEvent(event);
     }
     private void checkForTriggerEvent(MouseEvent event)
     {
      if(event.isPopupTrigger())
       popup.show(textarea, event.getX(), event.getY());
     }
    }
    );
  textarea.addKeyListener(new KeyListener(){
      public void keyTyped(KeyEvent e){
       if(status==1)
       status=0;
      }
      public void keyPressed(KeyEvent e){
      }
      public void keyReleased(KeyEvent e){ 
      }
     });  
  Document doc = textarea.getDocument(); 
  doc.addUndoableEditListener(new UndoableEditListener() {
         public void undoableEditHappened(UndoableEditEvent evt) {
             undo.addEdit(evt.getEdit());
         }
     });
  scroll=new JScrollPane(textarea);
  scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);  
  scroll.setBounds(0,20,width, height-53);
  scroll.setVisible(true); 
  container.add(scroll,BorderLayout.CENTER); 
  setSize(width+5,height);
  setVisible(true);  
 }
//事件监听
 private class actionEventHandler implements ActionListener
 {
  public void actionPerformed(ActionEvent event)
  {
   if(event.getSource()==tupian_item[0])
   
   open_picture()
    }
 private void mysetTitle(String tmp)
 {
  this.setTitle(tmp);
 }
  
  public void open_picture()
 {
 FileDialog s=new FileDialog(frame);
  s.setMode(FileDialog.LOAD);
  s.setVisible(true);
  fileDictionary=s.getDirectory();
  myfileName=s.getFile();
  status=1;
  if(myfileName==null||myfileName.equals(""))
   JOptionPane.showMessageDialog(this, "您没有选择任何文件!", "出错信息",JOptionPane.ERROR_MESSAGE );
  else
  {            //这里该怎么写
   }catch(IOException ioexception){
    JOptionPane.showMessageDialog(this, "文件打开出错", "警告", JOptionPane.ERROR_MESSAGE);
    }
  }
  }
  }
 public static void main(String args[])
 {
  myword application=new myword();
  application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 }