-2

我和我的搭档一直在做一个项目的不同部分,现在我们想把它们放在一起,但我们遇到了麻烦。他为 jOptionpane 编写了他的部分代码,但完成的项目将使用 jFrame。

代码的这个特定部分可以编译,但它不会向文件中写入任何内容,它在 jOptionpane 版本中工作。基本上我们想从文本字段中读取并将它们写入 .txt 文件。我知道在使用基于 unix 的操作系统时可能会有细微的差别,但我们在 Windows 上也没有成功。我试图模仿这个网站的其他答案,但我没有任何运气。有什么提示或建议吗?

public void jTextField1ActionPerformed (java.awt.event.ActionEvent te)               
{             
    String user,usersPass;
    FileWriter fw;
    FileReader fr;
    BufferedWriter writer;
    BufferedReader reader;



    if (te.getSource() == jTextField1)

    {

        user = jTextField1.getText();

    }
        else if (te.getSource() == jTextField2)

            {

                usersPass = jTextField2.getText();

             }


                try
                   {
                       ArrayList<String> nameList = new ArrayList<String>(100);
                       ArrayList<String> passwords = new ArrayList<String>(100);



                       File userNames = new File("Username.txt");
                       Scanner users = new Scanner(userNames);
                       File userPass = new File("Passwords.txt");
                       Scanner pass = new Scanner(userPass);



                       // FileWriter fw;
                       //  FileReader fr;
                       fw = new FileWriter("Macintosh  Hd/Users/smooth4prez/Desktop/Project/Username.txt");
                       fr = new FileReader("Macintosh Hd/Users/smooth4prez/Desktop/Project/Passwords.txt");
                       writer = new BufferedWriter(fw);
                       reader = new BufferedReader(fr);
                   }
                   catch(FileNotFoundException E)
                       {
                           E.printStackTrace();
                       }
                               catch(IOException e)
                                   {
                               e.printStackTrace();
                                   }

}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
 {

        if (evt.getSource() == jButton1)

            {
                dispose ();

                new SignIn().setVisible(true);

            }

}

这是我们正在尝试重做的 jOptionpane 版本。

import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.io.*;

public class LoginTest implements Serializable

{
    Scanner keyboard = new Scanner(System.in);  


 public static void main(String[] args)throws FileNotFoundException, IOException, NotSerializableException
 {
    Username();
 }
 public static void Username()throws FileNotFoundException, IOException
 {
   File userNames = new File("Username.txt");
    Scanner users = new Scanner(userNames);
    File userPass = new File("Passwords.txt");
    Scanner pass = new Scanner(userPass);

    FileWriter fWriteUser = new FileWriter("Username.txt", true);
   PrintWriter outUser = new PrintWriter(fWriteUser);
    FileWriter fWritePass = new FileWriter("Passwords.txt", true);
   PrintWriter outPass = new PrintWriter(fWritePass);       

    ArrayList<String> nameList = new ArrayList<String>(100);
  ArrayList<String> passwords = new ArrayList<String>(100);

    int userCount = 0;

    while(users.hasNext())
    {
        String oldName = users.nextLine();
        nameList.add(oldName);
        userCount = userCount + 1;
    }
    while(pass.hasNext())
    {
        String oldPass = pass.nextLine();
        passwords.add(oldPass);
    }

    String strNumUsers = "";

    strNumUsers = JOptionPane.showInputDialog("How many users would you like to register?");

    int numUsers = Integer.parseInt(strNumUsers);

    int newCount = userCount + 1;

   for(int j = 0;j < numUsers;j++)
    {

        String username = JOptionPane.showInputDialog("Enter the username");

        for(int i = 0;i < nameList.size();i++)
        {
            if(username.equals(nameList.get(i)))
            {
                JOptionPane.showMessageDialog(null, "That username is already taken");
                username = JOptionPane.showInputDialog("Enter the username");

                i = 0;
            }
        }

        nameList.add(username);              

        String password = JOptionPane.showInputDialog("Enter password");
       passwords.add(password);

        File dir = new File("Inbox" + newCount); 
        dir.mkdir();
        File folder = new File("Inbox" + newCount);

        File m;
        m = new File("Macintosh Hd/Users/smooth4prez/Desktop/Project3/Inbox" + newCount + "/messages.txt");
        m.createNewFile();

        File n;
        n = new File("Macintosh Hd/Users/smooth4prez/Desktop/Project3/Inbox" + newCount + "/outbox.txt");
        n.createNewFile();

        newCount = newCount + 1;
   }

    for(int k = userCount;k < nameList.size();k++)
    {
        outUser.println(nameList.get(k));
        outPass.println(passwords.get(k));
    }
    outUser.close();
    outPass.close();

    String signInUserName = JOptionPane.showInputDialog("Enter username to sign in");

    for(int i = 0;i < nameList.size();i++)
    {
        if(signInUserName.equals(nameList.get(i)))
        {

            String signInPassword = JOptionPane.showInputDialog("Please enter your password");
            if(signInPassword.equals(passwords.get(i)))
            {
                String response = "";

                JOptionPane.showMessageDialog(null, "Signed in!");
                i = nameList.size();

                response = JOptionPane.showInputDialog("Would you like to send a message, view messages, or exit?" + 
                "(Please enter send, view, or exit.)");

                /*switch(response.trim())
                {
                case "Send":
                case "send":
                    Messages.MessageTest(nameList, signInUserName);
                    break;
                case "View":
                case "view":
                    Messages.MessageView(nameList, signInUserName);
                    break;
                case "Exit":
                case "exit":
                    break;
                case "":
                    JOptionPane.showMessageDialog(null, "You didn't enter anything, DIPSHIT!");
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "That is not a proper response.");
                    break;
                }*/
                /*
                Save s = new Save();
                testObject Rob = new testObject();
                s.saveMyObject("myfile", Rob);

                //Saving an object with class Save.java
                /*
                Save s = new Save();
                Object myObject = new Object();
                String test = "test";
                myObject = (Object)test;
                s.saveMyObject("myfile", myObject);
                */


                //insert program to search for specific user homepage file
                //if found, load that homepage, if not found, call method to create homepage
                //and save it as a file with the title including the username
            }
            else
            {
                JOptionPane.showMessageDialog(null, "Incorrect password");
                i = nameList.size();
            }
        }
        else if(!signInUserName.equals(nameList.get(i)))
        {
            if(i + 1 == nameList.size())
            {
            JOptionPane.showMessageDialog(null, "That is not a valid username");
            }       
        }           
    }
} 
}
4

1 回答 1

3

他为 jOptionpane 编写了他的部分代码,但完成的项目将使用 jFrame。

框架可能会根据需要弹出对话框或选项窗格。我在上面的(格式可怕的)代码中看到的一个很好的例子是向用户查询信息。如果它在一个框架中,另一种选择是有一个JTextField带有动作监听器的输入字段(例如)。当用户输入内容并点击时,它将被执行Enter

在后一种情况下,您可能需要使用布局管理器或布局管理器的组合来布置多个组件。

使用消息类型选项窗格的代码部分可能会在 GUI 中的适当位置替换为 a JLabel,尽管其中一些消息实际上似乎应该出现在自由浮动选项窗格中。YMMV。

于 2013-04-22T01:50:40.993 回答