2014-02-27 84 views
0

你好,我创建简单的Java编辑器简单的Java编辑器GUI

我创建菜单的GUI代码,但我需要匹配 文件:新建:创建一个新的文件。询问文件的名称(以及公共类)以及它将被存储的目录。将创建的文件插入公共类的结构中,例如公共类MyClass {}。

打开:打开源代码为java(.java)的文件。 保存:将当前片段保存在创建期间建立的同一个文件中。 另存为:显示您请求文件名称的对话框以及存储文件的目录格式。格式将是java文件(.java)。该窗口的主要部分将使用户使用编辑器来编辑文件源Java。

窗口的主要部分将有编辑器供用户用来编辑文件源Java。这将一个片段的处理过程中被更新的信息:数字线的数目在Java源代码

格式化文本保留字


每个文件都将打开格式化并在下面的规则来处理时将格式化:该java的保留字将以蓝色显示。 的意见会显示在绿色 字符串文字橙色 所有其他与黑色 的字体将成为快递字体大小12磅

我会提供GUI代码有人能帮助我与上面?

问候 安东尼

// ClassEEFrame 

package editor.gui; 

import java.awt.Color; 
import java.awt.Component; 
import java.awt.Dimension; 
import java.awt.GridBagConstraints; 
import java.awt.GridBagLayout; 
import java.awt.HeadlessException; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.InputStreamReader; 

import javax.swing.Box; 
import javax.swing.BoxLayout; 
import javax.swing.JComponent; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JOptionPane; 
import javax.swing.JPanel; 
import javax.swing.JScrollPane; 
import javax.swing.JTextField; 
import javax.swing.JTextPane; 
import javax.swing.border.TitledBorder; 
import javax.swing.filechooser.FileNameExtensionFilter; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.Style; 
import javax.swing.text.StyleConstants; 
import javax.swing.text.StyleContext; 
import javax.tools.JavaCompiler; 
import javax.tools.ToolProvider; 

public class EEFrame extends JFrame { 

    /** 
    * 
    */ 
    private static final long serialVersionUID = -1709009137090877913L; 
    private GridBagLayout layout; 
    private GridBagConstraints constraints; 
    private EEMenuBar menuBar; 
    private EETextPane editor; 
    private EEConsole console; 
    private EEStatusBar statusBar; 
    private File file; 

    public EEFrame() throws HeadlessException { 
     super("Elearn Editor"); 

     JScrollPane scrollPane; 

     layout = new GridBagLayout(); 
     setLayout(layout); 

     constraints = new GridBagConstraints(); 

     menuBar = new EEMenuBar(); 
     setJMenuBar(menuBar); 

     editor = new EETextPane(); 

     scrollPane = new JScrollPane(editor); 
     scrollPane.setBorder(new TitledBorder("Editor")); 

     setConstraints(1, 100, GridBagConstraints.BOTH); 
     addComponent(scrollPane, 0, 0, 1, 1); 

     console = new EEConsole(); 

     scrollPane = new JScrollPane(console); 
     scrollPane.setBorder(new TitledBorder("Console")); 

     setConstraints(1, 40, GridBagConstraints.BOTH); 
     addComponent(scrollPane, 1 ,0 ,1, 1); 

     statusBar = new EEStatusBar(); 
     setConstraints(1, 0, GridBagConstraints.BOTH); 
     addComponent(statusBar, 2, 0, 1, 1); 

     file = null; 
    } 

    public JTextPane getTextPane() { 
     return this.editor; 
    } 

    public void setLines(int lines) { 
     this.statusBar.setLines(lines); 
    } 

    public void setWords(int words) { 
     this.statusBar.setJavaWords(words); 
    } 

    private void setConstraints(int weightx, int weighty, int fill) { 
     constraints.weightx = weightx; 
     constraints.weighty = weighty; 
     constraints.fill = fill; 
    } 

    private void addComponent(Component component, int row, int column, int width, int height) { 
     constraints.gridx = column; 
     constraints.gridy = row; 
     constraints.gridwidth = width; 
     constraints.gridheight = height; 
     layout.setConstraints(component, constraints); 
     add(component); 
    } 

    private class EEMenuBar extends JMenuBar { 

     /** 
     * 
     */ 
     private static final long serialVersionUID = -2176624051362992835L; 
     private JMenu fileMenu, compilationMenu; 
     private JMenuItem newItem, openItem, saveItem, saveAsItem, exportItem, compileProcessItem, compileClassItem; 

