基本GUI验证任务

时间:2016-04-08 11:52:45

标签: java eclipse validation user-interface

我已经获得了一系列任务,如下所示我已经设法完成除验证之外所需的一切。到目前为止,我还包含了我的代码。我会喜欢一些帮助。谢谢。从此我正在使用eclipse来创建它。

此过程应涉及根据预期格式验证文件的内容。如果缺少某些内容,则应以对话框的形式向用户显示error消息。

 public class BasicGUI extends JFrame {


    private JPanel contentPane;
    public JTextArea inputTextArea;
    public JTextArea outputGraphicalArea;

    JSplitPane splitPane;




    public BasicGUI() {

        createMenuBar();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

        contentPane.setLayout(new BorderLayout(0, 0));




        inputTextArea = new JTextArea();
        outputGraphicalArea = new JTextArea();

        JScrollPane scrollPanelLeft = new JScrollPane(inputTextArea);
        JScrollPane scrollPanelRight = new JScrollPane(outputGraphicalArea);

        JSplitPane applicationpanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                scrollPanelLeft, scrollPanelRight);

        applicationpanel.setOneTouchExpandable(true);

        contentPane.add(applicationpanel, BorderLayout.CENTER);
        applicationpanel.setResizeWeight(0.5); 


        setContentPane(contentPane);
        setTitle(" Requirement 1 + 2 ");
        setSize(350, 250);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);


    }
    private void createMenuBar() {

        JMenuBar menubar = new JMenuBar();
        JMenu fileMenu = new JMenu("File");
        JMenu helpMenu = new JMenu("Help");
       JMenuItem aboutMenu = new JMenuItem("About");
       aboutMenu.addActionListener(new ActionListener()
          {
             public void actionPerformed(ActionEvent event)
             {
                if (dialog == null) 
                dialog = new AboutDialog(BasicGUI.this);
                dialog.setVisible(true); 
             }
          });                
        ImageIcon iconLoad = new ImageIcon("load_icon.png");
        JMenuItem loadMi = new JMenuItem("Load", iconLoad);


        loadMi.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            JFileChooser chooser = new JFileChooser();
            int status = chooser.showOpenDialog(null);
            if (status != JFileChooser.APPROVE_OPTION)
                inputTextArea.setText("No File Chosen");
             else
             {
                 File file = chooser.getSelectedFile();
                 Scanner scan = null;
                try {
                    scan = new Scanner(file);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                 String info = "";
                 while (scan.hasNext())
                    info += scan.nextLine() + "\n";


                 inputTextArea.setText(info);
             }


        }
        });

       ImageIcon iconSave = new ImageIcon("save_icon.png");
       JMenuItem saveMi = new JMenuItem("Save", iconSave);


     ImageIcon iconExit = new ImageIcon("exit_icon.png");
     JMenuItem exitMi = new JMenuItem("Exit", iconExit);

       exitMi.setMnemonic(KeyEvent.VK_E);       
       exitMi.setToolTipText("Exit application");

       exitMi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
           ActionEvent.CTRL_MASK));
       exitMi.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent event) {
               System.exit(0);
           }
    });


       fileMenu.add(loadMi);
       fileMenu.add(saveMi);
       fileMenu.addSeparator();
       fileMenu.add(exitMi);
       menubar.add(fileMenu);
       menubar.add(aboutMenu);
       menubar.add(Box.createHorizontalGlue());
       menubar.add(helpMenu);
       setJMenuBar(menubar);  
    }


       private AboutDialog dialog;
    }



    @SuppressWarnings("serial")
    class AboutDialog extends JDialog
    {
       public AboutDialog(JFrame owner)
       {
          super(owner, "About DialogBox", true);


          add(
                  new JLabel
                  (
                        "<html><h1><i>Requirement 1 –  Basic GUI creation </i></h1><hr>The first requirement for this assignment is to implement the basic Graphical User Interface (GUI) as an initial prototype. At this point the application will be an “empty shell”, with very limited functionality. However it should consist of an application frame, a menu bar and a main application panel split into two halves.  One half should be capable of displaying a textual representation of the file being processed, and the other will (eventually) show the graphical representation of that data<hr>The text panel should be capable of showing all information read from the text file (see requirement 2), but in an aesthetically pleasing manner. You may choose to use labels and associated values to show heading information, such as the 'Title'.  The data should be shown within some kind of text window (with scrollbars when required).<hr>The menu bar should consist of a 'File' and 'Help' menu.  The File menu should include options for loading, saving and exiting the application.  The 'Help' menu should contain an option for showing a dialogue box which identifies information about the application. At this point however only the 'Exit' and 'About' options need to work<hr>The application should be designed so that it uses layout managers where appropriate, and can be sensibly resized by the user.  The menu options should also include short-cuts and icons where appropriate.<hr><h1><i>Requirement 2 –  Loading and parsing </i></h1><hr>Once a basic GUI is available the next requirement is to add the ability to actually load, parse and display the data. The 'File | Load' option should show a file open dialogue allowing selection of a data file.  Once this is done the file should be opened, read and parsed.  This process should involve validating the contents of the file against the expected format. If something is missing then an error message should be shown to the user in the form of a dialogue box.<hr>Once the file information has been loaded and parsed, the information should be displayed within the appropriate textual representation elements of the GUI (as developed as part of requirement 1).</html>"  
                  ),
                  BorderLayout.CENTER);


          JButton ok = new JButton("Ok");
          ok.addActionListener(new ActionListener()
             {
                public void actionPerformed(ActionEvent event)
                {
                   setVisible(false);
                }
             });



          JPanel panel = new JPanel();
          panel.add(ok);
          add(panel, BorderLayout.SOUTH);

          setSize(550, 750);

      ImageIcon iconExit = new ImageIcon("exit_icon.png");
      JMenuItem exitMi = new JMenuItem("Exit", iconExit);

        exitMi.setMnemonic(KeyEvent.VK_E);
        exitMi.setToolTipText("Exit application");

        exitMi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_E,
            ActionEvent.CTRL_MASK));
        exitMi.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
     });


   }

}

1 个答案:

答案 0 :(得分:0)

要根据格式验证文件内容,首先您显然需要打开文件来分析其内容。这是一种方法:

File file = chooser.getSelectedFile();

try(FileReader fileReader = new FileReader(file);) {

    BufferedReader buffReader = new BufferedReader(fileReader);

    String line = buffReader.readLine();

    //you must adapt this to your particular format
    String format = "^Title: \w+ Xlabel: \w+ Ylabel: \w+ start: \d+ interval: \d+-\d+ \w+$";

    if(line.matches(format)){

        //file format is valid, do something

    }else{

        //file format is not valid, show error message

    }

} catch (IOException e) {

    //Handle the exception

}

查看still does文档和BufferedReader的Java教程。

从String类中了解Reading, Writing, and Creating Files和方法regular expressions