程序应该等待按下按钮(Java)

时间:2016-11-24 09:22:42

标签: java swing button wait

我的具体问题是:在下面的代码中程序运行并直接进入最后一行“system.out.println(”转到这一点“)”。我希望它等到按下按钮(buttonsav)选择一些文件。稍后将处理这些文件。没有这些文件,程序无法运行。那么,如何让程序等待buttonclick。点击程序后,请转到最后一行" system.out ....然后继续进行,然后再进行!!这是为了使问题更加清晰而减少的代码:

import java.awt.BorderLayout;
import java.io.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.*;

import javax.swing.*;
 import javax.swing.filechooser.FileFilter;
 import javax.swing.filechooser.FileNameExtensionFilter;


 import java.util.prefs.*;


 public class FileRead_3 extends JFrame 
 {
      Boolean filesAdded = false;
      String path ="";
      static  int status =0;
      public FileRead_3()
      {

      }

      public static void main(String[] args) 
      {      

        FileRead_3 eval = new FileRead_3(); 
        Preferences pref = Preferences.userRoot();
        eval.path = pref.get("DEFAULT_PATH", "");
           JFrame frame1 = new JFrame("BGMN-Auswertung-Optimierung");  
           frame1.setDefaultCloseOperation(frame1.DISPOSE_ON_CLOSE);
           frame1.setSize(1000, 800);                                
           frame1.setLocationRelativeTo(null);   
           JPanel panel1 = new JPanel();
           BorderLayout bol = new BorderLayout();

           JButton buttonsav   = new JButton("open sav files");
           JButton buttonlst   = new JButton("open lst files");

           eval.filesAdded=false;

           System.out.println("filesaddedbefore "+eval.filesAdded);


               eval.filesAdded = true;
              buttonsav.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){

                  eval.filesAdded = true;
                   // Code to run after button is pressed.

                 System.out.println("Hello sav-world");
            JOptionPane.showMessageDialog(frame1,  " Choose sav-File!");

                JFileChooser chooser_sav = new JFileChooser();  // 1907  
                chooser_sav.setCurrentDirectory(new File(eval.path));
FileFilter sav_filter = new FileNameExtensionFilter("txt-files",".txt");
                chooser_sav.addChoosableFileFilter(sav_filter);
                chooser_sav.setFileFilter(sav_filter);  // 20.07.2016

                chooser_sav.setMultiSelectionEnabled(true);             
                status = chooser_sav.showOpenDialog(null); //18.11.2016

               } // action performed
           }); //action listener
            //  eval.filesAdded = true;


           panel1.add(buttonsav);               
           panel1.add(buttonlst);
           frame1.getContentPane().add(panel1);
           frame1.setVisible(true);
           System.out.println("go to this point");
      }
 }

还有一个问题:当我选择while(eval.filesAdded!= false)时,它直接进入最后一行" system.out ...当我使用while时(eval.filesAdded == false) )我得到一个无限循环 这里的代码那么我还有什么不对?

 public class FileRead_3 extends JFrame 
 {
     Boolean fileAdded = false;
   public FileRead_3()
   {
 }
 public static void main(String[] args) 
   {      
    FileRead_3 eval = new FileRead_3(); 

           JFrame frame1 = new JFrame("BGMN-Auswertung-Optimierung");  
           frame1.setDefaultCloseOperation(frame1.DISPOSE_ON_CLOSE);
           frame1.setSize(1000, 800);                                
           frame1.setLocationRelativeTo(null);   
           JPanel panel1 = new JPanel();
           BorderLayout bol = new BorderLayout();
           MigLayout mlayout = new MigLayout("align 20% 20%","[][] [40]", "[]40[]");
           panel1.setLayout(mlayout);

           JButton buttonsav   = new JButton("open sav files");
           JButton buttonlst   = new JButton("open lst files");
    eval.filesAdded=false;
           System.out.println("filesaddedbefore "+eval.filesAdded);
              while(eval.filesAdded != false)
           {             
              buttonsav.addActionListener(new ActionListener(){
              public void actionPerformed(ActionEvent e){
                  eval.filesAdded = true;
                   // Code to run after button is pressed.

                   System.out.println("Hello sav-world");
            JOptionPane.showMessageDialog(frame1,  " Choose sav-File!");

             JFileChooser chooser_sav = new JFileChooser();  // 1907  
             chooser_sav.setCurrentDirectory(new File(eval.path));
 FileFilter sav_filter= new FileNameExtensionFilter("Sav-files", ".sav");
                chooser_sav.addChoosableFileFilter(sav_filter);
                chooser_sav.setFileFilter(sav_filter);  // 20.07.2016

                chooser_sav.setMultiSelectionEnabled(true);          
                status = chooser_sav.showOpenDialog(null); //18.11.2016

               } // action performed
           }); //action listener
            //  eval.filesAdded = true;
           } // ende while



           panel1.add(buttonsav);
           panel1.add(labsav,"wrap");
           panel1.add(buttonlst);
           panel1.add(lablst,"wrap");
           frame1.getContentPane().add(panel1);
           frame1.setVisible(true);

           buttonsav.addMouseListener(new MouseAdapter()
           {
             public void mouseClicked(MouseEvent me)
             {
                 System.out.println("Hello world");
                   JOptionPane.showMessageDialog(frame1,  " Choose sav-File!");

                  JFileChooser chooser_sav = new JFileChooser();  

                  chooser_sav.setCurrentDirectory(new File(eval.path));
 FileFilter sav_filter= new FileNameExtensionFilter("Sav-files", ".sav");
                  chooser_sav.addChoosableFileFilter(sav_filter);
                   chooser_sav.setFileFilter(sav_filter);                        
                  chooser_sav.setMultiSelectionEnabled(true);                 
                  status = chooser_sav.showOpenDialog(null);                      
                  if(status != chooser_sav.APPROVE_OPTION)
                  {
                         JOptionPane.showMessageDialog(null, "No sav-file chosen!!");
                         System.exit(0);
                  }
                  else
                  {
                         eval.fsav = chooser_sav.getSelectedFiles(); 
                         chooser_sav.setCurrentDirectory(eval.fsav[0]);


                  }
             }
           });
  buttonlst.addMouseListener(new MouseAdapter()
           {
                 public void mouseClicked(MouseEvent me)
                 {
         JOptionPane.showMessageDialog(frame1,  " Choose lst-File!");
         JFileChooser chooser_lst = new JFileChooser();
         chooser_lst.setCurrentDirectory(new File(eval.path));
  FileFilter lst_filter=new FileNameExtensionFilter("Lst-files", ".lst");
         chooser_lst.addChoosableFileFilter(lst_filter);
         chooser_lst.setFileFilter(lst_filter);
chooser_lst.setMultiSelectionEnabled(true);

                   int status2 = chooser_lst.showOpenDialog(null);       
                   if(status2 != JFileChooser.APPROVE_OPTION || status != chooser_lst.APPROVE_OPTION)
                   {
 JOptionPane.showMessageDialog(frame1, "No lst-file chosen!!");

                          System.exit(0);
                   }
                   else
                   {
                       eval.flst = chooser_lst.getSelectedFiles(); 
                       chooser_lst.setCurrentDirectory(eval.flst[0]);

                   }
                 }
           });  
      System.out.println("program runs to this point"); 

0 个答案:

没有答案