     public EEMenuBar() { 
      super(); 

      fileMenu = new JMenu("File"); 

      newItem = new JMenuItem("New"); 

      newItem.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent arg0) { 
        /* TODO Dispay dialog with inputs class name and file path */ 
       } 

      }); 

      fileMenu.add(newItem); 

      openItem = new JMenuItem("Open"); 

      openItem.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent arg0) { 
        /*TODO Open existing java source file*/ 

       } 

      }); 

      fileMenu.add(openItem); 

      saveItem = new JMenuItem("Save"); 
      saveItem.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        /*TODO save changes to file*/          
       } 

      }); 

      fileMenu.add(saveItem); 

      saveAsItem = new JMenuItem("Save As"); 

      saveAsItem.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        /*TODO Save as new java source file*/ 
       }    
      }); 

      fileMenu.add(saveAsItem); 

      exportItem = new JMenuItem("Export to pdf"); 

      exportItem.addActionListener(new ActionListener(){ 

       @Override 
       public void actionPerformed(ActionEvent arg0) { 
        // TODO save as pdf(formatted) 

       } 
      }); 

      fileMenu.add(exportItem);   

      add(fileMenu); 

      compilationMenu = new JMenu("Compilation"); 

      compileProcessItem = new JMenuItem("Compile with system jdk"); 

      compileProcessItem.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent e) { 
        /*TODO Compile java source code and show results in teminal(inside editor)*/ 
       } 

      }); 

      compilationMenu.add(compileProcessItem); 

      compileClassItem = new JMenuItem("Compile with JavaCompiler Class"); 

      compileClassItem.addActionListener(new ActionListener() { 

       @Override 
       public void actionPerformed(ActionEvent arg0) { 
        /*TODO Call system compiler for file*/ 
       } 
      }); 

      compilationMenu.add(compileClassItem); 

      add(compilationMenu); 

     } 
    } 

    private class EETextPane extends JTextPane { 

     /** 
     * 
     */ 
     private static final long serialVersionUID = -7437561302249475096L; 

     public EETextPane() { 
      super(); 

      //add styles to document 
      Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 
      StyleConstants.setForeground(def, Color.BLACK); 
      StyleConstants.setFontFamily(def, "Courier"); 
      StyleConstants.setFontSize(def, 12); 

      Style keyword = addStyle("keyword", def); 
      StyleConstants.setForeground(keyword, Color.BLUE); 

      Style literal = addStyle("literal", def); 
      StyleConstants.setForeground(literal, Color.ORANGE); 

      Style comment = addStyle("comment", def); 
      StyleConstants.setForeground(comment, Color.GREEN); 
     } 
    } 

    private class EEConsole extends JTextPane { 

     /** 
     * 
     */ 
     private static final long serialVersionUID = -5968199559991291905L; 

     public EEConsole() { 
      super(); 

      //add styles to document 
      Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE); 
      StyleConstants.setForeground(def, Color.BLACK); 
      StyleConstants.setFontFamily(def, "Courier"); 
      StyleConstants.setFontSize(def, 12); 

      Style keyword = addStyle("error", def); 
      StyleConstants.setForeground(keyword, Color.RED); 

      Style literal = addStyle("success", def); 
      StyleConstants.setForeground(literal, Color.GREEN); 
     } 

    } 

    private class EEStatusBar extends JPanel { 

     /** 
     * 
     */ 
     private static final long serialVersionUID = 185007911993347696L; 
     private BoxLayout layout; 
     private JLabel linesLabel, lines, wordsLabel, words; 

     public EEStatusBar() { 
      super(); 

      layout = new BoxLayout(this, BoxLayout.X_AXIS); 
      setLayout(layout); 

      linesLabel = new JLabel("Lines : "); 
      linesLabel.setAlignmentX(LEFT_ALIGNMENT); 
      add(linesLabel); 

      lines = new JLabel(""); 
      lines.setAlignmentX(LEFT_ALIGNMENT); 
      add(lines); 

      add(Box.createRigidArea(new Dimension(10,10))); 

      wordsLabel = new JLabel("Java Words : "); 
      wordsLabel.setAlignmentX(LEFT_ALIGNMENT); 
      add(wordsLabel); 

      words = new JLabel(""); 
      words.setAlignmentX(LEFT_ALIGNMENT); 
      add(words); 
     } 

     public void setLines(int lines) { 
      /*TODO set line numbers */ 
     } 

     public void setJavaWords(int words) { 
      /*TODO set java keyword numbers*/ 
     } 
    } 

}


//class Main 

package editor.app; 

import javax.swing.JFrame; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

