2014-05-01 3 views
0

자바에서 텍스트 편집기를 만들려고하는데 컴파일되지 않습니다. 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) { 
     } 
    } 
} 

왜 그렇습니까?

+0

'신규'변수의 유형은 무엇입니까? 아니면'file.add ("New")'를 쓰려고 했습니까? –

+0

그런 오류에 대해 질문 할 때 우리에게 완전한 오류 메시지를 게시하고 싶을 것입니다. –

+1

당신은'Open','Save','SaveAs','Quit'과 달리 코드에서'New'' Action'을 정의하지 않았습니다. Java에서 변수 이름은 소문자로 시작해야하며 클래스 이름은 대문자로 시작해야합니다. 이렇게하면 서로 구별 할 수 있습니다. – MadProgrammer

답변

2

내가 &이 을 "종료"하지만 난 하나를 볼 수 없습니다 "다른 이름으로 저장" "열기" "저장"에 대한 AbstractActions 참조 "새로운" 나는 그게 문제라고 생각합니다, 당신은 "새로운"AbstractAction로는 아직 정의되지 않았습니다.

관련 문제