2013-10-17 51 views
0

我有一個程序要求你輸入你的名字和第二個名字。我使用OutputStream將名字保存在工作區中存儲的文件中。我使用BufferedReader來讀取文件,但我試圖獲取它,所以如果用戶在JOptionPane.YES_NO_DIALOG上單擊是,它會使用文件中的名稱!我試着做,如果聲明稱,如果JOptionPane的...然後text.setText(savedName),但它只是出來作爲「歡迎空空」緩衝式閱讀器

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 

public class BingoHelper extends JFrame implements WindowListener, ActionListener{ 
JTextField text = new JTextField(); 

//JLabel bg = new JLabel("helo"); 

private JButton b; { 
     b = new JButton("Click to enter name"); 
     } 

JPanel pnlButton = new JPanel(); 

public static String fn; 
public static String sn; 

public static int n; 

File f = new File("test.txt"); 

public void actionPerformed (ActionEvent e){ 

    Object[] yesNo = {"Yes", 
         "No",}; 
    n = JOptionPane.showOptionDialog(null,"Would you like to use previously entered data?","Welcome Back?",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE, null, yesNo,yesNo[1]); 

    if (n == JOptionPane.NO_OPTION){  
     for(fn=JOptionPane.showInputDialog("What is your first name?");!fn.matches("[a-zA-Z]+");fn.isEmpty()){ 
      JOptionPane.showMessageDialog(null, "Alphabet characters only."); 
      fn=JOptionPane.showInputDialog("What is your first name?"); 
     } 
     for(sn=JOptionPane.showInputDialog("What is your second name?");!sn.matches("[a-zA-Z]+");sn.isEmpty()){ 
      JOptionPane.showMessageDialog(null, "Alphabet characters only."); 
      sn=JOptionPane.showInputDialog("What is your second name?"); 
     } 

    } 
    //JOptionPane.showMessageDialog(null, "Welcome " + fn + " " + sn + ".", "", JOptionPane.INFORMATION_MESSAGE); 
    text.setText("Welcome " + fn + " " + sn + "."); 
    b.setVisible(false); 
    b.setEnabled(false); 
    text.setVisible(true); 
    text.setBounds(140,0,220,20); 
    text.setHorizontalAlignment(JLabel.CENTER); 
    text.setEditable(false); 
    text.setBackground(Color.YELLOW); 
    pnlButton.setBackground(Color.DARK_GRAY); 
    writeToFile(); 
    //bg.setVisible(true); 
} 

private void writeToFile() { 

    String nameToWrite = fn; 
    OutputStream outStream = null; 
    try { 
     outStream = new FileOutputStream(f); 
     outStream.write(nameToWrite.getBytes()); 
     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f))); 
     String savedName = br.readLine(); 

     //System.out.println(savedName); 
     br.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

    } finally { 
     if (null != outStream) { 
      try { 
       outStream.close(); 
      } catch (IOException e) { 
       // do nothing 
      } 
     } 
    } 
} 

回答

0

當您使用JOptionPane用自己的選項JOptionPane將返回指數由用戶選擇的選項 ...

也就是說,在你的情況下,如果用戶選擇「是」,然後JOptionPane將返回0或者如果用戶選擇「否」,則將返回1

因此,而不是使用JOptionPane.NO_OPTION你需要使用1,例如...

Object[] yesNo = {"Yes", 
        "No",}; 
n = JOptionPane.showOptionDialog(null,"Would you like to use previously entered data?","Welcome Back?",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE, null, yesNo,yesNo[1]); 

if (n == 1){  
    //... 
} 

我還要強烈,建議您避免在這種情況下使用static字段引用,因爲它可能會導致意外的行爲,如果你有機會多然後運行該類的一個實例;)

+0

謝謝,但我問了更多關於此程序的文件流式部分。我需要一個程序,用於在下次運行程序時將所輸入的內容保存到輸入對話框中(在第一個消息對話框中單擊否之後)。下一次我運行該程序,並在選項對話框中單擊「是」,我試圖讓文本字段說出用戶上次輸入時輸入的內容。 – JavaScrub