2013-10-17 48 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