繁体   English   中英

试图在java中创建一个文本编辑器

[英]Trying to make a text editor in java

我正在尝试用Java创建一个文本编辑器,它不会编译。 jGRASP一直给我一个关于file.add(new)的编译错误,说它无法找到它,即使它存在并在代码中明确定义。

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class textEditor extends JFrame{
   private static int WIDTH = 550;
   private static int HEIGHT = 660;
   private int row = 10;
   private int col = 20;

   // Initialize components

   private JTextArea area;
   private JFileChooser dialog = new JFileChooser(System.getProperty("user.dir"));
   private String currentFile = "Untitled";
   private boolean changed = false;
   private JScrollPane scroll;

   // main constructor
   public textEditor(){
      // Set font
      area.setFont(new Font("Monospaced", Font.PLAIN,12));
      // Set window title and size
      setTitle("Text Editor");
      setSize(WIDTH,HEIGHT);

      // Set textArea and ScrollPane
      area=new JTextArea(row, col);
      scroll = new JScrollPane(area, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
      add(scroll, BorderLayout.CENTER);

      // Add other window components
      JMenuBar jmb = new JMenuBar();
      JMenu file = new JMenu();
      JMenu edit = new JMenu();
      jmb.add(file);
      jmb.add(edit);

      file.add(New);
      file.add(Open);
      file.add(Save);
      file.add(Quit);
      file.add(SaveAs);
      file.addSeparator();


      for(int i=0; i<4; i++){
         file.getItem(i).setIcon(null);
      }

      edit.add(Cut);
      edit.add(Copy);
      edit.add(Paste);


      // Set the names of the items in the edit menu
      edit.getItem(0).setText("Cut");
      edit.getItem(1).setText("Copy");
      edit.getItem(2).setText("Paste");

      JToolBar tool = new JToolBar();
      add(tool,BorderLayout.NORTH);

      tool.add(New);
      tool.add(Open);
      tool.add(Save);
      tool.addSeparator();

      JButton cut=tool.add(Cut), cop=tool.add(Copy), pas=tool.add(Paste);
      cut.setText(null); cut.setIcon(new ImageIcon("cut.gif"));
        cop.setText(null); cop.setIcon(new ImageIcon("copy.gif"));
        pas.setText(null); pas.setIcon(new ImageIcon("paste.gif"));
      Save.setEnabled(false);
      SaveAs.setEnabled(false);

      setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        area.addKeyListener(k1);
        setTitle(currentFile);
        setVisible(true);
   }
   private KeyListener k1 = new KeyAdapter(){
      public void keyPressed(KeyEvent e){
         changed = true;
         Save.setEnabled(true);
         SaveAs.setEnabled(true);
      }
   };
   Action Open = new AbstractAction("Open", new ImageIcon("open.gif")){
      public void actionPerformed(ActionEvent e){
         saveOld();
         if(dialog.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
            readInFile(dialog.getSelectedFile().getAbsolutePath());
         }
         SaveAs.setEnabled(true);
      }
   };
   Action Save = new AbstractAction("Save", new ImageIcon("save.gif")) {
        public void actionPerformed(ActionEvent e) {
            if(!currentFile.equals("Untitled"))
                saveFile(currentFile);
            else
                saveFileAs();
        }
    };
   Action SaveAs = new AbstractAction("Save as...") {
        public void actionPerformed(ActionEvent e) {
            saveFileAs();
        }
    };
   Action Quit = new AbstractAction("Quit") {
        public void actionPerformed(ActionEvent e) {
            saveOld();
            System.exit(0);
        }
    };
   ActionMap m = area.getActionMap();
    Action Cut = m.get(DefaultEditorKit.cutAction);
    Action Copy = m.get(DefaultEditorKit.copyAction);
    Action Paste = m.get(DefaultEditorKit.pasteAction);

   private void saveFileAs() {
        if(dialog.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
            saveFile(dialog.getSelectedFile().getAbsolutePath());
    }

   private void saveOld() {
        if(changed) {
            if(JOptionPane.showConfirmDialog(this, "Would you like to save "+ currentFile +" ?","Save",JOptionPane.YES_NO_OPTION)== JOptionPane.YES_OPTION)
                saveFile(currentFile);
        }
    }

   private void readInFile(String fileName) {
        try {
            FileReader r = new FileReader(fileName);
            area.read(r,null);
            r.close();
            currentFile = fileName;
            setTitle(currentFile);
            changed = false;
        }
        catch(IOException e) {
            Toolkit.getDefaultToolkit().beep();
            JOptionPane.showMessageDialog(this,"Editor can't find the file called "+fileName);
        }
    }

   private void saveFile(String fileName) {
        try {
            FileWriter w = new FileWriter(fileName);
            area.write(w);
            w.close();
            currentFile = fileName;
            setTitle(currentFile);
            changed = false;
            Save.setEnabled(false);
        }
        catch(IOException e) {
        }
    }
}

这是为什么?

我看到AbstractActions为“打开”“保存”“另存为”和“退出”,但我没有看到一个“新”我认为这是你的问题,你“新”AbstractAction尚未定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM