/** Writes the string to the input file.
        */
        public void writeStringToFile(File file, String s){                try{
                        FileWriter filewriter = new FileWriter(file);
                        StringReader stringreader = new StringReader(s);
                        BufferedReader bufferedreader = new BufferedReader(stringreader);
                        String lineread = "";
                        while ((lineread = bufferedreader.readLine()) != null){
                                filewriter.write(lineread + "\r\n");
                        }
                        filewriter.close();
                }catch (FileNotFoundException fnfe){
                        System.err.println("FileNotFoundException: " + fnfe.getMessage());
                        showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
                }catch (IOException ioe){
                        System.err.println("IOException: " + ioe.getMessage());
                        showExceptionErrorMessage("IOException: " + ioe.getMessage());
                }
        }        /** Appends the contents of the input file to the text editor.
        */
        public void writeFileToEditor(File file){                try{
                        FileReader filereader = new FileReader(file);
                        BufferedReader bufferedreader = new BufferedReader(filereader);
                        String lineread = "";
                        while ((lineread = bufferedreader.readLine()) != null){
                                editor.append(lineread + "\n");
                        }
                        filereader.close();
                }catch (FileNotFoundException fnfe){
                        System.err.println("FileNotFoundException: " + fnfe.getMessage());
                        showExceptionErrorMessage("FileNotFoundException: " + fnfe.getMessage());
                }catch (IOException ioe){
                        System.err.println("IOException: " + ioe.getMessage());
                        showExceptionErrorMessage("IOException: " + ioe.getMessage());
                }        } /*write input on the textarea as the information of a log
*/
        public void writeToTextArea() {                final int nday = 1;
                final int nexercise = 9;
                final int nrepititions = 46;
                final int nsets = 63;
                final int nprogram = 28;
                final int nintensity = 72;
                final int ndone = 86;                try{
//set the first line of a log
                        StringBuffer str = new StringBuffer(" Day     Exercise           Program           Repititions      Sets     Intensity     Done(%)");                        if ( isfirst ) {
                            editor.append( str.toString() );
                            editor.append("\n");
                            isfirst = false;
                        }                        for ( int i=0; i<str.length(); i++ ) {
                            str.replace(i, i+1, " ");
                        }                        int t = str.length();
//get the info from input
                        str.insert(nday, day.getText().trim());
                        str.insert(nexercise, strExercise);
                        str.insert(nprogram, program.getText().trim());
                        str.insert(nrepititions, repititions.getText().trim());
                        str.insert(nsets, sets.getText().trim());
                        str.insert(nintensity, intensity.getText().trim());
                        
                        if ( t < str.length() ) {
                            editor.append( str.toString() );
                            editor.append("\n");
                        }
//error message
                }catch (IllegalArgumentException e){
                        System.err.println("IllegalArgumentException: " + e.getMessage());
                        showExceptionErrorMessage("IllegalArgumentException: " + e.getMessage());
                }
        }        /** Provides actions to Log(user input).
        */
        public void actionPerformed(ActionEvent actionevent){
                String actioncommand = actionevent.getActionCommand();
                if (actioncommand.compareTo("Exit") == 0) {
                        System.exit(0);
                }else if (actioncommand.compareTo("Load Log") == 0) {
                        File f = getAFileToOpen();
                        writeFileToEditor(f);
                }else if (actioncommand.compareTo("Save Log") == 0) {
                        String s = editor.getText();
                        File f = getAFileForSave();
                        writeStringToFile(f,s);
                        showMessage("Contents of the Text Editor saved to file " + f.getName());
                }else if (actioncommand.compareTo("Add") == 0) {
                        writeToTextArea();
                }
        }        /** Conveniently displays a message to the user and waits for
        * confirmation.
        */
        public void showMessage(String s){
                JOptionPane.showMessageDialog(frame,s);
        }        /** Conveniently displays an exception error message to the user
        * and waits for confirmation.
        */
        public void showExceptionErrorMessage(String s){
                JOptionPane.showMessageDialog(frame,s,s,JOptionPane.ERROR_MESSAGE);
        }
}

解决方案 »

  1.   

    /*the class of login dialog window
    */
    class LoginDialog extends JDialog {        JPanel panel = new JPanel();

    //set the username and password
            String username = new String("zongyi");
            String password = new String("zongyi");        JTextField userText;
            JPasswordField pwText;        int loginCount = 0;
            JFrame parentFrame;        LoginDialog(JFrame parent) {                super(parent, "Login Dialog", true);
                    setSize(300, 150);
                    getContentPane().add(panel);
                    parentFrame = parent;                init();
             }         public void init() {
    //set the layout
                    JPanel panel1 = new JPanel();
                    JPanel panel2 = new JPanel();
                    JPanel panel0 = new JPanel();                panel1.setLayout( new GridLayout(3,2) );                panel.add(panel0, "North");
                    panel.add(panel1, "Center");
                    panel.add(panel2, "South");                JLabel userLabel = new JLabel("Username");
                    panel1.add( userLabel );
                    userText = new JTextField("", 10);
                    panel1.add( userText );                JLabel pwLabel = new JLabel("Password");
                    panel1.add( pwLabel );
                    pwText = new JPasswordField("", 10);
                    panel1.add( pwText );                JButton ok = new JButton("OK");
                    panel2.add(ok); //check the usernaem and password to login
                    ok.addActionListener(new java.awt.event.ActionListener() {
                            public void actionPerformed(ActionEvent e) {                              if ( userText.getText().compareTo(username) == 0 &&
                                       pwText.getText().compareTo(password) == 0 ) {                                   dispose();
                                  }
                                  else{                                  JOptionPane.showMessageDialog( parentFrame, "Invaild Login, please try again!" );                                  userText.setText("");
                                      pwText.setText("");                                  loginCount ++;
                                      if ( loginCount > 3 ) exit();                              }
                            }
                    });                JButton cancel = new JButton("CANCEL");
                    panel2.add(cancel);                cancel.addActionListener(new java.awt.event.ActionListener() {
                            public void actionPerformed(ActionEvent e) {
                               exit();
                            }
                    });
             }
     
     //action of exit
             public void exit() {             dispose();
                 System.exit(0);
             }
    }