【问题标题】:Simple Java editor GUI简单的 Java 编辑器 GUI
【发布时间】:2014-02-27 23:13:55
【问题描述】:

您好,我为简单的 Java 编辑器创建了 GUI 代码

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

打开:使用源代码 java ( .java) 打开文件。 保存:将当前的 sn-p 保存在与创建期间建立的文件相同的文件中。 另存为:显示您请求文件名的对话框,将在其中存储文件的目录格式。格式为 java 文件 (.java)。窗口的主要部分将具有供用户用于编辑文件源 Java 的编辑器。

窗口的主要部分将具有供用户用于编辑文件源 Java 的编辑器。在sn -p处理过程中将被更新的信息:Number lines Java源代码中的保留字数

格式化文本


每个文件都会被格式化打开,并且在按以下规则处理时会被格式化: java的保留字会以蓝色显示。 cmets 将显示为绿色 带有橙色的字符串文字 所有其他黑色 字体将是 Courier 字体大小 12pt

我将提供 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

【问题讨论】:

  • 请尝试缩小您的问题范围,这样您获得答案的机会可能会更高。
  • 你应该尽量减少问题并尽可能完整地删除噪音......以获得快速答案,并且可能是一个很好的答案!
  • 问题是什么?你有什么问题?

标签: java


【解决方案1】:

首先你应该看看File 类。它为您提供创建、打开、修改和保存文件的方法。要阅读文件,您可能还想给 BufferedReader 或任何其他读者一个机会。

  • 创建文件:FilecreateNewFile() 方法,与exists() 结合使用。
  • 要打开和读取文件,请使用 try-with-resource(Java 手册中实际上有一个很好的 tutorial 关于它)。
  • 要保存文件,您应该查看FileWriter,它可以写入字符串或将它们附加到文件中。
  • 对于您的编辑器,您可能希望将前面提到的BufferedReader 替换为LineReader,它还提供了获取行号的方法。除此之外,您必须自己弄清楚如何给行编号。 (实际上只是在谷歌上搜索了很多,会有一些想法,如this one - 我没有详细检查,但它可能会有所帮助)。
  • 当然,对于编辑器,您应该首先将文件读入字符串,使用格式化程序,然后您可以呈现它并在需要时重新格式化。

除了这些提示之外,我无法为您提供更详细的答案,因为您也可以在 cmets 中阅读,如果您提供更详细的问题会更容易。您现在只是给了我们一个 GUI,它几乎与您的实际问题无关。
向我们展示您的一些(有问题的)工作,我们可以帮助您,但除此之外,我们只能像我刚才所做的那样给您一些提示。因此,请尝试考虑您的问题,尝试如何寻求更准确的答案,并根据需要提出一些新问题。
但是不要忘记查看该网站以获取答案,对我来说,我想问的几乎所有问题都已经在某个地方以类似的方式提出过。

【讨论】:

    【解决方案2】:

    你好,让我们把工作分成几个步骤,

    首先我想创建新的,打开,保存,另存为,导出到 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);           
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2010-09-10
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多