Java 基本GUI验证任务

Java 基本GUI验证任务,java,eclipse,validation,user-interface,Java,Eclipse,Validation,User Interface,我被分配了一组任务,如下所示,除了验证之外,我已经完成了所有需要的工作。到目前为止,我还包括了我的代码。我想得到一些帮助。谢谢基于此,我目前正在使用eclipse来创建这个 此过程应包括根据预期格式验证文件内容。如果缺少某些内容,则应以对话框的形式向用户显示错误消息 public class BasicGUI extends JFrame { private JPanel contentPane; public JTextArea inputTextArea; pu

我被分配了一组任务,如下所示,除了验证之外,我已经完成了所有需要的工作。到目前为止,我还包括了我的代码。我想得到一些帮助。谢谢基于此,我目前正在使用eclipse来创建这个

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

 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);
            }
     });


   }

}
公共类BasicGUI扩展JFrame{
私有JPanel内容窗格;
公共JTextArea输入extarea;
公共JTextArea outputGraphicalArea;
JSplitPane拆分窗格;
公共基础GUI(){
createMenuBar();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=newjpanel();
setboorder(新的EmptyBorder(5,5,5,5));
setLayout(新的BorderLayout(0,0));
InputExtArea=新的JTextArea();
outputGraphicalArea=新的JTextArea();
JScrollPane ScrollPanel Left=新的JScrollPane(InputExtArea);
JScrollPane scrollPanelRight=新的JScrollPane(outputGraphicalArea);
JSplitPane应用程序面板=新的JSplitPane(JSplitPane.VERTICAL_SPLIT,
滚动面板左,滚动面板右);
applicationpanel.setOneTouchExpandable(真);
添加(applicationpanel,BorderLayout.CENTER);
applicationpanel.setResizeWeight(0.5);
setContentPane(contentPane);
设置标题(“要求1+2”);
设置大小(350250);
setLocationRelativeTo(空);
setDefaultCloseOperation(关闭时退出);
setVisible(真);
}
私有void createMenuBar(){
JMenuBar menubar=新的JMenuBar();
JMenu fileMenu=新JMenu(“文件”);
JMenu helpMenu=新JMenu(“帮助”);
JMenuItem aboutMenu=新的JMenuItem(“关于”);
aboutMenu.addActionListener(新建ActionListener())
{
已执行的公共无效操作(操作事件)
{
如果(对话框==null)
dialog=新的AboutDialog(BasicGUI.this);
对话框.setVisible(true);
}
});                
ImageIcon iconLoad=新的ImageIcon(“load_icon.png”);
JMenuItem loadMi=新的JMenuItem(“加载”,iconLoad);
loadMi.addActionListener(新ActionListener(){
已执行的公共无效行动(行动事件ae){
JFileChooser chooser=新的JFileChooser();
int status=chooser.showOpenDialog(null);
if(状态!=JFileChooser.APPROVE\u选项)
InputExtArea.setText(“未选择文件”);
其他的
{
File File=chooser.getSelectedFile();
扫描仪扫描=空;
试一试{
扫描=新扫描仪(文件);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
字符串信息=”;
while(scan.hasNext())
info+=scan.nextLine()+“\n”;
InputExtArea.setText(信息);
}
}
});
ImageIcon iconSave=新的ImageIcon(“save_icon.png”);
JMenuItem saveMi=新的JMenuItem(“保存”,iconSave);
ImageIcon iconExit=新的ImageIcon(“exit_icon.png”);
JMenuItem exitMi=新的JMenuItem(“退出”,iconExit);
exitMi.setMnemonic(KeyEvent.VK_E);
exitMi.setToolTipText(“退出应用程序”);
exitMi.setAccelerator(击键.getKeyStroke(KeyEvent.VK_E,
ActionEvent.CTRL_掩码);
exitMi.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件){
系统出口(0);
}
});
fileMenu.add(loadMi);
fileMenu.add(saveMi);
fileMenu.addSeparator();
fileMenu.add(exitMi);
菜单栏。添加(文件菜单);
菜单栏。添加(关于菜单);
menubar.add(Box.createHorizontalGlue());
菜单栏。添加(帮助菜单);
setJMenuBar(菜单栏);
}
私人AboutDialog对话框;
}
@抑制警告(“串行”)
类AboutDialog扩展了JDialog
{
关于ALOG的公共信息(JFrame所有者)
{
super(所有者,“关于对话框”,true);
加(
新JLabel
(
“要求1–基本GUI创建
此任务的第一个要求是实现基本图形用户界面(GUI)作为初始原型。此时,应用程序将是一个“空壳”,功能非常有限。但是,它应该由一个应用程序框架、一个菜单栏和一个分为两半的主应用程序面板组成。其中一半应该能够显示正在处理的文件的文本表示,另一半将(最终)显示该数据的图形表示
文本面板应能够显示从文本文件读取的所有信息(见要求2),但以美观的方式。您可以选择使用标签和相关值来显示标题信息,如“标题”。数据应显示在某种文本窗口中(需要时带有滚动条)应用程序的设计应使其在适当的情况下使用布局管理器,并可由用户合理调整大小。菜单选项还应在适当的情况下包括快捷方式和图标。
要求2–加载和解析
一旦基本GUI可用
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

}