import elearning.editor.gui.EEFrame; 
import elearning.editor.util.EECodeFormater; 
import elearning.editor.util.EEJavaWordCounter; 
import elearning.editor.util.EELineCounter; 

public class EEditor { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 

     try { 
      // Set cross-platform Java L&F (also called "Metal") 
      //UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName()); 

      //Set Motif L&F 
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); 

      //Set Nimbus L&F 
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); 

      //Set System L&F 
      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 

      //Set GTK L&F --> Same as System L&F on Linux and Solaris\ 
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel"); 

      //Set Windows L&F --> Same as System L&F on Windows 
      //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     } 
     catch (UnsupportedLookAndFeelException e) { 
      // handle exception 
     } 
     catch (ClassNotFoundException e) { 
      // handle exception 
     } 
     catch (InstantiationException e) { 
      // handle exception 
     } 
     catch (IllegalAccessException e) { 
      // handle exception 
     } 

     EEFrame frame = new EEFrame(); 

     frame.setSize(500, 600); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); 


     /* TODO Instatiate Threads */ 


     /*TODO Start Threads */ 

    } 

} 

此外,我提供它的一个样机:

Mockup

+2

请尝试缩小你的问题,这样你可能有更高的机会得到答案。 –

+1

你应该尽量减少完整的问题,删除噪音..以获得快速的答案,可能是一个很好的答案! – nachokk

+0

问题是什么?你有什么问题? – MadProgrammer

回答

0

首先你应该看看File课程。它为您提供创建,打开,修改和保存文件的方法。要阅读文件,你也可以给BufferedReader或任何其他读者一个镜头。

  • 创建一个文件:File有方法createNewFile(),结合使用它exists()
  • 要打开并阅读文件,请使用试用资源(在Java手册中实际上有一个很好的tutorial)。
  • 要保存文件,你应该检查出FileWriter,它可以写入字符串或将它们追加到文件。
  • 对于您的编辑器,您可能需要将前面提到的BufferedReader替换为LineReader,它还提供了获取行号的方法。除此之外,你必须弄清楚如何为线条编号。 (其实这只是google搜索,会有一些想法,如this one - 我没有详细检查,但它可能会有所帮助)。
  • 当然,对于编辑器来说,您应该先将文件读入字符串,然后使用格式化程序,然后在需要时将其呈现并重新格式化。

除了这些提示,我不能提供更详细的答案,因为你也可以阅读评论,如果你会提供更详细的问题,它会更容易。你现在只给了我们一个与你的实际问题几乎没有任何关系的GUI。
向我们展示您的一些(有问题的)工作,我们可以帮助您,但除此之外,我们不能做的只是给您提供一些提示,就像我刚才所做的那样。因此,尽量考虑你的问题,尝试如何寻求更准确的答案,并打开一些新的问题,如果你想。
但不要忘了查看网站的答案,对于我来说,几乎我想问的所有问题都已经以类似的方式提出。

0

您好再让拆分工作纳入步骤,

首先,我想创造新的,打开,保存,另存为,导出为PDF菜单和事件

下面

是我使用的代码, GUI框架以新的方式正确打开,打开,保存,另存为,导出到pdf标签中,但因为操作没有发生。

有人可以写信给我正确的代码吗?请记住我非常java初学者。

public EEMenuBar() { 
     super(); 

     fileMenu = new JMenu("File"); 
     //Create the New menu item 
     newItem = new JMenuItem("New"); 
     newItem.setMnemonic(KeyEvent.VK_N); 
     newItem.addActionListener(new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 

      } 

     }); 

     fileMenu.add(newItem); 

     openItem = new JMenuItem("Open"); 
     openItem.setMnemonic(KeyEvent.VK_0); 
     openItem.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       /*TODO Open existing java source file*/ 

      } 

     }); 

     fileMenu.add(openItem); 

     saveItem = new JMenuItem("Save"); 
     saveItem.setMnemonic(KeyEvent.VK_S); 
     saveItem.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       /*TODO save changes to file*/          
      } 

     }); 

     fileMenu.add(saveItem); 

     saveAsItem = new JMenuItem("Save As"); 
     saveAsItem.setMnemonic(KeyEvent.VK_A); 
     saveAsItem.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       /*TODO Save as new java source file*/ 
      }    
     }); 

     fileMenu.add(saveAsItem); 

     exportItem = new JMenuItem("Export to pdf"); 
     exportItem.setMnemonic(KeyEvent.VK_E); 
     exportItem.addActionListener(new ActionListener(){ 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       // TODO save as pdf(formatted) 

      } 
     }); 

     fileMenu.add(exportItem);   
+0

有帮助吗?请!! – user3362685