我的源代码在下面,我想实现的功能是运行程序后,会出现一个界面。里面有submit和change setting两个按钮。点击submit之后,程序会查看c盘根目录下是否有xiang这个文件夹,如果没有则会弹出一个jframe,里面有yes和no两个按钮。依此来决定xiang里面的文件内容。只有当使用者选择了yes或no后,才打印“我是猪”。如果有xiang文件夹,则直接打印“我是猪”。
现在的问题是,当我没有xiang这个文件夹的时候。程序跳出的jframe是空白。我要设置的按钮全部没有。然后程序就卡在这里了。需要强制关闭。这是为什么呢?
急啊,在线等待。
跳出的jframe可以通过点击change setting查看。但是点击yes和no后会创建xiang文件夹。需要删掉才能出现这个场景。
提前谢谢各位大侠了。import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;public class temp{
public static JFrame mainFrame3 = new JFrame();
public static void changeSetting()
{

   JPanel ChangeSetting = new JPanel(new GridLayout(2, 1));
   JPanel pane = null;
   pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
   pane.add(new JLabel("Do you want your information to be encrypted?\r\nReminder:It will take longer time to finish the transimission if it is encrypted."));
   ChangeSetting.add(pane);
    
   ActionListener buttonListener = null;
   buttonListener = new ActionListener() {    public void actionPerformed(ActionEvent e) {
   if (e.getActionCommand().equals("YES")) {
   String folder=new String();
   folder="C:/xiang/";
   File dir2=new File(folder);
   boolean bFile = dir2.exists();
   if( bFile == true )
   {
   }
   else
   {
   dir2.mkdir();
   }
   File toFile = new File(dir2, "setting.in");    try{
   byte b[]="Encryption:Yes".getBytes();
   FileOutputStream os=new FileOutputStream(toFile);
   os.write(b);
   os.close();
   mainFrame3.dispose();
   }
   catch(IOException e1){
   System.out.println(e1);
   }
   }
   if (e.getActionCommand().equals("NO")) {
   String folder=new String();
   folder="C:/xiang/";
   File dir2=new File(folder);
   boolean bFile = dir2.exists();
   if( bFile == true )
   {
   }
   else
   {
   dir2.mkdir();
   }
   File toFile = new File(dir2, "setting.in");
         
   try{
   byte b[]="Encryption:No".getBytes();
   FileOutputStream os=new FileOutputStream(toFile);         
   os.write(b);
   os.close();
   mainFrame3.dispose();
   }
   catch(IOException e1){
   System.out.println(e1);
   }
   
   }
   }
   };
    
   JPanel ButtonPanel= new JPanel(new GridLayout(1, 2));
   JButton button1 = new JButton("YES");    JButton button3 = new JButton("NO");
   button1.setEnabled(true);
   button1.setActionCommand("YES");
   button1.addActionListener(buttonListener);
   
   button3.setEnabled(true);
   button3.setActionCommand("NO");
   button3.addActionListener(buttonListener);
   ButtonPanel.add(button1);
   ButtonPanel.add(button3);
   ChangeSetting.add(ButtonPanel);
//main frame   
   mainFrame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   mainFrame3.setContentPane(ChangeSetting);
   mainFrame3.setSize(mainFrame3.getPreferredSize());
   mainFrame3.setLocation(200, 200);
   mainFrame3.pack();
   mainFrame3.setVisible(true);
}
public static void main(String args[]){  
final JFrame mainFrame2 = new JFrame();
   JPanel LogInWin = new JPanel(new GridLayout(2, 1));
   JPanel pane = null;
   
  
    
    pane = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    pane.add(new JLabel("User"));
    JTextField idField = new JTextField(15);
    idField.setText("kkk");
    idField.setEnabled(false);
    pane.add(idField);
    LogInWin.add(pane);    
//button
   ActionListener buttonListener = null;
    buttonListener = new ActionListener() {        public void actionPerformed(ActionEvent e) {
             if (e.getActionCommand().equals("Submit")) {
             
              String folder=new String();
  folder="C:/xiang/";
  File dir2=new File(folder);
  boolean bFile = dir2.exists();
  if(!bFile)
  {
  changeSetting(); 
  }
 
  int n=1;
  while(n==1)
  {
  if(!mainFrame3.isShowing())
  {
  System.out.println("我是猪");
  n++;
  }
  }
 
           }
             if (e.getActionCommand().equals("Change Setting")) {              changeSetting();
             }
        }
        
} ;
JPanel ButtonPanel= new JPanel(new GridLayout(1, 2));
JButton button1 = new JButton("Submit");JButton button3 = new JButton("Change Setting");
button1.setEnabled(true);
button1.setActionCommand("Submit");
button1.addActionListener(buttonListener);button3.setEnabled(true);
button3.setActionCommand("Change Setting");
button3.addActionListener(buttonListener);
ButtonPanel.add(button1);
ButtonPanel.add(button3);
LogInWin.add(ButtonPanel);
//main frame
mainFrame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame2.setContentPane(LogInWin);
mainFrame2.setSize(mainFrame2.getPreferredSize());
mainFrame2.setLocation(200, 200);
mainFrame2.pack();
mainFrame2.setVisible(true);
}
}

解决方案 »

  1.   


    晕,你都没有创建文件夹。 在后面加上这句代码dir2.mkdir();
      

  2.   

    那个我创建文件夹是在changesetting方法里做的。
    if( bFile == true )
    {
    }
    else
    {
    dir2.mkdir();
    }
      

  3.   

    我说的是在你的public void actionPerformed(ActionEvent e){}这个方法里加:
      

  4.   

    那个,你所指的死循环是不是这个
    int n=1;
      while(n==1)
      {
      if(!mainFrame3.isShowing())
      {
      System.out.println("我是猪");
      n++;
      }
      }
    我的想法是,main里面只是才看下xiang存不存在。创建文件的事情交给changeSetting()来做。而如果xiang不存在,进入循环后则要等待mainFrame3消失了才能打印东西。而现在的问题是mainFrame3出现了,可是里面的panel没有被加载上去。
    谢谢高手们耐心的解答。
    if(!bFile)
      {
      changeSetting();
      }
    这个只会执行一次。