2013-04-22 31 views
-2

我的合作伙伴和我一直在研究項目的單獨部分,現在我們想將它們放在一起,但我們遇到了麻煩。他爲jOptionpane編寫了代碼的一部分,但完成的項目將使用jFrame。與jFrame代碼混合使用JOptionpane代碼

代碼的這個特定部分編譯,但它不會寫入任何文件,它在jOptionpane版本中工作。基本上我們想從textfields讀取並將它們寫入.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"); 
      }  
     }   
    } 
} 
} 
+1

使用括號和縮進絕對沒有意義,請先修復它。 – timss 2013-04-22 01:00:53

+0

對不起。在我讀到你的評論之前,我正在解決這個問題。它確實沒有按照我想要的方式粘貼。 – user2016569 2013-04-22 01:10:18

+0

讓寫它的人去修復它。停止混合I/O API的無理由:P – MadProgrammer 2013-04-22 01:10:47

回答

3

他寫了他的代碼的一部分的JOptionPane,但完成的項目將使用JFrame的。

根據需要,框架可能彈出對話框或選項窗格。我在上面看到的一個很好的例子(可怕的格式)代碼是查詢用戶的信息。另一種選擇是,如果它在一個框架中,則會有一個輸入字段(例如JTextField)和一個動作監聽器。它將在當用戶鍵入的東西和命中輸入

在後一種情況下采取行動,你可能會需要它需要使用layout manager,或佈局管理器的組合佈局的多個組件。

使用的消息類型選項窗格可能有JLabel在GUI的適當位置被替換的代碼的部分,但其中的一些消息似乎確實是事情應該出現在自由浮動的選擇窗格。因人而異。

+0

代碼已經在使用帶有監聽器的jTextField。 – user2016569 2013-04-22 02:08:15

+0

在我進行雙目視之前,我只能讀到很多東西。請*,請*,***請注意關於對代碼塊和代碼聲明使用一致的邏輯縮進的建議。 – 2013-04-22 02:11:12

+0

這是第一行。你是在談論代碼的頂部還是底部? – user2016569 2013-04-22 02:15:08